expected primary-expression before 'else'

i am trying to make a simpel calculator but it wont work, i have written the code. but on line 34 and 38 the error Message comes up; "[i]expected primary-expression before 'else'" and "expected ';' before 'else'"... what shall i do to solve it??? please help im cind of new to this, and im 13 years old so...
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
#include <conio.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    int Answer;
    
    
    int numberOne;
    
    printf("Pleas enter first number: ");
    scanf("%d", &numberOne);
    
    
    float calculation;
    
    printf("Pleas enter calculation: ");
    scanf("%d", &calculation);
    
    
    int numberTwo;
    printf("Pleas enter number two: ");
    scanf("%d", &numberTwo);
    
    
    
    if (calculation = '*')
        Answer = numberOne * numberTwo;
        printf("%d times %d is %d\n", numberOne, numberTwo, Answer);
                   
    else if (calculation = '+')
        Answer = numberOne + numberTwo;
        printf("%d plus %d is %d\n", numberOne, numberTwo, Answer);
                     
    else (calculation = '-')
        Answer = numberOne - numberTwo;
        printf("%d minus %d is %d\n", numberOne, numberTwo, Answer);   
        
    
    getch();
    return EXIT_SUCCESS;
}





Thank you werry much for answers :)<3
Love
On line 34 and 38 you are assigning values to calculation instead of comparing them. Change = to == ( = is assignment operator while == is a comparison operator)

Multiple statements for if else if and else must be surrounded by curly braces {}
1
2
3
4
5
6
7
8
9
10
if(something)
  {
     do_x();
     do_y();
  }  
else if(anything)
  {
     do_z();
    do_x();
  }



You need to declare calculation as char calculation; because you need to get a character and line 21 must be scanf("%c", &calculation); // %c indicates that you need to get a single character

You are missing if keyword on line 38 -> else if (calculation == '-')

EDIT:
you'll need fflush(stdin); on line 17

You can use cin and cout for input/output: http://www.cplusplus.com/doc/tutorial/basic_io/



Last edited on
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
nt main(int argc, char *argv[])
{
    
    int Answer, numberOne, numberTwo;
	char calculation;
    

    printf("Pleas enter first number: ");
    cin >>numberOne;
    
     printf("Pleas enter number two: ");
    cin >> numberTwo;
    
    printf("Pleas enter calculation: ");
    cin >> calculation;
    
    
	if (calculation == '*'){
        Answer = numberOne * numberTwo;
		cout << "Result:" << Answer << endl;
	}

    else if (calculation == '+'){
        Answer = numberOne + numberTwo;
        cout << "Result:" << Answer << endl;
	}
                     
    else if (calculation == '-'){
        Answer = numberOne - numberTwo;
        cout << "Result:" << Answer << endl;   
	}
    
    return 0;
}
Last edited on
Thanks for reply, this helped alot :)<3
Love
@matsern97
It does not matter. good luck.
Topic archived. No new replies allowed.