-5

In the following C program I need to use backspace when i input grade which in divided into two parts,otherwise the program is not working properly. Please help me how can i run the program normally without using space in input value. Means if the input value is 45 then it should take 4 as &f and 5 as &l. and in case of 100 it should take 10 as &f and 0 as &l. Thanks.

#include <stdio.h>

 int main(void)
 {
 int f,l;
 printf("Enter numerical grade:");
 scanf("%d %d",&f,&l);

 switch(f){
 case 0: printf("Letter grade: F");
 break;
 case 1: printf("Letter grade: F");
 break;
 case 2: printf("Letter grade: F");
 break;
 case 3: printf("Letter grade: F");
 break;
 case 4: printf("Letter grade: F");
 break;
 case 5: printf("Letter grade: F");
 break;
 case 6: printf("Letter grade: D");
 break;
 case 7: printf("Letter grade: C");
 break;
 case 8: printf("Letter grade: B");
 break;
 case 9: printf("Letter grade: A");
 break;
 case 10: printf("Letter grade: A");
 break;
 default :
     printf("Invalid grade\n" );
 break;


}

return 0;
}
JGut
  • 532
  • 3
  • 13
moniker
  • 23
  • 1
  • 8
  • 1
    Possible duplicate of [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – Yunnosch Apr 22 '18 at 17:06
  • 1
    You should scan one number and then use `/` and `%` to get the two values that you want. – Mars Apr 22 '18 at 17:07

2 Answers2

1

My suggestion is to get one number, scanf("%d", &f). And then afterwards get the values you want by division and mod, /, %.

To get what you are after you will have two more variables and the first one will contain all but the last digit (or 0 if f < 10) and the second will contain the last digit.

int var1 = f / 10;
int var2 = f % 10;
Mars
  • 4,677
  • 8
  • 43
  • 65
0

change your scanf function to this:

scanf("%d", &f);

Then add these lines in order to get correct values:

l = f % 10;
f /= 10;

And this will do the trick!

letsintegreat
  • 3,328
  • 4
  • 18
  • 39