Results 1 to 8 of 8

Thread: If problems in C++

  1. #1
    Senior Member Tech^salvager's Avatar
    Join Date
    Dec 2002
    Location
    Portland, TX
    Posts
    803

    If problems in C++

    My program compiles fine, it is that It will still put all the awnsers down though the if statments seem to work. Does anybody here know what I'm doing wrong?

    Code:
    #include <iostream>
    
    //Version 0.0.2 of caluclator program
    //Made by Justin R. Barrera
    
    using namespace std;
    
    int main()
    {
        int d=1;
        while (d != 0)    //Loop
          {
          int c=0;
          int b=0;
          int m=0;
    
          cout << "Please input a number ";
          cin >> c;
          cout << "Please input a number ";
          cin >> b;
          {
          cin >> m;
          if  (m == 1)
          cout << "The product is ";
          cout << c * b << endl;
          if (m == 2)
          cout << "The quotient is ";
          cout << c / b << endl;
          if (m == 3)
          cout << "The Difference is ";
          cout << c - b << endl;
          if (m == 4)
          cout << "The sum is ";
          cout << c + b << endl << endl;
          }    
          cout << "To exit enter 0" << endl;
          cin >> d;
          }
          return 0;
    }
    Thanks Tech^

  2. #2
    Senior Member Tech^salvager's Avatar
    Join Date
    Dec 2002
    Location
    Portland, TX
    Posts
    803
    Well I messed around more now it works like I want it to, but I still would like to know why it did what it did before. Heres the new code
    Code:
    #include <iostream>
    
    //Version 0.0.3 of caluclator program
    //Made by Justin R. Barrera
    
    using namespace std;
    
    int main()
    {
        int d=1;
        while (d != 0)    //Loop
          {
          int c=0;
          int b=0;
          int m=0;
    
          cout << "Please input a number ";
          cin >> c;
          cout << "Please input a number ";
          cin >> b;
          {
             cin >> m;
             if (m == 1)
             cout << "The product is " << c * b << endl;      
             if (m == 2)
             cout << "The quotient is " << c / b << endl;
             if (m == 3)
             cout << "The Difference is " << c - b << endl;
             if (m == 4)
             cout << "The sum is " << c + b << endl;
          }    
          cout << "To exit enter 0" << endl;
          cin >> d;
          }
          return 0;
    }

  3. #3
    Senior Member ScaryBinary's Avatar
    Join Date
    Mar 2000
    Location
    Temporary Internet Files
    Posts
    741
    You need to put curly braces around all the statements you want to execute in your "if" block, otherwise only the first statement after the "if" gets run.

    In your original code, you broke your output into two statements. If you wanted both statements to be run when your "if" condition is met, you'd have to code it like:
    Code:
    if  (m == 1) {
          cout << "The product is ";
          cout << c * b << endl;
    }
    Since you didn't have the curly braces, only the first "cout" was run if m==1, but the second "cout" was always run. This is a C thing.

    In your second attempt, you put the output all in the same statement, so it worked.

    I always put curly braces around my if blocks, even if they're only one line. It tends to avoid future confusion! Hence I would code your second attempt like:
    Code:
    if (m == 1) {
             cout << "The product is " << c * b << endl;
    }
    even though what you have there will work fine. In the future, if you do have to add another statement inside your if block, you won't forget to add the curly braces if they're already there!

  4. #4
    Senior Member Tech^salvager's Avatar
    Join Date
    Dec 2002
    Location
    Portland, TX
    Posts
    803
    Scarybinary how would I use
    if
    else if
    else if
    else
    ?
    I tried that first but it didn't work so i did all ifs

  5. #5
    Senior Member ScaryBinary's Avatar
    Join Date
    Mar 2000
    Location
    Temporary Internet Files
    Posts
    741
    You'd do it like:
    Code:
    cin >> m;
    if (m == 1)
    {
      /* Compute and display product. */
      cout << "The product is " << c * b << endl;
    }
    else if (m == 2)
    {
      /* Compute and display quotient. */
      cout << "The quotient is " << c / b << endl;
    }
    else if (m == 3)
    {
      /* Compute and display difference. */
      cout << "The Difference is " << c - b << endl;
    }
    else if (m == 4)
    {
      /* Compute and display sum. */
      cout << "The sum is " << c + b << endl;
    }
    else
    {
      /* User entered invalid input.  Print error. */
      cout << "Invalid input.  Try again.";
    }

  6. #6
    Ultimate Member
    Join Date
    Oct 2002
    Location
    Jersey, UK
    Posts
    1,850
    why not use switch/case?

    Nicer syntax

    Code:
    switch(m){
        case 1:
           cout<<"You entered 1"<<endl;
        break;
        case 2:
            cout<<"You entered 2"<<endl;
        break;
        default:
            cout<<"You didnt enter 1 or 2"<<endl;
    }
    Last edited by cwin; 09-18-2004 at 05:55 AM.

  7. #7
    Member
    Join Date
    Aug 2002
    Posts
    208
    I agree, switch statements for a program like this is definitely the more elegant way to go.

  8. #8
    Senior Member Tech^salvager's Avatar
    Join Date
    Dec 2002
    Location
    Portland, TX
    Posts
    803
    Originally posted by klatty
    I agree, switch statements for a program like this is definitely the more elegant way to go.
    yep
    Code:
    #include <iostream>
    
    //Version 0.0.32 of caluclator program
    //Made by Justin R. Barrera
    
    using namespace std;
    
    int main()
    {
        int restart=1;
        while (restart != 0)    //Loop
          {
          float c=0, b=0;
          int m=0;
    
          cout << "Please type in a number then press enter " << endl;
          cin >> c;
          cout << "Please type in a number then press enter " << endl;
          cin >> b;
          cout << "Choose a mathmatical operation" << endl;
          cout << "1=Multiplication" << endl;
          cout << "2=Division" << endl;
          cout << "3=Subtraction" << endl;
          cout << "4=Addition" << endl;
          cin >> m;
             switch(m)
             {
                 case 1:
                 cout << "The product is " << c * b << endl;
                 break;
                 
                 case 2:
                 cout << "The quotient is " << c / b << endl;
                 break;
                 
                 case 3:
                 cout << "The Difference is " << c - b << endl;
                 break;
                 
                 case 4:
                 cout << "The sum is " << c + b << endl;
                 break;
    
                 default:
                 cout << "HAHA don't input that" << endl;
                 break;
             }             
          cout << "To exit enter 0" << endl;
          cin >> restart;
          }
          return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •