short code help

What I'm trying to do is create a program that lets a user enter two numbers (floating point) and a symbol (+, -, * or /) and then calculates the answer. The only trouble I'm running into (I believe) is storing a character as a "char" variable. Any help would be appreciated!

This is the code:
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
#include <iostream>

using namespace std;

double NumGet()
{
    cout << "Please enter a floating number: \n";
    double x;
    cin >> x;
    return x;
}

char CharGet()
{
    cout << "Please choose a symbol (+, -, * or /) \n";
    char y;
    cin >> y;
    return y;
}

void Solution(int z)
{
    cout << "The solution is: " << z << endl;
}

int main()
{
    double x, y;
    char z;
    x = NumGet();
    y = NumGet();
    z = CharGet();
    Solution(x 'z' y);
    return 0;
}
The problem is here Solution(x 'z' y);. That line doesn't even compile. What you need to do is choose the operation based on the character entered.

1
2
3
4
5
6
7
8
9
if (z == '+')
{
    //do addition
}
else if (z == '-')
{
    //do subtraction
}
...
ah, gotcha. I was hoping c++ would make it easier then that, but I guess it's not that difficult.

Thanks =)
Actually now I'm coming up with another error that's saying expected ')' before '+' and it's saying it for each operation.

This is the adjusted code:
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
int main()
{
    double x, y;
    char z;
    x = NumGet();
    y = NumGet();
    z = CharGet();

    if (z == '+')
    {
         Solution(x '+' y);
    }

    else if (z == '-')
    {
        Solution(x '-' y);
    }
    else if (z == '*')
    {
        Solution(x '*' y);
    }
    else if (z == '/')
    {
        Solution(x '/' y);
    }

    return 0;
}
Your code is not making sense?

You are trying to pass 2 functions to an void function which only accepts one argument?

I don't know why you are trying to do everything in seperate functions, if this is an assignment I can work out a reply for you using functions but.. you can do all of this without any excess fucntions.
I used a switch case but if/else works fine as well.
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>
using namespace std;
int main()
{
float a, b, total;
char op;
cout << "Enter a number: " << endl;
cin >> a;
cout << "Enter another number: " << endl;
cin >> b;
cout << "Choose an operation ( + , - , * , / ): ";
cin >> op;
switch (op)
{
case '+':
total = a + b;
cout << a << op << b << " = " <<total;
break;
case '-':
total = a - b;
cout << a << op << b << " = " <<total;
break;
case '*':
total = a * b;
cout << a << op << b << " = " <<total;
break;
case '/':
total = a/b;
cout << a << op << b << " = " <<total;
break;
default :
cout << "Invalid choice";
break;
}
return 0;
}
Last edited on
Actually now I'm coming up with another error that's saying expected ')' before '+' and it's saying it for each operation.

As before, this Solution(x '+' y); doesn't compile. Two doubles with a char in the middle is not a valid expression.

However, this is a valid expression Solution(x + y);. If you get rid of the single quotes in each of the similar statements (turning the middle char into an operator), then it will work. Also, be sure for Solution to take in a double instead of an int.
Oh, I see. I had been told it's best to split everything up that way, so I was practicing that, but I guess that's not something you should always follow.

It's working fine for me now, thanks for the help guys =) and nice catch with the "solution" there shacktar, I don't know why I had int.
Last edited on
Topic archived. No new replies allowed.