Recommended Posts

ok I need to know why my while loop is not working.. (it ask for input untill -1 is entered but never does the rest of the loop)

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

//function prototype
void clear_screen (void);
int promptUser (const string& );

//main function
int main ()
{
    // local variables
   int userInput, holder;
   const int array_size= 9, poll_size=10;
   //setup arrays
   int salary [array_size] =  {200,300,400,500,600,700,800,900,1000};
   int Polldata [poll_size] = {0};

  // clear screen
  clear_screen();
   //main loop of program
   while (( userInput =  promptUser ( "Enter Salary or -1 to quit: ")) != -1 );{

             holder = userInput / 100;
             cout << holder << endl;

              ++Polldata[holder];
                                 }
   clear_screen();
   for ( int x =1; x <= 9; x++){
         cout << setw (4) << salary [x-1] << setw (10) << " " << setw (10) << Polldata[x] << endl;
             }
   return 0;
}

//clear screen function
void clear_screen()
{
    #if defined WIN32
    system ("cls");
    #elif defined UNIX
    system ("clear");
    #endif
    }

//check function for correct input type

int promptUser (const string& s)
{
   while (true) {
         int snumber;
         cout << s << flush;
         cin >> snumber;
         if (cin.fail()) {
                         cerr << "invalid input \n";
                         cin.clear();
                         cin.ignore();
                         }
         else
           return snumber;
         }
}

Link to post
Share on other sites
those damn stray semicolins :-)

my thought too..

:D

Link to post
Share on other sites
while (( userInput =  promptUser ( "Enter Salary or -1 to quit: ")) != -1 );    <-- stray semicolon

thank you.. I was messing with this all the way untill 3 am before just going to bead adn hopeing somesome would show me what is wrong..

now I can do my homework as I understand how to input things in the array..

your the king.. should be Linux and programing expert.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...