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

int menu( bool hidemenu,int *parr)
{
	int choice;
	using namespace std;
	cout << "****MENU****" <<endl<<endl;
	cout << "(1) Set Array Size" << endl;
	if(parr != 0)
		cout << "(2) Set Array Values" << endl;
	if(hidemenu==false)
	{
		cout << "(3) Add Array Values" << endl;
		cout << "(4) Average Values in Array" << endl;
		cout << "(5) Find Highest Value in Array" << endl;
		cout << "(6) Find Lowest Value in Array" << endl;
		cout << "(7) Print the Array" << endl;
		cout << "(9) Delete the Array" << endl;
		cout << "(q) Quit" << endl;
	}
	cin >> choice;
	if(!cin)
	{clear,ignor,badInput=True,blah blah.....}
//doesnt work b/c cant pass back badinput value
	return choice;

How could I get around this problem?

Recommended Answers

All 4 Replies

What is the error that you are getting? Or whats going wrong, how do you know it fails?

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.

you are inputting to an int; there's your problem.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.