You are not logged in.

#1 2021-02-21 09:06:02

fire100265
Member
Registered: 2020-10-05
Posts: 95

[SOLVED] GCC gives me errors on the C script I wrote

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

#2 2021-02-21 09:24:16

GeorgeJP
Member
From: Czech Republic
Registered: 2020-01-28
Posts: 71

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#3 2021-02-21 09:27:45

progandy
Member
Registered: 2012-05-17
Posts: 4,278

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#4 2021-02-21 09:28:56

fire100265
Member
Registered: 2020-10-05
Posts: 95

Re: [SOLVED] GCC gives me errors on the C script I wrote

GeorgeJP wrote:

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

#5 2021-02-21 09:32:23

fire100265
Member
Registered: 2020-10-05
Posts: 95

Re: [SOLVED] GCC gives me errors on the C script I wrote

progandy wrote:

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

#6 2021-02-21 09:46:14

progandy
Member
Registered: 2012-05-17
Posts: 4,278

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#7 2021-02-21 09:57:10

fire100265
Member
Registered: 2020-10-05
Posts: 95

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#8 2021-02-21 10:13:32

GeorgeJP
Member
From: Czech Republic
Registered: 2020-01-28
Posts: 71

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#9 2021-02-21 10:15:51

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#10 2021-02-21 10:40:09

fire100265
Member
Registered: 2020-10-05
Posts: 95

Re: [SOLVED] GCC gives me errors on the C script I wrote

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

#11 2021-02-21 10:41:57

progandy
Member
Registered: 2012-05-17
Posts: 4,278

Re: [SOLVED] GCC gives me errors on the C script I wrote

respiranto wrote:

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:

man scanf wrote:

... 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

#12 2021-02-21 10:45:34

fire100265
Member
Registered: 2020-10-05
Posts: 95

Re: [SOLVED] GCC gives me errors on the C script I wrote

Thank you for the quick answer! I will mark this thread as solved now.

Offline

Board footer

Powered by FluxBB