Thread: Revaluing x and cin.get(); needed in if

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    22

    Revaluing x and cin.get(); needed in if

    If I were making a if thing say

    Code:
    include <iostream>
    using namespace std;
    int main()
    {
    cout<<"Please select 1 or 0\n";
    cin<< x;
    cin.ignore;
    if ( x == 0 ) {
    cout<<"Hi, you were wrong, please choose again\n";
    cin<< x;
    cin.ignore
    if ( x == 1 ) {
    cout<<"Yay, you got it right!\n";
    cin.get();
    }
    else if ( x == 1 ) {
    cout<<"Well done!\n";
    cin.get();
    }
    else {
    cout<<"I said 1 or 0, pick again!\n";
    cin<< x;
    cin.ignore;
    if ( x == 1 )
    cout<<"Yay!\n";
    }
    Do I need the cin.get(); and how do you revalue x? Don't worry about other mistakes I just made this up on the spot. Thanks

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    12

    reply

    idont know!!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Thanks... Anyone else have any idea?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Don't worry about other mistakes I just made this up on the spot.
    Few people will be inclined to help if the code isn't readable. Proper indentation, compilable code, and concise questions are always advised.

    In the meantime, there may some FAQ entries that are of interest.

    gg

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Short answer: No.
    Slightly longer answer: Fix your code, kthx.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Despite it all, your code I can read through no prob...

    It's your question that I find myself at a loss. Can you elaborate, or rephrase?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Ok, I tried fixing the code but something is wrong, it says

    8 C:\Documents and Settings\Chris\My Documents\My game\cout.cpp statement cannot resolve address of overloaded function

    Here's the code

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
    cout<<"Please select 1 or 0\n";
    cin>> x;
    cin.ignore;
    if ( x == 0 ) {
    cout<<"Hi, you were wrong, please choose again\n";
    cin>> x;
    cin.ignore;
    if ( x == 1 ) {
    cout<<"Yay, you got it right!\n";
    cin.get();
    }
    else if ( x == 1 ) {
    cout<<"Well done!\n";
    cin.get();
    }
    else {
    cout<<"I said 1 or 0, pick again!\n";
    cin>> x;
    cin.ignore;
    if ( x == 1 ) {
    cout<<"Yay!\n";
    }
    }
    Thanks.

    Edit: To try and rephrase my question is, If I put a cin>> x; and someone named x 6 but then later in the program I wondered if it was possible to change x to 5, how do I change x? Actually after reading that that's complete BS. I hope you understand it. Also I wondered that in a if file example:

    Code:
    if ( x == 1 ) {
    cout<<"Yay, you got it right!\n";
    cin.get();
    }
    Do I need the cin.get(); ?

    thanks
    Last edited by ballmonkey; 01-11-2005 at 02:49 PM.

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Unless you explicitly declare x as a constant (ie... const int x = 5) then you can change it whenever you like.

    The cin.get() seems to be used in this example just to pause the program until the user hits enter to continue. So whether you need it is up to you.

    Also the use of cin.ignore should be cin.ignore(). And I personally feel cin.ignore() is not a very reliable method of flushing the input stream. If there's more than one character in the input stream, cin.ignore() will only flush the next character (the default with no parenthesis is one char). If you want to be sure the input stream is clear, use while ( cin.get() != '\n' );.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Thanks alot! So I can redefine x at any moment? I was about to try it but an error comes up when I try this code

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
    cout<<"Please select 1 or 0\n";
    cin>> x;
    cin.ignore();
    if ( x == 0 ) {
    cout<<"Hi, you were wrong, please choose again\n";
    cin>> x;
    cin.ignore();
    if ( x == 1 ) {
    cout<<"Yay, you got it right!\n";
    cin.get();
    }
    else if ( x == 1 ) {
    cout<<"Well done!\n";
    cin.get();
    }
    else {
    cout<<"I said 1 or 0, pick again!\n";
    cin>> x;
    cin.ignore();
    if ( x == 1 ) {
    cout<<"Yay!\n";
    }
    }
    Is says:
    28 C:\Documents and Settings\Chris\My Documents\My game\cout.cpp syntax error at end of input
    and
    C:\Documents and Settings\Chris\My Documents\My game\cout.cpp In function `int main()':

    What does this mean and how can I get this to work?

    Thanks

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by ballmonkey
    Thanks alot! So I can redefine x at any moment? I was about to try it but an error comes up when I try this code



    Is says:
    28 C:\Documents and Settings\Chris\My Documents\My game\cout.cpp syntax error at end of input
    and
    C:\Documents and Settings\Chris\My Documents\My game\cout.cpp In function `int main()':

    What does this mean and how can I get this to work?

    Thanks
    It means you don't have enough closing '}'. You can get it to work by supplying a couple more of them. It might help if you used indentation, as was previously suggested. Here is a possibility:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
    
        cout<<"Please select 1 or 0\n";
        cin>> x;
        cin.ignore();
    
        if ( x == 0 ) {
            cout<<"Hi, you were wrong, please choose again\n";
            cin>> x;
            cin.ignore();
            if ( x == 1 ) {
                cout<<"Yay, you got it right!\n";
                cin.get();
            }
            else if ( x == 1 ) {
                cout<<"Well done!\n";
                cin.get();
            }
            else {
                cout<<"I said 1 or 0, pick again!\n";
                cin>> x;
                cin.ignore();
                if ( x == 1 ) {
                    cout<<"Yay!\n";
                }
            }
    // What's missing here?

  11. #11
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Your problem is that you're missing a couple of closing brackets. You really should indent your code so you can better spot where they are missing.

    You're missing two }'s in your code.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Oh, oops. I feel like a complete idiot... hee hee. Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  2. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  3. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  4. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM