Thread: Simple code making me nuts!!

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Unhappy Simple code making me nuts!!

    Code:
    #include<iostream>
    int main()
    {
        using namespace std;
        cout<<"\nEnter first num:";
        double a,b;
        cin>>a;
        cout<<"\nEnter second number:";
        cin>>b;
        char ch;
        cout<<"\nChoose one:\n+,-,*,/";
        cin>>ch;
            
        if(ch=='+')
                   {
                        cout<<endl<<a+b;
                        cin.get();
                   }
        if(ch=='-')
                   {
                        cout<<endl<<a-b;
                        cin.get();
                   }
        if(ch='*')
                  {
                        cout<<endl<<a*b;
                        cin.get();
                  }
        if(ch='/')
                  {
                        cout<<endl<<(a/b);
                        cin.get();
                  }
        cin.get();
        return 0;
    }
    Here are few outputs i got:

    First:
    Code:
    Enter First num:8
    
    Enter second number:6
    
    choose one:
    +,-,*,/
    +
    
    14
    48
    
    1.33333
    second:
    Code:
    Enter First num:78
    
    Enter second number:5
    
    choose one:
    +,-,*,/
    -
    
    73
    390
    
    15.6
    Third:
    Code:
    Enter First num:54
    
    Enter second number:25
    
    choose one:
    +,-,*,/
    *
    
    1350
    2.16
    Fourth:
    Code:
    Enter First num:7
    
    Enter second number:3
    
    choose one:
    +,-,*,/
    /
    
    21
    2.33333
    I included cin.get() in all the four if blocks bcoz the window was shutting down automatically.
    I am using Dev c++
    Thanks!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look carefully at:
    Code:
    if(ch='*')
    and
    Code:
    if(ch='/')
    Think of the difference between = and ==. Do you see the problem now?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    sorry

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    That a no? You're assigning values in the if() statement not comparing. It's just the latter two that are not working. You should also probably use else if ladders rather than four separate ifs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple piece of code
    By Furious5k in forum C++ Programming
    Replies: 10
    Last Post: 12-12-2008, 05:25 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM