C++ operand programming homework

My question is

Write a program that has the user enter two integers and then has the user enter a math operand to do the mathematic operation on the two inputted integers. The operands are ‘+’, ‘-‘, ‘*’, ‘/’, and ‘%’, any other character entered for the math operand should produce an error message of some kind. If the operand is correct, then the mathematical output should be displayed along with the correct formula. For example, if 4 and 6 and + are entered, output should be something like the following: 4 + 6 = 10. To receive full credit you must use a “switch” statement in your program. Program should also continue to loop until the user chooses ‘N’ for No in some kind of loop.

Here is my program

#include<iostream>
using namespace std;

int main()
{
int IntegerOne, IntegerTwo, Answer,Operand;
cout << "Enter equation to be performed --> ";
cin >> IntegerOne,IntegerTwo,Operand;
if (Operand =='+')
{
Answer=IntegerOne+IntegerTwo;
cout << Answer;
}
if (Operand == '-')
{
Answer=IntegerOne-IntegerTwo;
cout << Answer;
}
if (Operand == '*')
{
Answer=IntegerOne*IntegerTwo;
cout << Answer;
}
if (Operand == '/')
{
Answer=IntegerOne/IntegerTwo
cout << Answer;
}
if (Operand == '%')
{
Answer=IntegerOne%IntegerTwo;
cout << Answer;
}

}

not sure what is wrong.
Last edited on
1
2
3
4
int IntegerOne, IntegerTwo, Answer,Operand;
char Operand;
cin >> IntegerOne,IntegerTwo,Operand;
cin >> IntegerOne >> Operand >> IntegerTwo;


Bonus for megacredit: do it without single if or switch :)
Is this right?

#include<iostream>
using namespace std;

int main()
{
int IntegerOne, IntegerTwo, Answer;
char Operand;
cout << "Enter equation to be performed --> ";
cin >> IntegerOne >> Operand>> IntegerTwo;
switch (Operand)
{
case '+': Answer = IntegerOne + IntegerTwo;
cout << Answer << endl;
break;
case '-': Answer = IntegerOne - IntegerTwo;
cout << Answer << endl;
break;
case '*': Answer = IntegerOne * IntegerTwo;
cout << Answer << endl;
break;
case '/': Answer = IntegerOne / IntegerTwo;
cout << Answer << endl;
break;
case '%': Answer = IntegerOne % IntegerTwo;
cout << Answer << endl;
}
}


how do i do without if or switch?????
Last edited on
Is this right?
Does it work? If so, you are cool.

how do i do without if or switch??
Using popular idiom of functional programming.
I have no idea what popular idiom is but i will be looking into it. Thanks again!
trw
i almost forgot that i need to have loop till character N is entered. This is what i have but it won't end loop.

#include<iostream>

using namespace std;



int main()

{

int IntegerOne, IntegerTwo, Answer;

char Operand,No = 'N';

cout << "Enter equation to be performed --> ";

cin >> IntegerOne >> Operand>> IntegerTwo;

while (Answer != No)
{

switch (Operand)

{

case '+': Answer = IntegerOne + IntegerTwo;

cout << Answer << endl;

break;

case '-': Answer = IntegerOne - IntegerTwo;

cout << Answer << endl;

break;

case '*': Answer = IntegerOne * IntegerTwo;

cout << Answer << endl;

break;

case '/': Answer = IntegerOne / IntegerTwo;

cout << Answer << endl;

break;

case '%': Answer = IntegerOne % IntegerTwo;

cout << Answer << endl;
break;

}
}
1
2
Answer != No
Operand != No
loop will still not stop and N will not stop loop
You do not take another input inside your loop. So Operand will never change.
Copy input line before the end of your loop
#include<iostream>
using namespace std;

int main()

{

int IntegerOne, IntegerTwo, Answer;
char Operand,No = 'N';

do
{
cout << "Enter equation to be performed --> ";
cin >> IntegerOne >> Operand>> IntegerTwo;

switch (Operand)
{
case '+': Answer = IntegerOne + IntegerTwo;
cout << Answer << endl;
break;
case '-': Answer = IntegerOne - IntegerTwo;
cout << Answer << endl;
break;
case '*': Answer = IntegerOne * IntegerTwo;
cout << Answer << endl;
break;
case '/': Answer = IntegerOne / IntegerTwo;
cout << Answer << endl;
break;
case '%': Answer = IntegerOne % IntegerTwo;
cout << Answer << endl;
break;
}
}
while (Operand != No);
}


loop now works but when N is entered it will continuously loop non stop! and how do i put my code in the format on this forum?
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
#include<iostream>


int main()
{
    while (true) {
        char op;
        std::cout << "Enter equation to be performed --> ";
        std::cin >> op;
        if (op == 'N')
            break;
        std::cin.unget();
        int left_int, right_int, result;
        std::cin >> left_int >> op>> right_int;
        switch (op)
        {
          case '+':
            result = left_int + right_int;
            break;
          case '-':
            result = left_int - right_int;
            break;
          case '*':
            result = left_int * right_int;
            break;
          case '/':
            result = left_int / right_int;
            break;
          case '%':
            result = left_int % right_int;
            break;
          default:
            std::cout << "Unsuppoted operand: " << op;
            break;
        }
        std::cout << result << std::endl;
    }
}

Code tags looks like <> on the format tab to the right.
That doesnt work
What exactly does not work? this code is valid C++, which should solve your problem. I thested it and it Does work:
Enter equation to be performed --> 232 / 5
46
Enter equation to be performed --> N

Process returned 0 (0x0)   execution time : 9.847 s
Press any key to continue.
http://ideone.com/StwNZM
i found out what i was doing wrong. thank you!
Bonus (without ifs or switch):
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
#include <functional>
#include <iostream>
#include <unordered_map>


int main()
{
    std::unordered_map<char, std::function<int (int, int)>> operations {
        {'+', std::plus<int>()},       {'-', std::minus<int>()},
        {'*', std::multiplies<int>()}, {'/', std::divides<int>()},
        {'%', std::modulus<int>()}
    };
    while (true) {
        char op;
        std::cout << "Enter equation to be performed --> ";
        std::cin >> op;
        if (op == 'N')
            break;
        std::cin.unget();
        int left_int, right_int;
        std::cin >> left_int >> op>> right_int;
        try {
            std::cout << operations.at(op)(left_int, right_int) << std::endl;
        } catch(std::out_of_range& ex) {
            std::cout << "Unsupported operation" << std::endl;
        }
    }
}
Topic archived. No new replies allowed.