C++ ELSE PROBLEM

#include <stdio.h>
// Begin Main
main ()
{
// Define variables
int num1 = 0;
int num2 = 0;
int result = 0;
int choice = 0;

// Print the menu
printf("Calculator v2.0\n");
printf("------------\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Devide\n");
printf("5. Remainder\n");
printf("----------------\n");
printf("Choice:");

scanf("%d" , &choice);

if ((choice >= 1) && (choice <= 5))
// Print : Enter num1
printf("Enter the first number:");
// Read num1
scanf("%d" , &num1);
// Print : Enter num2
printf("Enter the second number:");
//Read num2
scanf("%d", &num2);

if (choice == 1)
{
// Add num1 and num2
result = num1 + num2;
}
else if (choice == 2)
{
// Subtract num1 and num2
result = num1 - num2;
}
else if (choice == 3)
{
// Multiply num1 and num2
result = num1 * num2;
}
else if (choice == 4 )
{
// Devide num1 and num2
result = num1 / num2;
}
else if (choice == 5)
{
// Remainder num1 and num2
result = num1 %num2;
}

//Print : The result
printf("The result is %d\n", result);


else <---- here the programs says to me else without previous if, idk why thought
{
//Print Error message
printf("Please enter a number from 1 to 5!\n");
}

return 0;


}// End Main






You have
1
2
3
4
5
6
7
8
9
10
11
if (thing)
{
    // ...
}

print_something();

else
{
    // ...
}

The "else" must immediately follow the end of the "if" block.
1
2
3
4
5
6
7
8
if (thing)
{
    // ...
}
else
{
    // ...
}

(Print either inside the if/else-blocks or after all them)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
if (choice == 5)
{
  // statements
} // end of if's block

printf( "Independent statement" );

else // What is before this else? An if?
{
  // statements
}

There is no if before the else. There is printf before the else.


PS. Do not shout.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (choice == 1)
 result = num1 + num2;
 printf("The result is %d\n", result);
else if (choice == 2)
 result = num1 - num2;
 printf("The result is %d\n", result);
else if (choice == 3)
 result = num1 * num2;
 printf("The result is %d\n", result);
else if (choice == 4 )
 result = num1 / num2;
 printf("The result is %d\n", result);
else if (choice == 5)
 result = num1 %num2; 
 printf("The result is %d\n", result);
else 
 printf("Please enter a number from 1 to 5!\n");
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
//...
//...

bool f=true ;//set flag


if (choice == 1)
{
// Add num1 and num2
result = num1 + num2;
}
else if (choice == 2)
{
// Subtract num1 and num2
result = num1 - num2;
}
else if (choice == 3)
{
// Multiply num1 and num2
result = num1 * num2;
}
else if (choice == 4 )
{
// Devide num1 and num2
result = num1 / num2;
}
else if (choice == 5)
{
// Remainder num1 and num2
result = num1 %num2;
}
else //<---- here the programs says to me else without previous if, idk why thought
{
//Print Error message
f = false;
printf("Please enter a number from 1 to 5!\n");
}

//Print : The result
if (f) printf("The result is %d\n", result);


return 0;
}


should've done it using the
switch-case
for large number of selections
Last edited on
Topic archived. No new replies allowed.