8 Replies - 1127 Views - Last Post: 10 September 2011 - 02:57 AM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

asterisks histogram issue

Posted 05 September 2011 - 10:06 PM

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

void printHisto( int[], int );
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	const int MAX = 10;
	int numArray[ MAX ];
	int count = 0;

	while( count < MAX && numArray[ count ] != 'exit' )
	{
		cout << "Enter a POSITIVE integer: ";
		cin >> numArray[ count ];

		if (numArray[ count ] < 0)
		{
			cout << "Number not a POSITIVE integer! " << endl
				  << "Enter a POSITIVE integer: ";
			cin >> numArray[ count ];
		}
		count++;
	}
	system("cls");
   
   printHisto( numArray, count );
	getch();
	return 0;
}
//------------------------------------------------------------------------------
void printHisto( int numArray [], int count )
{
   for( int i = 0; i < count; i++ )
   {
      for( int j = 0; j < numArray[ i ]; j++ )
      {
         cout << "*";
      }
      cout << endl;
   } 
}  
//------------------------------------------------------------------------------



Now I have 1 problem... other than that my code seems to be ok...

1) When I type "exit" it automatically plays out to the maximum of 10 loops... where the user should be able to choose only 5 or 4 entries etc

This post has been edited by rdhc1330: 05 September 2011 - 10:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: asterisks histogram issue

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: asterisks histogram issue

Posted 05 September 2011 - 10:25 PM

You are a "victim" :) of a dangling line terminator stored in the console's input buffer. When you enter a number in response to the prompt the data actually stored in the input buffer consists of the numeric string representing the number and a line terminator indicating where the end of the string is.

Trying to enter text - "exit" - after entering numeric strings results in an error in cin. As a result no further input, text or numeric, will be accepted by cin. You need to clear the error before reading cin again: cin.clear();.

You are getting the error due to trying to store a string in an INTEGER array. This forces a type conversion on cin setting the error bit.

It's hard to type one handed lying on my side. I'll let others point out your other errors. :wheelchair:

This post has been edited by n8wxs: 05 September 2011 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: asterisks histogram issue

Posted 05 September 2011 - 10:32 PM

View Postn8wxs, on 05 September 2011 - 10:25 PM, said:

You are a "victim" :) of a dangling line terminator stored in the console's input buffer. When you enter a number in response to the prompt
the data actually stored in the input buffer consists of the numeric string representing the number and a line terminator indicating where the end of the string is.

Trying to enter text - "exit" - after entering numeric strings results in an error in cin. As a result no further input, text or numeric, will be accepted by cin. You need to clear the error before reading cin again: cin.clear();.


where do I put it?
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: asterisks histogram issue

Posted 05 September 2011 - 10:34 PM

duh, after each call?
Was This Post Helpful? -2
  • +
  • -

#5 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: asterisks histogram issue

Posted 05 September 2011 - 10:38 PM

View Postn8wxs, on 05 September 2011 - 10:34 PM, said:

duh, after each call?

int main(int argc, char *argv[])
{
	const int MAX = 10;
	int numArray[ MAX ];
	int count = 0;

	while( count < MAX && numArray[ count ] != 'exit' )
	{
		cout << "Enter a POSITIVE integer: ";
		cin >> numArray[ count ];
		cin.clear();

		if (numArray[ count ] < 0)
		{
			cout << "Number not a POSITIVE integer! " << endl
				  << "Enter a POSITIVE integer: ";
			cin >> numArray[ count ];
			cin.clear();
		}
		count++;
	}
	system("cls");
   
   printHisto( numArray, count );
	getch();
	return 0;
}
//------------------------------------------------------------------------------



Like so?
Was This Post Helpful? 0
  • +
  • -

#6 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: asterisks histogram issue

Posted 08 September 2011 - 03:06 PM

anyone want to help me on this?

all working apart from that???


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

void printHisto( int[], int );
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	const int MAX = 10;
	int numArray[ MAX ];
	int count = 0;

	while( count < MAX && numArray [ count ] != 'exit' ) 
	{
		cout << "Enter a POSITIVE integer, type exit when you're done: ";
		cin >> numArray[ count ];
		
		if ( numArray [ count ] < 0)
		{
			cout << "Number not a POSITIVE integer! " << endl
				  << "Enter a POSITIVE integer: ";
			cin >> numArray[ count ];
			
		}
		count++;
	}
	system("cls");
   
   printHisto( numArray, count );
	getch();
	return 0;
}
//------------------------------------------------------------------------------
void printHisto( int numArray [], int count )
{
   for( int i = 0; i < count; i++ )
   {
      for( int j = 0; j < numArray[ i ]; j++ )
      {
         cout << "*";
      }
      cout << endl;
   } 
}  
//------------------------------------------------------------------------------


Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: asterisks histogram issue

Posted 09 September 2011 - 10:18 PM

Quote

argc 1 int
+ argv 0x00454530 char * *
- numArray 0x0028f9c8 int [10]
[0] 5 int
[1] 6 int
[2] 7 int
[3] 8 int
[4] -858993460 int
[5] -858993460 int
[6] -858993460 int
[7] -858993460 int
[8] -858993460 int
[9] -858993460 int
count 10 int
MAX 10 const int

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: asterisks histogram issue

Posted 09 September 2011 - 10:56 PM

Quote

Enter a POSITIVE integer, type exit when you're done: 5
Enter a POSITIVE integer, type exit when you're done: 6
Enter a POSITIVE integer, type exit when you're done: 7
Enter a POSITIVE integer, type exit when you're done: 8
Enter a POSITIVE integer, type exit when you're done: exit
Number not a POSITIVE integer!
Enter a POSITIVE integer: Enter a POSITIVE integer, type exit when you're done:
Number not a POSITIVE integer!
Enter a POSITIVE integer: Enter a POSITIVE integer, type exit when you're done:
Number not a POSITIVE integer!
Enter a POSITIVE integer: Enter a POSITIVE integer, type exit when you're done:
Number not a POSITIVE integer!
Enter a POSITIVE integer: Enter a POSITIVE integer, type exit when you're done:
Number not a POSITIVE integer!
Enter a POSITIVE integer: Enter a POSITIVE integer, type exit when you're done:
Number not a POSITIVE integer!
Enter a POSITIVE integer:


View Postn8wxs, on 09 September 2011 - 10:18 PM, said:

Quote

argc 1 int
+ argv 0x00454530 char * *
- numArray 0x0028f9c8 int [10]
[0] 5 int
[1] 6 int
[2] 7 int
[3] 8 int
[4] -858993460 int
[5] -858993460 int
[6] -858993460 int
[7] -858993460 int
[8] -858993460 int
[9] -858993460 int
count 10 int
MAX 10 const int

Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: asterisks histogram issue

Posted 10 September 2011 - 02:57 AM

I have no idea how you hope an int array will compare with 'exit'?

Usually, you want logic like this:

while(1) {
   user inputs something as a string
   if(the input is not a number) {
      if (the input string is "exit" (note the TWO quotation marks))
         break;
      else {
         print "Usage: number, or exit"
         continue;
      }
   }
   //process the number, here.
   atoi or strtoi, function to convert input string to int
   //etc.
}


This post has been edited by Adak: 10 September 2011 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1