Help with ! (NOT)

This is a discussion on Help with ! (NOT) within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm trying to learn C++ with no past programming experience, and I have encountered a problem. You see, ...

  1. #1
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7

    Help with ! (NOT)

    Hi there, I'm trying to learn C++ with no past programming experience, and I have encountered a problem.

    You see, I made this program which works perfectly:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int password;
        
        cout<<"Please enter the password (made of 4 numbers) to learn the two rules of success: ";
        cin>> password;
        cin.ignore();
        if ( password == 1337) {
             cout<<"The two rules of success are:\n 1.Never tell everything you know.";
             }
             else if ( password >= 1300 || password <=1400 ) {
                  cout<<"Close enough, but this is not the password.";
                  }
                  else {
                       cout<<"Get out of my sight, this is not even close to the password!";
                       }
                       cin.get();
                       }
    The problem is that when I try to change the bolded line to:

    else if !( password >= 1300 || password <=1400 ) {

    the program says there is an error: syntax error before '!' token.

    What's wrong with my program

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    The ! goes inside, and you need more ()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7
    Wow that was fast, thanks a lot.

    While I'm at it, I don't completely understand what cin.ignore(); does, and attempts by others to explain it to me have failed.

    Can you please tell me what it does in layman's terms?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It may be a little too involved for you to understand at this point, but here goes.

    cin.ignore() is the same as cin.ignore(1) because 1 is the default value. It ignores one character of input. When a user types in some input and then hits Return, an invisible '\n' is added to the input. So, if a user types in a password and hits return, this is what the input really looks like:

    1234\n

    Then, when you do this:

    cin>>password;

    that causes input to be read in until a '\n' is encountered, and most importantly, input is read up to but not including the '\n'. The '\n' is said to remain in the 'input stream', which basically means that if you ask the user for input later in your program, that '\n' will be read in before anything else.

    Later in your program, you have this line:

    cin.get();

    which is there so your program's window doesn't disappear. If you executed your program without that statement, the program would execute, flash the output window, and close the ouput window in a blink of an eye. That statement says to get some input from the user, which means the program can't finish executing until the user enters some more input. That allows you to look over any preceding output your program produced, and when you are good an ready, you can hit any key causing your program to terminate and close the window.

    But, remember that '\n' that was left over? cin.get() will read that and not wait for the user to enter data, and the program will shut down a split second after it started--defeating the purpose of having the cin.get() there in the first place. That's where cin.ignore() comes in--it removes that '\n' that's left over. After using cin.ingore() to remove the left over '\n', there is no input for any subsequent input statements to read. Therefore, when the cin.get() line is executed, cin.get() can't find any input and has to wait for the user to enter something.

    If you stick to reading input using those arrrows >>, you can read in input as many times as you want without any problems, and you don't need to use cin.ignore(). But if you start off reading input with the arrows >>, and then switch to reading in input using a get() function, like cin.get(), then you have to use cin.ignore().

    How's Noobville these days? I moved to the suburbs, but just yesterday Salem sent me downtown to run some errands.
    Last edited by 7stud; 02-18-2005 at 07:10 PM.

  5. #5
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7
    Thanks for the answer, I understand now. I knew it had a link with the program closing itself, but now I understand exactly how it works.

    N00bville is a pretty dark and gl00my place. People have to work hours and hours, thinking for a second that an elusive concept is within their grasp, and then meeting the harsh reality: nope, they still don't get it.

    EDIT: Hurray, that elusive concept is now really in my grasp!

    My first real program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int addition ( int a, int b );
    
    int substraction ( int c, int d );
    
    int multiplication ( int e, int f);
    
    int division ( int g, int h);
    
    int main()
    {
        int i;
        
        int j = 1;
        
        while ( j == 1 ) {
        cout<<"Please choose what type of mathematical operation you want to do:\n 1.Addition\n 2.Substraction\n 3.Multiplication\n 4.Division(might be imprecise)\n Any other number.End program\n";
        cin>> i;
        cin.ignore();
        
        if ( i == 1 ) {//This is the addition function.
             int a;
             
             int b;
                      
             cout<<"Enter the two numbers to be added, pressing enter after each one: ";
             cin>> a >> b;
             cin.ignore();
             cout<<"The result is: "<< addition ( a, b )<<"\n";
             }
             else if ( i == 2 ) {//This is the substraction function.
             int c;
             
             int d;
             
             cout<<"Enter the two numbers to be substracted, pressing enter after each one: ";
             cin>> c >> d;
             cin.ignore();
             cout<<"The result is: "<< substraction (c, d )<<"\n";
             }
             else if ( i == 3 ) {//This is the multiplication section.
             int e;
             
             int f;
             
             cout<<"Enter the two numbers to be multiplicated, pressing enter after each one: ";
             cin>> e >> f;
             cin.ignore();
             cout<<"The result is: "<< multiplication ( e, f )<<"\n";
             }
             else if ( i == 4 ) {//This is the division section.
             int g;
             
             int h;
             
             cout<<"Enter the two numbers to be divided, pressing enter after each one: ";
             cin>> g >> h;
             cin.ignore();
             cout<<"The result is: "<< division ( g, h )<<"\n";
             }      
             int k;
                                              
             cout<<"Would you like to do another operation?  Press 1 if you would like to, otherwise press any number: ";
             cin>> k;
             cin.ignore();
             if ( k == 1) {
                  j = 1;
                  }
                  else {
                       j++;
                       }
             }
             cin.get();
             }
             
             int addition ( int a, int b )
             {
                 return a + b;
                 }
                 
                 int substraction ( int c, int d )
                 {
                     return c - d;
                     }
                     
                     int multiplication ( int e, int f )
                     {
                         return e * f;
                         }
                         
                         int division ( int g, int h )
                         {
                             return g / h;
                             }
    Me = proud.
    Last edited by Skarjak; 02-18-2005 at 08:38 PM.

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