-
errors troubling me
hi!
could you please help me in correcting the error I got in this program?
#include <stdio.h>
void main()
{
int c , f ,s;
printf("Enter the class in which the student is classified 1 , 2 or 3 ");
scanf("%d",&c );
switch(c)
{
case '1':
printf("Enter the status of failed subjects (>3),(<3)");
scanf("%d",&f );
switch(f)
{
case '>3':
printf("NO GRACE MARKS");
break;
case '<3':
printf("Enter the number of failed subjects 1,2 or 3");
scanf("d",&s);
break; } }
switch(s)
{
case '1':
printf("The grace marks to be given is 5");
break;
case '2':
printf("The grace marks to be given is 10");
break;
case '3':
printf("The grace marks to be given is 15");
break;
break; }
}
case '2': {
printf("Enter the status of failed subjects (>2) or (<2)");
scanf("%d",&f);}
{
switch(f)
case'>2':
printf("NO GRACE MARKS");
break;
case'<2':
printf("Enter the number of failed subjects 1 or 2");
scanf("%d",&s);
break;}
switch(s)
{
case '1':
printf("Grace marks to be awarded is 4");
break;
case '2':
printf("Grace marks to be awarded is 8");
break;
}
{
case '3':
printf("Enter the status of failed subjects >1 or =1");
scanf("%d",&f);}
{
switch(f);
case '>1':
printf("NO GRACE MARKS");
break;}
{case '=1':
printf("Grace marks to be awarded is 5");
break;}
default:
printf("INVALID INPUT");
}
p.s-the errors are:
1.unexpected } in the last line.
2.declaration terminated incorrectly in line 38
-
Perhaps you should understand that the spirit of [code][/code] tags is to make your code readable to everyone else. It is not a hurdle for you to jump through by spraying the tags randomly through your post until the popup goes away.
I would also suggest you read this and then take a more measured approach to writing code, one which involves compiling and testing as you go.
You might think there is only one error, but I got into double figures way before the end of the post.
-
Some of your errors are as follows:
- You cannot use equality operators like > and < inside switch statements. case statements match only a single value. If you want to use < etc., use if-else statements.
- '0' is a character. If you read in an integer, you want to use 0 (without the single quotes). This goes for switch statements, if clauses, assignment, etc.
- void main() is non-standard. Use int main(). http://faq.cprogramming.com/cgi-bin/...&id=1043284376
Also, not really errors but they still need attention:
- You should probably be printing \n at the end of your printf() statements. Or at least a space, as you did for one printf(). Run it to find out why.
This is incorrect. Use curly braces: {}.- Your indentation is really bad. It's making your code hard to understand, and certainly hard to write and change. Use a consistent indentation style. Blocks (code inside {}) should be indented more than the code surrounding them, e.g.
Code:
#include <stdio.h>
int main() {
int x = 0;
if(x == 0) {
printf("zero\n");
}
else {
printf("non-zero\n");
}
return 0;
}
- You have some curly braces where you shouldn't. If you indented your program you'd probably find out which ones these are.
- There are several switch statements where you could have some default cases.
Finally, I recommend that you have a look at this switch statement tutorial. http://www.cprogramming.com/tutorial/c/lesson5.html