I need an example

This is a discussion on I need an example within the C++ Programming forums, part of the General Programming Boards category; Odd to have cin.ignore in the loop structure, I would of placed that before return 0, but I suppose it ...

  1. #16
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Odd to have cin.ignore in the loop structure, I would of placed that before return 0, but I suppose it depends on the OP and how he/she wants to implement the program
    I'm just trying to be a better person - My Name Is Earl

  2. #17
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Try running your program from the commandline, and see if the window still blinks.

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
    #inclide <iostream
    
    using namespace std;
    
    int main()
    {
        int a;
        int ( a == 159753 )
        do {
        cout<< "Password:";
        cin>> a;
        cin.ignore();
    } while ( a != 159753 )
        
        cout<< "Incorrect, try again.\n";
    }
    
    else if ( a == 159753 )
    
        cout<< "Correct, please proceed.";
    }
    OK I get an error on the line that says do.
    What is wrong there?

  4. #19
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    do while loops have to end with a semicolon:
    Code:
    } while ( a != 159753 );
    And your if needs an opening bracket.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you fix your indentation, the problem should be obvious.

    Edit: Never mind, there's more than one problem and the first one is before the broken indentation starts (though that doesn't help).

    Edit: Actually, I see two errors on the very first line. I'm surprised the compiler got as far as it did.
    Last edited by robatino; 03-26-2007 at 05:34 PM.

  6. #21
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    "expected `;' before "do""

    That is the error I get on the do line.
    what does that mean exactly?

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    It means that the compiler expected a semi-colon before the word "do" on that line. That means that the previous line has confused the compiler. It may be something small, like adding a semi-colon, or it may be something more complicated, like accidentally typing int when you meant if.

  8. #23
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
     #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        int ( a == 159753 );
    do{
        cout<< "Password:";
        cin>> a;
        cin.ignore();
    } 
    while ( a != 159753 );
        
        cout<< "Incorrect, try again.\n";
    }
    
    else if ( a == 159753 ) 
    {
    
        cout<< "Correct, please proceed.";
    }
    On line 19 (the one with else if) gets these errors

    expected unqualified-id before "else"
    expected `,' or `;' before "else"

    How can I fix it?

  9. #24
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Can someone patch up the program abocve for me?

    There seems to be a few things wrong with it.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For starters, the line
    Code:
      int ( a == 159753 );
    doesn't make sense. You don't seem to need it anyway, so get rid of it. After that, fix your indentation as I suggested earlier, and you will uncover other mistakes yourself.

  11. #26
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    It says
    expected primary-expresion before "eleS"
    expected `;' before "else"
    [line 23]expected `}' at end of input (times 2)

    What do all those mean?

  12. #27
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    You program won't exit the loop until a == 159753 so there is no point in checking what it is. You can't have an else if statement unless you have an if statement preceeding it. You don't want the '}' right after the while ( a != 159753 ); And main should return 0; although technically you don't need it. And just remove int ( a == 159753);.
    Don't quote me on that... ...seriously

  13. #28
    Registered User
    Join Date
    Mar 2007
    Posts
    14
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
    
        cout<< "Password:";
        cin>> a;
        cin.ignore(); 
        if ( a != 159753 ); {
           cout<< "Incorrect, try again.\n";
        }
        else {
             cout<< "Correct, please proceed.";
        }
        cin.get();
    }
    I changed it to this
    It got rid of all the errors except
    expected primary-expresion before "eleS"
    expected `;' before "else"

    what can i do to fix it?

  14. #29
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    if ( a != 159753 );
    Remove the semicolon at the end.
    Don't quote me on that... ...seriously

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21