943,536 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 340
  • C++ RSS
Mar 17th, 2009
0

Input Questions

Expand Post »
I am working inside a function and am already passing back a value. Inside the function I am using cin to get an int from the user. If they want to quit they type q. The input buffer fails when this happens. I could use an if statement and then clear the buffer but I cant pass back another value. For example
C++ Syntax (Toggle Plain Text)
  1. int menu( bool hidemenu,int *parr)
  2. {
  3. int choice;
  4. using namespace std;
  5. cout << "****MENU****" <<endl<<endl;
  6. cout << "(1) Set Array Size" << endl;
  7. if(parr != 0)
  8. cout << "(2) Set Array Values" << endl;
  9. if(hidemenu==false)
  10. {
  11. cout << "(3) Add Array Values" << endl;
  12. cout << "(4) Average Values in Array" << endl;
  13. cout << "(5) Find Highest Value in Array" << endl;
  14. cout << "(6) Find Lowest Value in Array" << endl;
  15. cout << "(7) Print the Array" << endl;
  16. cout << "(9) Delete the Array" << endl;
  17. cout << "(q) Quit" << endl;
  18. }
  19. cin >> choice;
  20. if(!cin)
  21. {clear,ignor,badInput=True,blah blah.....}
  22. //doesnt work b/c cant pass back badinput value
  23. return choice;

How could I get around this problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slik33 is offline Offline
4 posts
since Feb 2009
Mar 17th, 2009
0

Re: Input Questions

What is the error that you are getting? Or whats going wrong, how do you know it fails?
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Mar 17th, 2009
0

Re: Input Questions

I just assume that because when I type in q it doesnt let me input anything anymore it just keeps repeating my code without stopping.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slik33 is offline Offline
4 posts
since Feb 2009
Mar 17th, 2009
0

Re: Input Questions

you are inputting to an int; there's your problem.
Reputation Points: 51
Solved Threads: 0
Banned
Rashakil is offline Offline
6 posts
since Jan 2006
Mar 17th, 2009
0

Re: Input Questions

The int choice is an int and as q is a char it sends the ascii value you should either make all your choices chars or make q a int like 0.

That may solve your problem.
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Child removal problem with Xerces
Next Thread in C++ Forum Timeline: Help auto adjust command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC