Simple code making me nuts!!

This is a discussion on Simple code making me nuts!! within the C++ Programming forums, part of the General Programming Boards category; 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; ...

  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
    21,461
    Look carefully at:
    Code:
    if(ch='*')
    and
    Code:
    if(ch='/')
    Think of the difference between = and ==. Do you see the problem now?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,262
    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, 04: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, 11:51 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

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