You are not logged in.
Hi,
I recently started learning C and am now trying to make a calculator from scratch. GCC gives me this error when compiling the script:
calculator.c: In function ‘main’:
calculator.c:16:10: warning: comparison between pointer and integer
16 | if (sym == "/") {
| ^~
calculator.c:19:10: warning: comparison between pointer and integer
19 | if (sym == "*") {
|
I don't see what the problem is. Here is the script. It isn't finished yet though:
#include <stdio.h>
int main() {
printf ("This is a calculator :)\n");
float a;
float b;
char sym;
float out;
printf ("What's the first number? ");
scanf ("%f", &a);
printf ("What's the second number? ");
scanf ("%f", &b);
printf ("Whats's the operation you want to do? ");
scanf ("%c", &sym);
if (sym == "/") {
out = a / b;
}
if (sym == "*") {
out = a * b;
}
}
The script also dies at the last prompt without any errors:
This is a calculator :)
What's the first number? 5
What's the second number? 10
Whats's the operation you want to do?
Help is appreciated.
Last edited by fire100265 (2021-02-21 10:45:49)
Offline
You can't compare string this way in C
if (sym == "*")
Try to use
strcmp()
and look for some good tutorial about strings in C
Offline
You do not need strcmp since you want to compare a single character. In that case use single quotes for the one character.
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Try to use
strcmp()
and look for some good tutorial about strings in C
Thank you for our reply. Would you mind giving me an example of how to use this in my script?
Last edited by fire100265 (2021-02-21 09:30:45)
Offline
You do not need strcmp since you want to compare a single character. In that case use single quotes for the one character.
Thank you for your reply as well. I tried single quotes but the script still dies at the last prompt. How can I fix this?
Last edited by fire100265 (2021-02-21 09:33:47)
Offline
Probably this:
https://stackoverflow.com/questions/524 … the-buffer
Last edited by progandy (2021-02-21 09:46:40)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Thank you! That was the problem! Input works now but answers are very off. Here is an example:
What's the first number? 5
What's the second number? 10
Whats's the operation you want to do? /
47
Here is the updated version of the script:
#include <stdio.h>
int main() {
printf ("This is a calculator :)\n");
float a;
float b;
char sym;
float out;
printf ("What's the first number? ");
scanf ("%f", &a);
printf ("What's the second number? ");
scanf ("%f", &b);
printf ("Whats's the operation you want to do? ");
scanf (" %c", &sym);
if (sym == '/') {
out = a / b;
};
if (sym == '*') {
out = a * b;
};
printf ("%d\n", out);
}
Why is this so? What exactly results in the calculations being so wrong?
Offline
Why %d in (use %f)
printf ("%d\n", out);
Try this
#include <stdio.h>
int main()
{
char Operator;
float num1, num2, result = 0;
printf("\n Please Enter an Operator (+, -, *, /) : ");
scanf("%c", &Operator);
printf("\n Please Enter the Values for two Operands: num1 and num2 : ");
scanf("%f%f", &num1, &num2);
if(Operator == '+')
{
printf("\n The result of %.2f + %.2f = %.2f", num1, num2, num1 + num2);
}
else if(Operator == '-')
{
printf("\n The result of %.2f - %.2f = %.2f", num1, num2, num1 - num2);
}
else if(Operator == '*')
{
printf("\n The result of %.2f * %.2f = %.2f", num1, num2, num1 * num2);
}
else if(Operator == '/')
{
printf("\n The result of %.2f / %.2f = %.2f", num1, num2, num1 / num2);
}
else
{
printf("\n Invalid Operator ");
}
return 0;
}
Offline
You have `%d' in the final printf call, where it should be `%f'. Consider to use warning flags like `-Wall' in the future to be told of such mistakes.
Also, I note you prefix only one of the scanf strings with a space. You probably want this for either all of them or none.
Offline
Thank you for your replies everyone. I replaced %d with %f and the script works as intended. Thank you! Sorry for asking but may I know the difference between %d, %f and %c?
Offline
Also, I note you prefix only one of the scanf strings with a space. You probably want this for either all of them or none.
That is not necessary, c is one of the few that do not skip whitespace:
... Most conversions discard initial white space characters (the exceptions are noted below) ...
...
c Matches a sequence of characters ... The usual skip of leading white space is suppressed.
@fire100265: https://www.tutorialspoint.com/c_standa … _scanf.htm
Last edited by progandy (2021-02-21 10:42:54)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Thank you for the quick answer! I will mark this thread as solved now.
Offline