Thread: What's wrong?

  1. #1
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27

    Angry What's wrong?

    Why won't this code work? It only will put out good job! Please explain why it won't put out Nope. I just started coding.

    #include <iostream.h>
    int main()
    {
    int a=6;
    cout<<"What is 3+3: ";
    cin>>a;
    if(a=6)
    {
    cout<<"Good job! ";
    }
    else if(a>6)
    {
    cout<<"Nope. ";
    }
    else if(a<6)
    {
    cout<<"Nope. ";
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The equality operator in C++ is ==. = just assigns something. So right now you're assigning 6 to a and seeing if a is true (which it is; anything non-zero is true). You need to use ==.

    if (a == 6)

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use code tags and more formatting... here's an example and a way to use the 'else' part of the if-else if-else statement:
    Code:
    #include <iostream.h>
    int main()
    {
       int a;  // no need to initialize
    
       cout<<"What is 3+3: ";
       cin>>a;
    
       if(a>6)
          cout<<"Nope. too high";
       else if(a<6)
          cout<<"Nope. too low";
       else
          cout<<"Good job! ";
    
       return 0;
    }
    or this is also good:
    Code:
    #include <iostream.h>
    int main()
    {
       int a;  // no need to initialize
    
       cout<<"What is 3+3: ";
       cin>>a;
       
       if(a!=6)
          cout<<"Nope.";
       else
          cout<<"Good Job!";
    
       return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM