Ok So i have a university project . i must write a program that allows the user to select if he wants to add multiply subsctract divide or have the remainder of the number he /she choose . So the user must have this options and he/she decides the option he/she wants. This is what i did but i need some help as i get the error : Else without a previous if and i dont know what to do after checking my solutions.
#include <stdio.h>
// Begin main
int main()
{
// Define variables
int num1 = 0;
int num2 = 0;
int result = 0;
int a = num1 + num2;
int s = num1 - num2;
int m = num1 * num2;
int d = num1 % num2;
int r = 0;
int input = 0;
printf ("Do you want to add , subtract , multiply , divide or the remainder;");
if (input == a);
{ // Add num1 and num2 }
printf("Selection:a");
// 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);
// Add num1 and num2
result = num1 + num2;
// Print : The Result
printf("The result is %d\n", result);
}
else if (input == 's')
{ // Subtract num1 from num2 }
printf("Selection:s");
// 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);
// Subtract num1 and num2
result = num1 - num2;
// Print : The Result
printf("The result is %d\n", result);
}
else if (input == 'm')
{ // Multiply num1 and num2 }
printf("Selection:m");
// 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);
// Multiply num1 and num2
result = num1 * num2;
// Print : The Result
printf("The Result is %d\n", result);
}
else if (input == 'd')
{ // Devide num1 and num2 }
printf("Selection:d");
// 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);
// Devide num1 and num2
result = num1 / num2;
// Print : The Result
printf("The Result is %d\n", result);
}
else (input == 'r')
;{ // Show the remainder from num1 and num2 }
printf("Selection:r");
// 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);
//Remain num1 and num2
result = num1 % num2;
// Print : The Result
printf("The Result is %d\n", result);
(1) Please use code formatting. Edit your post and add [code] and [/code] to your code.
(2) This is barely C++. If your professor is teaching this as typical C++, please tell him or her that they're doing it wrong, and should just teach C instead (okay, obviously don't actually say that, but ah...)
Okay, that aside...
look at this line:
1 2
if (input == a);
{
and this
1 2
else (input == 'r')
;{ // Show the remainder from num1 and num2 }
That's not how an else statement works. The syntax should be:
1 2 3 4 5 6 7 8 9 10 11 12
if (something) // NO SEMI-COLON
{
}
elseif (something else)
{
}
else // NO SEMI-COLON or (condition)
{
}
Notice, there's no parentheses OR semi-colon (;) after the else condition, and there's no semi-colon after any if condition.
PS: You're asking for trouble by doing this:
1 2 3 4 5 6 7
int num1 = 0;
int num2 = 0;
int result = 0;
int a = num1 + num2;
int s = num1 - num2;
int m = num1 * num2;
int d = num1 % num2;
Just initialize a, s, m, d to 0. There's no point assigning them actual computational results yet, because num1 and num2 are just 0 at this point. In fact, if you refactored your code, you could get rid of all 4 of those last variables and just use result.
Maybe I could show how to do this in C++ later, but I gotta go.