C++ calculator error.

Im currently new to C++ and im currently trying to make a basic calculator, but i seem to get some errors which i cant figure out, i would really appreciate some help :)

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

using namespace std;

int main()
{
	char method;


    int a;
    int b;
    int resultat;

{
     cout << "Enter a number \n";
    cin >> a;

    cout << "Enter second number \n";
    cin >> b;

    cout << "Enter operator +,-,*,/,%:";
    cin >> method
    
    cout << " udregningen er dette. \n";

    if(method =='-') resultat = a - b;
    
    if(method =='+') resultat = a + b;
    
    if(method =='*') resultat = a * b;
    
    if(method =='/') resultat = a / b;
    
    if(method ==('%') resultat = a % b;
    
    cout << "Resultat: " << resultat;
}




}while (true);


    return 0;
Last edited on
you defined a variable as a character. and called it op for representing the input character that will represent the operation.
for getting it from the user you didn't use the same name.
you wrote cin >> operator. you must use the same name you called it. ie cin >> op; and you forgot the semicolon.

and about the loop, if it's a do {} while() loop, you forgot the do up before the braces. and you closed the braces of the loop body twice. and didn't close the main function


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

using namespace std;

int main()
{
	char op;


    int a;
    int b;
    int result;

do
{
     cout << "Enter a number \n";
    cin >> a;

    cout << "Enter second number \n";
    cin >> b;

    cout << "Enter operator +,-,*,/:";
    cin >> op;

    if(op =='-') result = a - b;
    if(op =='+') result = a + b;
    if(op =='*') result = a * b;
    if(op =='/') result = a / b;
    cout << "Result: " << result;

}while (true);

    return 0;
}
Last edited on
when declaring many variables of the same kind you could declare them all in one phrases.
i.e.
 
int a, b, result;


and you should better make the program start every new period of the loop in a new line. or even leave 2 or 3 lines by editing the last line in the loop
 
cout<<"Result: "<<result<<endl<<endl<<endl;
and in conditions, a nonzero number is considered as a true condition. thus you could just wrote
 
while (1);

instead of
 
while (true);

Thanks for the help i'll try that out :)
Topic archived. No new replies allowed.