If/else not working

This is a discussion on If/else not working within the C++ Programming forums, part of the General Programming Boards category; I have enter this into codeblocks but no matter what I change it always gives the answer as yes... Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Question If/else not working

    I have enter this into codeblocks but no matter what I change it always gives the answer as yes...

    Code:
    // My program which is based on a tutorial
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int ans;
    
        cout<<"Can you spell tea? Type 1 for yes or 0 for no.\n";
        cin>>ans;
        cin.ignore ();
    
    
        if (ans=1)
        {
            cout<<"you can!?";
        }
        else if (ans=0)
        {
            cout<<"You Can't!?";
        }
    
    
    return 0;
    }
    How do I fix this?

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    I don't use C++, but I think C is almost the same.

    Try == instead of =.

    a = 1 says to make a equal to 1
    a == 1 asks if a is equal to 1

    ** i just found the tutorial, and yes, this is how it's done

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Thanks, It works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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