DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Input Questions (http://www.daniweb.com/forums/thread181940.html)

slik33 Mar 17th, 2009 2:33 am
Input Questions
 
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?

Paul Thompson Mar 17th, 2009 5:47 am
Re: Input Questions
 
What is the error that you are getting? Or whats going wrong, how do you know it fails?

slik33 Mar 17th, 2009 1:08 pm
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.

Rashakil Mar 17th, 2009 1:17 pm
Re: Input Questions
 
you are inputting to an int; there's your problem.

kenji Mar 17th, 2009 1:20 pm
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.


All times are GMT -4. The time now is 9:27 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC