Thread: Stuck......help greatly appreciated.

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    54

    Stuck......help greatly appreciated.

    Hey Guys,

    So I have to write a program, using the switch statement, that allows a user to enter a percentage mark, and output the corresponding letter grade. This is what I've come up with so far. It compiles, but whenever a number is entered the program quits......

    Am i not returning properly?

    Any help would be greatly appreciated. Thanks in advance. Cheers.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int num_grade, tens;
    
      printf("Enter numerical grade:");
      scanf("%d", &num_grade);
    
      if ((num_grade < 0) || (num_grade > 100)) {
        printf("Grade must be positive AND less than 100.\n");
      }
      else {
        tens = num_grade / 10;
        switch (tens) {
          case 0: printf("The grade is F.\n");
                  break;
    
          case 1:printf("The grade is F.\n");
                  break;
    
          case 2:printf("The grade is F.\n");
                  break;
    
          case 3:printf("The grade is F.\n");
                  break;
    
          case 4:printf("The grade is F.\n");
                  break;
    
          case 5: printf("The grade is F.\n");
                  break;
    
          case 6: printf("The grade is D.\n");
                  break;
    
          case 7: printf("The grade is C.\n");
                  break;
    
          case 8: printf("The grade is B.\n");
                  break;
    
          case 9:  printf("The grade is A.\n");
                  break;
    
          case 10: printf("The grade is A.\n");
                  break;
    
        }
      }
    
      return 0;
    }
    Last edited by matt.s; 10-14-2009 at 05:20 PM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    It's probably not a problem with the code, but instead this. Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?

    Also, in switch statements you can do this:
    Code:
    switch( variable ) {
    case 1:
    case 2:
    case 3: // Will execute if variable is 1 or 2 or 3
      // code...
      break;
    
    // etc
    }
    Also, it might make more sense to do it with if-else trees rather than a switch, as you can specify grade ranges easily enough.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program may be quitting, just because it's done.

    Add this just before the return 0 line of code:

    Code:
    printf("\n\n\t\t\t     press enter when ready");
    tens = getchar();
    I use that for nearly all my programs because I have an old and a new compiler, and only the new one, holds the text (console), window open, after the program is done.

    The old one (Turbo C/C++), closes the window, immediately. If I don't hold the window open, I can't read the monitor before it's quit.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Hey thanks for the reply, i'll take a look into that!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome!

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Finally got it, yeah it wasn't ending correctly, or should I say I wasn't making it end the way I wanted :P Thanks everyone for their help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noobie question, all help greatly appreciated!
    By axehero in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2007, 09:47 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. Replies: 2
    Last Post: 05-10-2006, 10:43 PM
  4. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  5. Your help would be greatly appreciated
    By Sway2 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 07:55 AM