Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Hey,
I'm new to programming in C, and i have been having a problem with a simple program that i recently coded. Xcode is giving me parse error message for an "if" line that i can almost swear is right. Check it out.

#include<stdio.h>
#include<math.h>
int a, b, d, e, f ;
char c;

int main (void)
{
printf("enter a number");
scanf("%i",&a);
printf("enter another number");
scanf("%i",&b);
Printf("this is the menu");
Printf("\n 1. *");
printf("\n 2. +");
printf("\n 3. -");
printf("\n 4. /");
printf("\n 5. %");
printf("\n 6. ^");
scanf("%char",&c);

if(c==*){
d=a*b;
}
if(c==+){
d=a+b;}

the error message is "parse error before ")" token. I am not sure what the error is , but i've tried eveything i could, can someone help me out? i have an exam soon and i want to be up to mark.
Thanks..
 
if(c==*){

and

if(c==+){

are your immediate problems.

Since you are trying to compare chars, you need to put '*' and '+' inside single quotes.

The last scanf() is going to need work too, you will want to look a little more closely at the function documentation.
 
It's years since I looked at C but surely you need to enclose the string literal in quotes?

Code:
		if(c==[color=red]"[/color]*[color=red]"[/color]){
		d=a*b;
		}
		if(c==[color=red]"[/color]+[color=red]"[/color]){
		d=a+b;}


(sorry can't quite remember if it's double or single quotes you need though)
 
I see three issues:

1. printf is spelled with a capitol p in some cases.

2. if (c==*) should be if (c == '*')

3. The main function is missing a closing curly bracket.
 
dcv said:
It's years since I looked at C but surely you need to enclose the string literal in quotes?

Code:
		if(c==[color=red]"[/color]*[color=red]"[/color]){
		d=a*b;
		}
		if(c==[color=red]"[/color]+[color=red]"[/color]){
		d=a+b;}


(sorry can't quite remember if it's double or single quotes you need though)

Quotation marks for strings of characters and apostrophes for single characters.
 
Oh, and... please use code tags. Not using them will result in frustration of every programmer here trying to figure out what you write in the code, since there is no identation outside code tags.
 
It's also helpful to point out which line it gave you the error in (copying and pasting the debugger messages can help too). That way we know where in your code to start looking.
 
iMeowbot said:
if(c==*){

and

if(c==+){

are your immediate problems.

Since you are trying to compare chars, you need to put '*' and '+' inside single quotes.

The last scanf() is going to need work too, you will want to look a little more closely at the function documentation.

And it is never too early to learn that are ignoring return values of functions at your own risk...
 
If you give a menu with numbers, why are you checking for the symbol? Wouldn't it make more sense to make c an int?

And then, by using a menu like that, I prefer to use a case statement.
 
And then, by using a menu like that, I prefer to use a case statement.
It's normally best for new programmers to develop their own style - not some else's. Unless it's generally accepted to do it the other way for a very good reason.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.