-
Help
i cant figure out what is wroung help me please!!!!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int op;
int num2;
printf ("In this calculator * = Multiply, + = Add, - = subtract and / means divied.\n");
printf ("Enter the first integer:")
scanf (" %d", &num1);
printf ("Enter operator:");
scanf (" %d", &op);
printf ("Enter the second integer:");
scanf (" %d", &num2);
int answer = num1 op num2;
printf ("%d", answer);
system("pause");
return 0;
}
-
You ask the user to enter an operator "+, -, *, /"
>>printf ("Enter operator:");
>>scanf (" %d", &op);
The value stored in op is the ASCII value 43, 45, 42, or 47 respectively
Then you use variable op as an integer value
>>int answer = num1 op num2;
In C, variable answer must be declared up front, not on the fly as in C++. Then your compiler should expect a ; after num1.
num1 op num2 is an error.
The compiler cannot recognize op as an operator, only as an integer.
Better planning would be for you to run op thru a switch structure.
Also, you left out 1 or 2 semicolons.
HTH,
-
ok i changed one thing and i got rid of one error the only eror left is on line 16 the eror is: parse error before `num2'
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
char op;
int num2;
printf ("In this calculator * = Multiply, + = Add, - = subtract and / means divied.\n");
printf ("Enter the first integer:");
scanf (" %d", &num1);
printf ("Enter operator:");
scanf (" %d", &op);
printf ("Enter the second integer:");
scanf (" %d", &num2);
int answer = num1 op num2;
printf ("%d", answer);
system("pause");
return 0;
}
-
> int answer = num1 op num2;
That's because you can't do it like that. C++ has no idea how to deal with a inputted symbol unless you tell it what do with it. Try something like this instead.
Code:
int answer;
if(op == '+')
answer = num1 + num2;
else if (op == '-')
answer = num1 - num2;
etc., etc....
-
try changing this:
scanf (" %d", &op);
to this:
scanf("%c", &op);
then after you have num1, num2, and op, do a switch based on op, or a series of if/else if statements based on value of op. the if/else if has been demonstrated by Govtcheese. The switch syntax could start something like:
switch(op)
{
case '+':
//etc.
The problem is the computer has no idea what + as a char variable means. It can only follow instructions that have been hard coded. Therefore you have to create a link between the variable input and the hardcoded protocol. That's what the if/else if or switch is for.
-
cant figure out how to print results im bad a c++ help please here's the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
char op;
int num2;
switch (op)
{
case '+':
num1 + num2;
case '-':
num1 - num2;
case '*':
num1 * num2;
case '/':
num1 / num2;
}
printf ("In this calculator * = Multiply, + = Add, - = subtract and / means divied.\n");
printf ("Enter the first integer:");
scanf (" %d", &num1);
printf ("Enter operator:");
scanf (" %d", &op);
printf ("Enter the second integer:");
scanf (" %d", &num2);
system("pause");
return 0;
}
-
Put the switch after the user input. Go through the switch and have the expression stored into another variable called result. Then, print out result.
-
so would i like do
int result = switch;
becuase i totaly clue less