2 Replies - 493 Views - Last Post: 08 September 2011 - 09:58 PM Rate Topic: -----

#1 rdhc1330   User is offline

  • D.I.C Head

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

sentinel control error

Posted 08 September 2011 - 08:48 PM

#include <cstdlib>
#include <iostream>
#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, or type exit to finish: ";
		cin >> numArray[ count ];

		if (numArray[ count ] < 0)
		{
			cout << "Number not a POSITIVE integer! " << endl
				  << "Enter a POSITIVE integer: ";
			cin >> numArray[ count ];
		}
		count++;
	}
	clrscr();

   printHisto( numArray, count );
	getch();
	return 0;
}
//------------------------------------------------------------------------------
//void printHisto( int numArray [], int count )
//Purpose:   Prints a histogram based on user input.
//Inputs:	 NULL
//Outputs:   NULL
//------------------------------------------------------------------------
void printHisto( int numArray [], int count )
{
   for( int i = 0; i < count; i++ )
   {
      for( int j = 0; j < numArray[ i ]; j++ )
		{
         cout << "*";
      }
      cout << endl;
   }
}
//----------------------------------------------------------



please help, the 'exit' on the sentinel control works, but it goes crazy and prints a million *... help?

Is This A Good Question/Topic? 0
  • +

Replies To: sentinel control error

#2 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: sentinel control error

Posted 08 September 2011 - 09:54 PM

If you simply print the elements of the array, you see that it contains garbage values like 23487383, which become the upper limit for your inner loop in printHisto().

void printHisto( int numArray [], int count )
{

    for( int i = 0; i < count; i++ )
        cout<<numArray[i]<<endl;
    /*
    for( int i = 0; i < count; i++ )
    {
        for( int j = 0; j < numArray[ i ]; j++ )
        {
            cout << "*";
        }
        cout << endl;
    }
    */
}



So asterisks print while j < 23487383.

If you print the value of count in printHisto() it's certainly not the number of values the user enters at the command line.

What's happening is that count is incremented until it equals MAX; the second condition isn't correct.

numArray[ count ] != 'exit'



It's not semantically correct to compare an integer to a string. But, 'exit' is really a const char * so it's syntactically correct.

You can either read keyboard data as integers or strings. You could simply use -1 as an exit flag since you've specified that the user must enter positive integers.

do
    {
        cout << "Enter a POSITIVE integer, or type -1 to finish: ";
        cin >> numArray[ count ];

        if (numArray[ count ] < -1)
        {
            cout << "Number not a POSITIVE integer! " << endl
                 << "Enter a POSITIVE integer: ";
            cin >> numArray[ count ];
        }
        count++;
    }
    while( count < MAX && numArray[count-1] != -1 );



I'm pretty sure your histogram algorithm is off too.

This post has been edited by blackcompe: 08 September 2011 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: sentinel control error

Posted 08 September 2011 - 09:58 PM

First, you can't use 'single quotes' for strings. They are used for single characters. Use "double quotes".

Second, you can't store the string "exit" in an integer array. Either use negative numbers as the sentinel, or do something like this:
//add these to your list of includes
#include <string>
#include <sstream>

//now do this in your loop
string input;
cin >> input;
if(input == "exit") {
    break;
}
istringstream(input) >> numArray[count]; //create an istream (like cin) from input and extract an integer

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1