Hi everyone, I was wondering if any of you could help me. I just tried writing my first simple C program,
It looks perfect to me, I've double checked everything in my book, but it just won't compile. It finds a problem in the calculations if-else statement, saying ""syntax error before '=' token". If anyone can maybe elaborate why this won't compile, I would greatly appreciate it.
/Ensures the program can be re-run multiple times without closing/
while (reRunAns == 'Y')
{
printf("\nPlease enter the amount of the Sale: ");
scanf(" %f", &saleAmount);
/A Do-While loop to ensure proper selection entry/
do
{ printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
while ((state != 'R') && (state != 'C'));
{
printf("\nInvalid Choice.. Please Try again\n");
printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
/*Section responsible for calculations, and section that generates errors*/
I have been using DevC++ for only a few days but I alread have found problems with compiling that I didn't think existed.
The best answer I can give is make everything simple and comment out all the code which is not needed and try to compile the code which is in question.
Popeye :-J
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just as your error message says, you have a syntax error before the '='. Don't put () around netAmount. That is;
if(state == 'R')
{netAmount = (saleAmount * RITAXRATE);}
else
{netAmount = (saleAmount * CONNTAXRATE);}
I don't know what book you are using but (netAmount) has never been valid C syntax. If your book really uses that syntax than I would recommand you find another book to learn C because you are going to have a lot of problems with syntax errors!
I'm not going to quibble about the () around saleAmount * RITAXRATE and saleAmount * CONNTAXRATE are not required and the {} also are not necessary, I'm assuming you will learn why later in your book.
See Ya
Butch
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for your suggestions Butch. I made the revisions you suggested to my program, however the compiler still finds an error. The parenthesis have been removed from the calculation section, and I have actually read on to realize you don't need { } in an if-else statement if it contains only one line under the statement. So I've removed those from the statement also. Instead of two syntax errors, I now only have one (syntax error before '=' token) highlighted on the first line of my if-else statement. This is what the new code looks like in its entirety:
/Ensures the program can be re-run multiple times without closing/
while (reRunAns == 'Y')
{
printf("\nPlease enter the amount of the Sale: ");
scanf(" %f", &saleAmount);
/A Do-While loop to ensure proper selection entry/
do
{ printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
while ((state != 'R') && (state != 'C'));
{
printf("\nInvalid Choice.. Please Try again\n");
printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
/*Section responsible for calculations*/
if (state == 'R')
netAmount = saleAmount * RITAXRATE; /Line highlighted for syntax error/
else
netAmount = saleAmount * CONNTAXRATE;
printf("The net amount of the sale is %.2f$\n", netAmount);
printf("Would you like to calculate another sale?");
scanf(" %c", reRunAns);
}
return 0;
}
I don't understand why it would still generate such an error.
Anymore light you can shed on this problem? I'd greatly appreciate it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If I understand correctly you are still getting the error 'syntax error before '=' token' for the line 'netAmount = saleAmount * RITAXRATE; /Line highlighted for syntax error/'
I don't see anything else wrong with your code but maybe I'm missing something. I don't have a compiler to test it.
This shouldn't make any difference, but I would like you to select 'Execute > Rebuild All'.
Good Luck,
Butch
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Butch is wrong about the parentheses around netAmount, that is perfectly allowable but entirely unnecessary.
You should always post the full build log. That way Butch would have seen which line the error messafe referred to. Your problem id the macro definitions:
define CONNTAXRATE = .06
define RITAXRATE = .07
They should be
define CONNTAXRATE .06
define RITAXRATE .07
You defined CONNTAXRATE as "= .06", which would cause
netAmount = saleAmount * CONNTAXRATE;
to expand to
netAmount = saleAmount * = .06 ;
which is the syntax error.
With respect to {} not being necessary for single statement blocks, this is true, but not recommended. I suggest you put the {} back. It makes the code easier to read, and less error prone during maintenance, when you might only have to put them back when you need to add statements to the block.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As usual, Clifford is correct. Since macros don't use '=' I didn't even see that you had used them there.
As pointed out, the compiler will tell you exactly what is wrong if you know how to read what it is telling you. When only the code is given you are relying on someones eyes to be as good at catching an error as the compiler. Not everyone is like Clifford and has a compiler built into his eyeballs!
As I said yesterday, I didn't have access to a compiler. My preferred way of do this would have been to copy the code and compile it myself. To often posts give incomplete information (like this one), and compiling it myself will give me the compiler log where I would have seen the problem.
I have never seen, nor read in any of my many books on C, the use of () around netAmount. Since I've learned something new today, I can take the rest of the day off! :-)
See Ya
Butch
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you both for helping me solve this problem. Clifford hit the nail on the head, changing my constant definitions solved the problem, they now read:
define RITAXRATE .07
define CONNTAXRATE .06
After changing these, it eliminated the syntax error on the '=' token however created another error regarding the if-else statement for some reason. The compiler said "syntax error before else statement". I tried using them with and without the { }, and still produced this error. So I changed this section over to a conditional operator, reading:
Thank you both for helping me solve this problem. Clifford hit the nail on the head, changing my constant definitions solved the problem, they now read:
define RITAXRATE .07
define CONNTAXRATE .06
After changing these, it eliminated the syntax error on the '=' token however created another error regarding the if-else statement for some reason. The compiler said "syntax error before else statement". I tried using them with and without the { }, and still produced this error. So I changed this section over to a conditional operator, reading:
> The compiler said "syntax error before else statement".
Compiles fine here in VC++, you must have different code that that which you posted. ALWAYS post complete Compile Log text, don't just 'describe' the problem. And always post the current code, even if you think you have not changed it materially.
VC++ does throw out a couple of warnings however:
"warning C4244: '=' : conversion from 'double' to 'float', possible loss of data"
You actually need:
define RITAXRATE .07f
define CONNTAXRATE .06f
to make the constants have type float rather than double.
Your ternary operator code is overcomplicated also, that is not how one usually uses it:
Hi everyone, I was wondering if any of you could help me. I just tried writing my first simple C program,
It looks perfect to me, I've double checked everything in my book, but it just won't compile. It finds a problem in the calculations if-else statement, saying ""syntax error before '=' token". If anyone can maybe elaborate why this won't compile, I would greatly appreciate it.
/Calculate sales tax/
include <stdio.h>
define CONNTAXRATE = .06
define RITAXRATE = .07
int main()
{
/Variable definitions/
float saleAmount, netAmount;
char state;
char reRunAns = 'Y';
/Ensures the program can be re-run multiple times without closing/
while (reRunAns == 'Y')
{
printf("\nPlease enter the amount of the Sale: ");
scanf(" %f", &saleAmount);
/A Do-While loop to ensure proper selection entry/
do
{ printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
while ((state != 'R') && (state != 'C'));
{
printf("\nInvalid Choice.. Please Try again\n");
printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
if (state == 'R')
{(netAmount) = (saleAmount * RITAXRATE);}
else
{(netAmount) = (saleAmount * CONNTAXRATE);}
printf("The net amount of the sale is %.2f$\n", netAmount);
printf("Would you like to calculate another sale?");
scanf(" %c", reRunAns);
}
return 0;
}
I have been using DevC++ for only a few days but I alread have found problems with compiling that I didn't think existed.
The best answer I can give is make everything simple and comment out all the code which is not needed and try to compile the code which is in question.
Popeye :-J
Hi everyone:
Just as your error message says, you have a syntax error before the '='. Don't put () around netAmount. That is;
if(state == 'R')
{netAmount = (saleAmount * RITAXRATE);}
else
{netAmount = (saleAmount * CONNTAXRATE);}
I don't know what book you are using but (netAmount) has never been valid C syntax. If your book really uses that syntax than I would recommand you find another book to learn C because you are going to have a lot of problems with syntax errors!
I'm not going to quibble about the () around saleAmount * RITAXRATE and saleAmount * CONNTAXRATE are not required and the {} also are not necessary, I'm assuming you will learn why later in your book.
See Ya
Butch
Thank you for your suggestions Butch. I made the revisions you suggested to my program, however the compiler still finds an error. The parenthesis have been removed from the calculation section, and I have actually read on to realize you don't need { } in an if-else statement if it contains only one line under the statement. So I've removed those from the statement also. Instead of two syntax errors, I now only have one (syntax error before '=' token) highlighted on the first line of my if-else statement. This is what the new code looks like in its entirety:
/Calculate sales tax/
include <stdio.h>
define CONNTAXRATE = .06
define RITAXRATE = .07
int main()
{
/Variable definitions/
float saleAmount, netAmount;
char state;
char reRunAns = 'Y';
/Ensures the program can be re-run multiple times without closing/
while (reRunAns == 'Y')
{
printf("\nPlease enter the amount of the Sale: ");
scanf(" %f", &saleAmount);
/A Do-While loop to ensure proper selection entry/
do
{ printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
while ((state != 'R') && (state != 'C'));
{
printf("\nInvalid Choice.. Please Try again\n");
printf("\nWas the sale made in Rhode Island or Connecticut (R/C)? ");
scanf(" %c", &state);
}
if (state == 'R')
netAmount = saleAmount * RITAXRATE; /Line highlighted for syntax error/
else
netAmount = saleAmount * CONNTAXRATE;
printf("The net amount of the sale is %.2f$\n", netAmount);
printf("Would you like to calculate another sale?");
scanf(" %c", reRunAns);
}
return 0;
}
I don't understand why it would still generate such an error.
Anymore light you can shed on this problem? I'd greatly appreciate it.
Hi Kevin:
If I understand correctly you are still getting the error 'syntax error before '=' token' for the line 'netAmount = saleAmount * RITAXRATE; /Line highlighted for syntax error/'
I don't see anything else wrong with your code but maybe I'm missing something. I don't have a compiler to test it.
This shouldn't make any difference, but I would like you to select 'Execute > Rebuild All'.
Good Luck,
Butch
Butch is wrong about the parentheses around netAmount, that is perfectly allowable but entirely unnecessary.
You should always post the full build log. That way Butch would have seen which line the error messafe referred to. Your problem id the macro definitions:
define CONNTAXRATE = .06
define RITAXRATE = .07
They should be
define CONNTAXRATE .06
define RITAXRATE .07
You defined CONNTAXRATE as "= .06", which would cause
netAmount = saleAmount * CONNTAXRATE;
to expand to
netAmount = saleAmount * = .06 ;
which is the syntax error.
With respect to {} not being necessary for single statement blocks, this is true, but not recommended. I suggest you put the {} back. It makes the code easier to read, and less error prone during maintenance, when you might only have to put them back when you need to add statements to the block.
Clifford
As usual, Clifford is correct. Since macros don't use '=' I didn't even see that you had used them there.
As pointed out, the compiler will tell you exactly what is wrong if you know how to read what it is telling you. When only the code is given you are relying on someones eyes to be as good at catching an error as the compiler. Not everyone is like Clifford and has a compiler built into his eyeballs!
As I said yesterday, I didn't have access to a compiler. My preferred way of do this would have been to copy the code and compile it myself. To often posts give incomplete information (like this one), and compiling it myself will give me the compiler log where I would have seen the problem.
I have never seen, nor read in any of my many books on C, the use of () around netAmount. Since I've learned something new today, I can take the rest of the day off! :-)
See Ya
Butch
Thank you both for helping me solve this problem. Clifford hit the nail on the head, changing my constant definitions solved the problem, they now read:
define RITAXRATE .07
define CONNTAXRATE .06
After changing these, it eliminated the syntax error on the '=' token however created another error regarding the if-else statement for some reason. The compiler said "syntax error before else statement". I tried using them with and without the { }, and still produced this error. So I changed this section over to a conditional operator, reading:
(state == 'R') ? (netAmount = saleAmount * RITAXRATE) : (netAmount = saleAmount * CONNTAXRATE);
The program now compiles with no errors. Thank you once again for your help.
Thank you both for helping me solve this problem. Clifford hit the nail on the head, changing my constant definitions solved the problem, they now read:
define RITAXRATE .07
define CONNTAXRATE .06
After changing these, it eliminated the syntax error on the '=' token however created another error regarding the if-else statement for some reason. The compiler said "syntax error before else statement". I tried using them with and without the { }, and still produced this error. So I changed this section over to a conditional operator, reading:
(state == 'R') ? (netAmount = saleAmount * RITAXRATE) : (netAmount = saleAmount * CONNTAXRATE);
The program now compiles with no errors. Thank you once again for your help.
> The compiler said "syntax error before else statement".
Compiles fine here in VC++, you must have different code that that which you posted. ALWAYS post complete Compile Log text, don't just 'describe' the problem. And always post the current code, even if you think you have not changed it materially.
VC++ does throw out a couple of warnings however:
"warning C4244: '=' : conversion from 'double' to 'float', possible loss of data"
You actually need:
define RITAXRATE .07f
define CONNTAXRATE .06f
to make the constants have type float rather than double.
Your ternary operator code is overcomplicated also, that is not how one usually uses it:
// conditional assignment
netAmount = (state == 'R') ? saleAmount * RITAXRATE : saleAmount * CONNTAXRATE ;
Clifford