Program 12

Hello,

Now there is this strange exercise:

Write a program that takes an operation followed by two operands and outputs the result. For example:

+ 100 3.14
*4.5

Read the operation into a a string called operation and use an if-statement to figure out which operation the user wants, for example, if(operation=="+"). Read the operands into variables of type double. Implement this for operands called +, -, *, /, plus, minus, mul, and div with their obvious meanings.

Now I`m not sure if I fully understand the question, but I will do my best.

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
 #include<iostream>
#include<string>

int main()
{

    string operation;
    double a=0.0
    double b=0.0
    cout<< "Enter two numbers and select operation (+, -, * or /)";
    cin>>a>>b;
    cin>> + >> - >> * >> /;
    

   if  (operation == "+") { cout<< " << a + b << "; }

   else if (operation == "-") { cout<< " << a - b << "; }

   else if (operation == "*") { cout<< " << a * b << "; }

    else if (operation == "/") { cout<< " << a / b << "; }


}


When trying to compile though, getting error message.


 
  
listen to your compiler.
You're missing the std namespace from some of the calls.

e.g. line 7:
std::string operation;

You are missing semi-colons on lines 8 and 9.

And line 12 is just plain wrong in terms of syntax.

You're if statements are testing the value of 'operation' but you never actually set it to anything.

edit:

also you wrote:
Write a program that takes an operation followed by two operands


but in the code you are asking your user:
Enter two numbers and select operation


which is not the same.
Last edited on
Yes that`s true; I could also include std namespace in the header files. Here is the code improved:

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
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()
{

   string operation == + or - or * or /;       // Reads operations into a string
   double a=100;              // Reads operand a into double and sets default value 100 
   double b=3.14;             // Reads operand b into double and sets default value 3.14
   cout<<"Select  which operation you want (+, -, * or /) and press enter";
    

   if  ( "<< operation <<" == "+") { cout<< " << a + b << "; }

   else if ( "<< operation <<" == "-") { cout<< " << a - b << "; }

   else if ( "<< operation <<" == "*") { cout<< " << a * b << "; }

   else if ( "<< operation <<" == "/") { cout<< " << a / b << "; }


}


Does that look better?
Does that look better?
It looks like it does not compile. http://cpp.sh/452
Last edited on
Does that look better?

No.
As Chervil said it still does not compile.
Have you tried compiling it yourself and looking at the error message(s)?

And you are still not reading anything in from the user and assigning it to your 'operation' string.

Read "Introduction to strings" section from
http://www.cplusplus.com/doc/tutorial/variables/
to fix line 11.

and "cin and strings" section from
http://www.cplusplus.com/doc/tutorial/basic_io/

for user input.

once you've done that, you can address your if conditions :)
Last edited on
Then would be this then:

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
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()
{

   string operation;          // Reads operation into a string
   operation = "plus, minus, mul or div";
   double a=100;              // Reads operand a into double and sets default value 100 
   double b=3.14;             // Reads operand b into double and sets default value 3.14
   cout<<"Select  which operation you want (plus, minus, mul or div) and press enter";
   cin>> operation;
    

   if  ( "<< operation <<" == "plus") { cout<< " << a + b << "; }

   else if ( "<< operation <<" == "minus") { cout<< " << a - b << "; }

   else if ( "<< operation <<" == "mul") { cout<< " << a * b << "; }

   else if ( "<< operation <<" == "div") { cout<< " << a / b << "; }


}
"<< operation <<" is a character string. So is "plus"
I think it's fairly clear that these two strings are not the same. Hence the == test will always give false.

The idea is heading in the right direction, you just need to select the right two things to compare.
I would like to move on but here is the best I can do:

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
 #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;


int main()
{

   string operation;          // Reads operation into a string
   double a=0.0;              // Reads operand a into double and sets default value 0.00 
   double b=0.0;              // Reads operand b into double and sets default value 0.00
   cout<<"Select the numbers you want (a,b)";
   cin>>a>>b;
   string plus;                                 
   plus = "+";
   string minus;
   minus = "-";
   string mul;
   mul = "*";
   string div;
   div = "/"; 
   cout<<"Select which operation you want (plus, minus, mul or div) and press enter";
   cin>>plus>>minus>>mul>>div;                        // Reads operation
   
    

   if  ( "<< operation <<" == "+") { cout<< " << a + b << "; }

   else if ( "<< operation <<" == "-") { cout<< " << a - b << "; }

   else if ( "<< operation <<" == "*") { cout<< " << a * b << "; }

   else if ( "<< operation <<" == "/") { cout<< " << a / b << "; }


}

	



btw the program doesn`t work
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include<iostream>
#include<string>

int main()
{

    char operation; // Note only a single character is needed
    double a=0.0
    double b=0.0
    cout<< "Enter two numbers and select operation (+, -, * or /)";
    cin>>operation>>a>>b; // Note
    cin>> + >> - >> * >> /;
    

   if  (operation == '+') { cout<< "a + b = "<< a + b << std::endl; } // Note

   else if (operation == '-') { cout<< "a - b = "<< a - b << std::endl; } // Note

   else if (operation == '*') { cout<< "a * b = " << a * b << std::endl; } // Note

    else if (operation == '/') { cout<< "a / b = "  << a / b << std::endl; } // Note


}
I hope you're not trolling
Topic archived. No new replies allowed.