Thread: *shakes head* I need help

  1. #31
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32

    Same code new probs

    After the first question is asked "How many books have you read in the past year?" if I enter characters "faslkdfja" or any other combination of course, it throws me into an infinite loop... help!?

    Here it is:
    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
            	int books;
            	char book;
            	char input;
            	char choice;
            	while( choice != 'Y' )
            	{
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books;
    	        cin.ignore();
    	        cout<<"You read "<< books <<"?\n";
     
    		if ( books >= 90 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" a : 42' Tv Set!\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                       if ( input == 'a' )
                       {
                            cout<<"Wow! Congratulations! You've won a 42' Tv Set!\n";
                            cin.get();
                            }
                       else if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();     
                          }
                       else if ( input != 'a' || input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";             
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
        }
    		else if ( books >= 75 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore ();
                 if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                           else if ( input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                            cin.get();             
    		}
            cout<<"Hint: Press enter\n"; 
            cin.get();     
    		}
    		else if ( books >= 50 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                              cin.get();           
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 25 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.ignore();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                             cin.get();            
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();    
                          }
                          else if ( input != 'e' )
                          {
                               cout<<"Error. Please enter your only option next time, e\n";
                               cin.get();
    		}
    		else if ( books == 0 )
    		{
                 cout<<"Listed below are your options:\n";
    			cout<<"Sorry, Start reading and maybe you'll win something next time!\n";
    		}
            else if ( books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
                 }
    }
                 if ( books == book )
            {
                 cout<<"Oops! Please inter a positive integer!\n";
                 cin.get();
                 }
                 cout<<"Quit? Y/N\n";
                 cin>> choice;
                 if ( choice == 'Y' )
                 {
                      break;
    }
    }
    return 0;
    }
    Yay!!... not... Help please! lol
    The Pristine Angel lives on!

  2. #32
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Nice indentation.
    Code:
            	char choice;
            	while( choice != 'Y' )
    There's a possibility that choice will be 'Y', and the loop will never execute. To fix it:
    Code:
            	char choice = 0;
            	while( choice != 'Y' )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to flush the input buffer and clear it, etc. You're expecting a number, and if you give it a letter . . . you know.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #34
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Code:
    #include <iostream> 
    #include <cstdio> 
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(void)
    {
      int ch;
      char buf[BUFSIZ];
        
      cout <<"Flushing input" <<endl;
      
      while ((ch = cin.get()) != '\n' && ch != EOF);
      
      cout <<"Enter some text: ";
      cout.flush();
      
      if (cin.getline(buf, sizeof(buf)))
      {
        cout <<"You entered: " <<buf <<endl;
      }
    
      return 0;
    }
    So I need to add this to my code? excluding the couts? or.. how would I manage that?
    The Pristine Angel lives on!

  5. #35
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to use cout.flush(), I think, and maybe cout.clear() or something. I don't really know. (I know how to do it in C, but that won't help . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #36
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    bool InputCheck()
    {
      if (cin.fail()) {
        cerr << "Error: Invalid input." << endl;
        cin.clear();
        cin.ignore();
    
        return 0;
      }
    
      return 1;
    }
    Just call InputCheck() after trying to receive input.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #37
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Can you help me figure out where to place that within the above code?
    The Pristine Angel lives on!

  8. #38
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    #include <iostream>
     
    using namespace std;
    
    bool InputCheck();
    
    int main()
    {
            	int books;
            	char book;
            	char input;
            	char choice;
            	while( choice != 'Y' )
            	{
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books;
    	        InputCheck();
    	        cin.ignore();
    	        cout<<"You read "<< books <<"?\n";
     
    		if ( books >= 90 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" a : 42' Tv Set!\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
    	     InputCheck();
                 cin.ignore();
                       if ( input == 'a' )
                       {
                            cout<<"Wow! Congratulations! You've won a 42' Tv Set!\n";
                            cin.get();
                            }
                       else if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();     
                          }
                       else if ( input != 'a' || input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";             
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
        }
    		else if ( books >= 75 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
    	     InputCheck();
                 cin.ignore ();
                 if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                           else if ( input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                            cin.get();             
    		}
            cout<<"Hint: Press enter\n"; 
            cin.get();     
    		}
    		else if ( books >= 50 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
    	     InputCheck();
                 cin.ignore();
                 if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                              cin.get();           
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 25 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
    	     InputCheck();
                 cin.ignore();
                 if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.ignore();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                             cin.get();            
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
    	     InputCheck();
    	     cin.ignore();
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();    
                          }
                          else if ( input != 'e' )
                          {
                               cout<<"Error. Please enter your only option next time, e\n";
                               cin.get();
    		}
    		else if ( books == 0 )
    		{
                 cout<<"Listed below are your options:\n";
    			cout<<"Sorry, Start reading and maybe you'll win something next time!\n";
    		}
            else if ( books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
                 }
    }
                 if ( books == book )
            {
                 cout<<"Oops! Please inter a positive integer!\n";
                 cin.get();
                 }
                 cout<<"Quit? Y/N\n";
                 cin>> choice;
                 if ( choice == 'Y' )
                 {
                      break;
    }
    }
    
    bool InputCheck()
    {
      if (cin.fail()) {
        cerr << "Error: Invalid input." << endl;
        cin.clear();
    
        return 0;
      }
    
      return 1;
    }
    
    return 0;
    }
    I did modify the function from my own, but it should work.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #39
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I prefer this way to handle bad input. You have to #include <limits>. It is nicer in that it will ignore all bad data input by the user, unlike Dae's version which ignores 1 character only.
    Code:
    while (!(cin>> books))
    {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      // Prompt again here.
    }

  10. #40
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    I prefer this way to handle bad input. You have to #include <limits>. It is nicer in that it will ignore all bad data input by the user, unlike Dae's version which ignores 1 character only.
    Code:
    while (!(cin>> books))
    {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      // Prompt again here.
    }
    The version I use is like that actually, of course thanks to Prelude. But I changed it to simplify. I forgot though, would you have to extract all characters from the stream since they would still be there if the wrong type was entered? or would they be extracted like normal, and just failed to be assigned?

    Nevermind, I found the answer at gamedev, the bad input is still in the input stream. So yeah, you would most likely need to clear more than 1 character, haha. Thanks Daved.

    Just to explain, another alternative would be cin.ignore(256,'\n');. Which says it will keep extracting up to 256 character or a \n (newline character) is encountered. But it would be more reliable to use limits incase theres more than 256 characters - where limits would detect the definite amount of characters to extract from the stream. The loop will continue until valid input is given, when (cin>> books) resolves to true, and if it isnt valid input it clears the input. Thats pretty good, I think I saw it used a while back.. but didn't understand it so I read past it.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #41
    Registered User
    Join Date
    Oct 2005
    Posts
    1
    A w e s o m e !

Popular pages Recent additions subscribe to a feed