Having some trouble with my program, the trouble though is in my scanf. I have troubleshooted it and I know that's the problem.
if I make my scanf with a single %d and a single variable, everything works fine, But when I make my scanf %d%d, and &a, &b it doesn't work. I need to enter a two digit number, such as (example 94). Then test the first digit in a switch and print a corresponding letter grade. So if the user types 94, the switch tests the first digit, which is 9, and outputs, Letter grade: A.
But what is happening is that after I enter the two digit 94 in the scanf and hit enter, the program doesn't do anything, but advances to a blank line, then if I type another number or letter and hit enter again, it finally prints the default "F", no matter what numbers I typed. I don't know why it's not executing the switch and printing the corresponding letter grade. As I said, if I delete the second %d, and the b variable, and I just enter a 9, or an 8, or a 7, it works as intended. So all I'm really doing is allowing the user to enter a second variable which really doesn't make any difference to the program, because it is stored in b and not used. I'm treating each number the user inputs as a seperate number, so I should be able to test just the a variable against the cases 9, 8, 7, 6, and the default. I've tried some different things with the scanf, such as %d,%d, and the way it is now, %d%d, but neither work. only scanf ("%d", a) ; has worked so far and correctly tests the a variable in the switch.
if I make my scanf with a single %d and a single variable, everything works fine, But when I make my scanf %d%d, and &a, &b it doesn't work. I need to enter a two digit number, such as (example 94). Then test the first digit in a switch and print a corresponding letter grade. So if the user types 94, the switch tests the first digit, which is 9, and outputs, Letter grade: A.
But what is happening is that after I enter the two digit 94 in the scanf and hit enter, the program doesn't do anything, but advances to a blank line, then if I type another number or letter and hit enter again, it finally prints the default "F", no matter what numbers I typed. I don't know why it's not executing the switch and printing the corresponding letter grade. As I said, if I delete the second %d, and the b variable, and I just enter a 9, or an 8, or a 7, it works as intended. So all I'm really doing is allowing the user to enter a second variable which really doesn't make any difference to the program, because it is stored in b and not used. I'm treating each number the user inputs as a seperate number, so I should be able to test just the a variable against the cases 9, 8, 7, 6, and the default. I've tried some different things with the scanf, such as %d,%d, and the way it is now, %d%d, but neither work. only scanf ("%d", a) ; has worked so far and correctly tests the a variable in the switch.
Code:
#include <stdio.h>
main ()
{
int a, b ;
printf ("Enter Numerical Grade:") ;
scanf ("%d%d", &a, &b) ;
printf ("Letter Grade: " ) ;
switch (a) {
case 9: printf ("A\n") ;
break;
case 8: printf ("B\n") ;
break;
case 7: printf ("C\n") ;
break;
case 6: printf ("D\n") ;
break;
default: printf ("F\n") ;
break;
}
return 0 ;
}