Thread: Exercise: What is wrong with my code?

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    2

    Exercise: What is wrong with my code?

    Hi!

    For the sake of exercising, I wanted to create a simple cose which shall display a certain grade, based on the user-input score.
    But no matter which score I would let the user put in, it always would display "Grade C".
    Can you find my mistake?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
        int score;
        char grade1 = 'A';
        char grade2 = 'B';
        char grade3 = 'C';
        char grade4 = 'F';
    
        printf("What was the student's score? Please enter the score: ");
        scanf("%c", &score);
    
        if(score >= 90){
            printf("The student's grade is %c", grade1);
            }else if(score >=70 && score < 90){
                printf("The student's grade is %c", grade2);
                }else if(score >= 50 && score < 70 ){
                    printf("The student's grade is %c", grade3);
                    }else{
                        printf("The student's grade is %c and he has failed the test.", grade4);
                    }
    
    
        return 0;
    }
    Im grateful for any help.^^

    regards,
    Placebo

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    567
    You have two problems:
    1) The format specifier for an int in scanf() should be "%d", rather than "%c".
    2) You need a newline at the end of the line printed, with the exception of the printf() before the scanf().

    The grades could be an array, rather than individual variables.

    I also changed the indentation to make it a little more clear.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
       int score;
       char grade1 = 'A';
       char grade2 = 'B';
       char grade3 = 'C';
       char grade4 = 'F';
    
       printf("What was the student's score? Please enter the score: ");
       scanf("%d", &score);
    
       if(score >= 90) {
          printf("The student's grade is %c\n", grade1);
       } else if(score >=70 && score < 90){
          printf("The student's grade is %c\n", grade2);
       } else if(score >= 50 && score < 70 ){
          printf("The student's grade is %c\n", grade3);
       } else{
          printf("The student's grade is %c and he has failed the test.\n", grade4);
       }
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    2
    Hi!

    Thanks a lot for your help. Using c as the specifier for score was kind of an embarassing accident xD.
    So many chars in this code that I have somehow forgotten in my head that score is an integer xD
    Your hint with the need of using the escape sequence is helpful - I even did not know tbh that and why this is mendatory here.

    Again, thanks a lot for your input and also for your hint that this small exercise is also solveable by using an array (I will try that for myself out).

    Regards,
    Placebo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 10-04-2017, 05:39 PM
  2. Replies: 23
    Last Post: 10-01-2017, 03:55 PM
  3. matrix- exercise the use of arrays in the code.
    By CproG100 in forum C Programming
    Replies: 31
    Last Post: 08-28-2012, 07:40 AM
  4. K & R exercise - which code would you prefer?
    By kkk in forum C Programming
    Replies: 18
    Last Post: 08-17-2011, 06:42 PM
  5. an exercise gone wrong
    By GiraffeMan in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 03:09 AM

Tags for this Thread