change number to operator

I'm having hard time trying to change the line in 59. op will say 1,2,3,4. I want it to say +, -, *, /. I tried to add new function, int opType(int x). Create new line of if(x == 1) then return +;...

tho that failed since i cant change from int to char. Soo

I changed if to void opType then std::cout << "+" << std::endl; it work! but... it conflict because the (x) on line 59 has no define. Which left me in new loop again.

ps. I can only use what I learned, anything I can do to improved, pointer, error, type. please let me know.

advice is strongly recommend ha. thank in advance

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
63
64
65
66
67
68
  #include <iostream>

getANumber()
{
    std::cout << "Put your number here: " << std::endl;

        int value;

    std::cin >> value;

            return value;
}

getOperator()
{
    std::cout << "Pick a number: (1 = +, 2 = -, 3 = *, 4 = / " << std::endl;

        int op;

    std::cin >> op;

            return op;
}

storeNumber(int x, int op, int y)
{
    if(op == 1)
            return x + y;
    if(op == 2)
            return x - y;
    if(op == 3)
            return x * y;
    if(op == 4)
            return x / y;

            return x + y;
}

void getResult(int result)
{
    std::cout << "Your answer is " << result << std::endl;
}
int main()
{
        int value1 = getANumber();

    std::cout << "Your first number is: " << value1 << std::endl;
    std::cout << std::endl;

        int op = getOperator();

    std::cout << "So far: (" << value1 << ", " << op << std::endl;
    std::cout << std::endl;

        int value2 = getANumber();

    std::cout << "Your second number is: " << value2 << std::endl;
    std::cout << std::endl;
    std::cout << "Now...: (" << value1 << ", " << op << ", " << value2 << ")" << std::endl;

        int result = storeNumber(value1, op, value2);

    std::cout << std::endl;

    getResult(result);

            return 0;
}
Last edited on
the idea... you can use a string to store the table if you prefer. It just looks up what you want from what you have, you have 1,2,3,4 and you want the symbols. You can also do something clever with the ascii table, as I believe these ops are in the order you used there, but that is kinda hackish.

char tbl[] = {0,'+','-','*','/'};

cout << value1 << tbl[op] << endl;

hack version:

cout << value1 << char('+' + (op-1))
Last edited on
First, your functions need return types if not already void.

Add "int" before getANumber, getOperator, and storeNumber.

If you want to convert your arbitrary value into a character, then do something like:

1
2
3
4
5
6
/// Assumes op is 1, 2, 3, or 4
char convert_to_symbol(int op)
{
    char symbols[] = {'\0', '+', '-', '*', '/' };
    return symbols[op];
}
Last edited on
Thank for all the help, I cant use char and [], since its not part of session I'm on.

@Ganado, thanks I fixed the int part. I dont understand tho, the return types on line 3, 14, and 25 should be corrected due to return value; and same for other. Unless I am wrong?

@jonnin the cout << value1 << tbl where do I put and which will it refer to? Do I need to create new char inside int main? such as char tb, tho [], I don't know what it does.

But from all the suggest, It gave me some idea. It work but not as I intend. I got the answer such as 5 + 5 in the end, but the random addition information are added since it called twice.

Any idea of modify it without using the extra term which I haven't yet learned?

Thank again

ps. I stopped the adjusting on line 30, since keep going seem pointless as it doesn't get result as I intend. But its the idea I that got the part I wanted to work.

pss. I forget to say, this is my own personal testing. I already passed first quiz. I wanted to make this more convenient.



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
63
64
65
66
67
68
69
#include <iostream>

int getANumber()
{
    std::cout << "Put your number here: " << std::endl;

        int value;

    std::cin >> value;

            return value;
}

int getOperator()
{
    std::cout << "Pick a number: (1 = +, 2 = -, 3 = *, 4 = / " << std::endl;

        int op;

    std::cin >> op;

            return op;
}

int storeNumber(int x, int op, int y)
{

    if(op == 1)
            op = x + y;
        std::cout << x << op << "+" << y << std::endl;
            return x, op, y;
    if(op == 2)
            op = x - y;
    if(op == 3)
            op = x * y;
    if(op == 4)
            op = x / y;
}

void getResult(int result)
{
    std::cout << "Your answer is " << result << std::endl;
}
int main()
{
        int value1 = getANumber();

    std::cout << "Your first number is: " << value1 << std::endl;
    std::cout << std::endl;

        int op = getOperator();

    std::cout << "So far: (" << value1 << ", " << op << std::endl;
    std::cout << std::endl;

        int value2 = getANumber();

    std::cout << "Your second number is: " << value2 << std::endl;
    std::cout << std::endl;
    std::cout << "Now...: (" << value1 << ", " << op << ", " << value2 << ")" << std::endl;

        int result = storeNumber(value1, op, value2);

    std::cout << std::endl;

    getResult(result);

            return 0;
}
Last edited on
Topic archived. No new replies allowed.