0

Here is the code: I am getting null value when i printf scanf from user for the letter grade. I am expecting John DOE recived a 95% which is an A.

The following is my ouput

 make -s
 ./main
Enter Student 1:
Last Name: doe
First Name: john
Grade: 95
john doe recieved a  95% on the midterm exam which is a (null) 
#include<stdio.h>
int main()
{
   int score;
  
  char firstname[20], lastname[20];
   char grade =0;


    printf("Enter Student 1:\n");    
  printf("Last Name: ");
    scanf("%s", lastname);
    printf("First Name: ");
    scanf("%s", firstname);
    printf("Grade: ");
    scanf("%d", &score);
  

      printf("%s %s recieved a  %d%% on the midterm exam which is a %s \n", firstname, lastname, score,grade);

  
   printf("Enter Student 2:\n");
    printf("Last Name: ");
    scanf("%s", lastname);
    printf("First Name: ");
    scanf("%s", firstname);
    printf("Grade: ");
    scanf("%d", &score);

   printf("%s %s recieved a  %d % on the midterm exam which is a %c. \n", firstname, lastname, score,grade);
  

   printf("Enter Student 3:\n"); 
    printf("Last Name: ");
    scanf("%s", lastname);
    printf("First Name: ");
    scanf("%s", firstname);
    printf("Grade: ");
    scanf("%d", &score);

   printf("%s %s recieved a  %d % on the midterm exam which is a %c. \n", firstname, lastname, score,grade);

   printf("Enter Student 4:\n"); 
    printf("Last Name: ");
    scanf("%s", lastname);
    printf("First Name: ");
    scanf("%s", firstname);
    printf("Grade: ");
    scanf("%d", &score);

   if(score>=93 && score<=100)
    grade = "A";

   else if(score>=90 && score<=92)
     grade = 'A';

     
   else if(score>=87 && score<=89)
      grade = 'B';
     
   else if(score>=83 && score<=86)
    grade = 'B';

   else if(score>=80 && score<=82)
     grade = 'B';


     else if(score>=77 && score<=79)
    grade = 'C+';

   else if(score>=73 && score<=76)
    grade = 'C';

   else if(score>=70 && score<=72)
     grade = 'C-';
     

     else if(score>=67 && score<=69)
     grade = 'D+';

   else if(score>=63 && score<=66)
     grade = 'D';

   else if(score>=60 && score<=62)
     grade = 'D-';

   else
     grade = 'F';

}



Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Welcome to Stack Overflow. Please read our page on [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example). – Beta Oct 27 '22 at 04:14
  • 2
    Turn up your compile warnings and pay attention to them. Once you fix all of these mismatches your program will probably operate correctly. https://godbolt.org/z/jv6hrYEjM To print an actual percent symbol using `printf` you need to use `%%` in the format string. – Retired Ninja Oct 27 '22 at 04:16
  • 1
    `grade` was initialized to zero, and you haven't set it when you use it in `printf`. And your printfs are not consistent. You should always use `%c`, and not `%s` for `grade`, since it is of type `char`. – William Pursell Oct 27 '22 at 04:16
  • 1
    Unfortunately, "D+" is not a character. A variable of type `char` cannot hold it. – David Schwartz Oct 27 '22 at 04:19
  • same with `"A"` which is a string, change that to `'A'`. But as mentioned, if you want to capture + and - you'll need a `char*` or a `char` array. – yano Oct 27 '22 at 04:23
  • `grade = "A";` What does your compiler tell you about this? One of the first lessons should be to read compiler warnings and don't just ignore them. – Gerhardh Oct 27 '22 at 06:14
  • 1
    You cannot use `scanf ("%s"...)` without providing the *field-width* modifier to protect your array bounds. See [man 3 scanf](https://man7.org/linux/man-pages/man3/sscanf.3.html). Failure to provide the field-width modifier makes your code exploitable by buffer-overrun. The use of `scanf ("%s", ...)` is no safer than using `gets()`, see: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin Nov 01 '22 at 05:16
  • You don't need the `&&` second predicates in the way you have defined them: they are guaranteed by the ones before. And also, you might have a keener that scored 101% and is awarded with an F. – Neil Nov 01 '22 at 07:49

1 Answers1

1

As I understand, the program will run top down, so it means that the grade should be modified before printf.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
dat_chau
  • 11
  • 1
  • That is an entirely valid point. There are two uses of grade, so it might be good to stick into a function, `const char *grade(score)`. – Neil Nov 01 '22 at 07:57