Thread: errors troubling me

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    INDIA
    Posts
    13

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Some of your errors are as follows:
    1. 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.
    2. '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.
    3. 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:
    1. 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.

    2. Code:
      switch(f);
      This is incorrect. Use curly braces: {}.
    3. 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;
      }
    4. You have some curly braces where you shouldn't. If you indented your program you'd probably find out which ones these are.
    5. 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
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM