Thread: Problem with ! function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Unhappy Problem with ! function

    Im having a problem with this function. I'm trying to make a simple program to test it out (Cause i just started learning C++ Yesterday) But it keeps messing up. Help!

    Heres the program

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int number;
        
        cout<<"Please enter in 5: \n"; 
        cin>> number;
        cin.ignore();
        
        if ( number == 5 ) {
             cout<<"Yay. You entered in 5\n";
             }
        if ! ( number == 5); {     //HERES THE PROBLEM
             cout<<"THAT IS NOT 5. YOU LOSE.\n"; 
             }
             cin.get();
    }

    I'm just a beginner, so im not good at trouble shooting.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the thing behind an if statement must be in parentheses. "!(number==5)" is not in parentheses.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Found The Problem

    Ok...i gotta be more careful with these ;

    if ( number != 5 ); was the old one,
    if ( number != 5 ) is the new one. THE ; RUINED IT.
    Alright. Fixed it. So...Yeah.

    Also, id doesn't have to be. Theres variations. I used this formula to compare:
    !=....Yeah. its different from the normal == but it is easier if you ask me. Less code by 2 of these "
    Still learning...so i may make more mistakes.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your code more properly might look like this:

    Code:
    if ( number == 5 ) {
        cout<<"Yay. You entered in 5\n";
    }
    else { 
        cout<<"THAT IS NOT 5. YOU LOSE.\n"; 
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Since you were playing with the negation operator, it may be worth pointg out that this also would have worked:

    Code:
    if (!(number == 5)) {
        cout << "THAT IS NOT 5. YOU LOSE.\n" ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM