Need assistance with If statements

I read the the welcome post and I will attempt to sound the least bit like a semi-literate boob as possible. I am doing an assignment for my class at school. Very first programming class, very first semester of college. I am trying hard to understand all of this information. The goal of the code is to have the user choose and operation they would like to use then two numbers that go along with the operation. If he wanted to multiply 3 * 7, they would simply type multiplication, then enter the two numbers. I am stuck on the 'if statements' that would exclude the other operations after one is chosen. Any assistance would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

  int main()
{
cout<< "Please choose the operation you would liket to use\n";
    string desired_operation;
    cin>>

    if (string plus is chosen, go to addition) {
        i
    }
    
    {string addition;
    
    
    
    double a;
    double b;
    cout<<"Enter first number:\n";
    cin >> a;
    cout <<"Enter the second number:\n";
    cin>> b;
    double result = a + b;
    cout<<"Result is"<<"  "<<result<<"\n";
        return 0;}
    
    { string subtraction;

   double a;
    double b;
    cout<<"Enter first number:\n";
    cin >> a;
    cout <<"Enter the second number:\n";
    cin>> b;
    double result = a - b;
    cout<<"Result is"<<"  "<<result<<"\n";
    return 0;}

    {string multiplication;
    
    double a;
    double b;
    cout<<"Enter first number:\n";
    cin >> a;
    cout <<"Enter the second number:\n";
    cin>> b;
    double result = a * b;
    cout<<"Result is"<<"  "<<result<<"\n";
    return 0;}

    {string division;
    
    double a;
    double b;
    cout<<"Enter first number:\n";
    cin >> a;
    cout <<"Enter the second number:\n";
    cin>> b;
    double result = a / b;
    cout<<"Result is"<<"  "<<result<<"\n";
        return 0; }
}
Last edited on
The way I would approach this is to read all of the data at the beginning then do the check and perform the operation:

- Read operation character
- Read first operand integer
- Read second operand integer
- If operation character is '+':
- Print result of addition
- Else if operation character '-':
- ...and so on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

int main()
{
    std::cout << "Please choose the operation you would like to use\n"
              << "add/subtract/multiply/divide (all lower case characters)\n" ;
    std::string desired_operation ;
    std::cin >> desired_operation ;

    std::cout << "Please enter the two numbers you would like to " << desired_operation << '\n' ;
    double a ;
    double b ;
    std::cin >> a >> b ;

    double result = 0 ;
    char symbol = '?' ;

    // as of now, we accept only lower case characters in desired_operation

    // if desired_operation is "add"
    // ie. if the value of ( desired_operation == "add" ) is true
    if( desired_operation == "add" )  /* then */  { result = a + b ; symbol = '+' ; }

    // otherwise (else), if desired_operation is "subtract"
    else if( desired_operation == "subtract" ) { result = a - b ; symbol = '-' ; }

    else if( desired_operation == "multiply" ) { result = a * b ; symbol = '*' ; }

    else if( desired_operation == "divide" ) { result = a / b ; symbol = '/' ; } // assume for now that b is not zero

    else // if desired_operation is not one of the above
    {
        std::cout << "the operation '" << desired_operation << "' is not supported\n" ;
        return 1 ; // by convention, returning a non-zero value from main() indicates failure
    }

    std::cout << '\n' << a << ' ' << symbol << ' ' << b << " == " << result << '\n' ;

    // return 0 ; is implied here. by convention, returning zero from main() indicates success
}
Last edited on
So it would look something like this?

int main()
{
cout<< "Please choose the operation you would like to use\n";
char desired_operation;
cin>> desired_operation;

{ double a;
double b;
cout<< "Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a + b;
cout<<"Result is"<<" "<<result<<"\n";
if (desired_operation == '+') {
cout<< result;

} else {
desired_operation |= '+';
}


{ double a;
double b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a - b;
cout<<"Result is"<<" "<<result<<"\n";
return 0;}

I decided to try and get the addition and subtraction part figured out before I added in multiplication and division.
Topic archived. No new replies allowed.