0

It should be accepting digit as scores. The program will continue to accept values until a negative number is input. (solved)

I just need to know how to display invalid if its anything besides numbers are input. I tried using else statement for displaying "invalid" but it doesn't work. I'm new to c programming.

#include <stdio.h>
#include <stdio.h>
    
int main()
{
    int numericScore=0,a=0,b=0,c=0,d=0,f=0;
      
    while(numericScore>-1) //while loop
    {
        printf("What is the score?\n");
        scanf("%d",&numericScore);
      
        if (numericScore >100)
            break;
        
        if (numericScore >=90 && numericScore <=100)
        {
            a++; //increment
            printf("grade is A \n" );
        } 
    
        else if (numericScore>=80 )
        {
            b++; //increment
            printf("grade is B \n");
        }
    
        else if (numericScore >=70)
        {
            c++; //increment
            printf("grade is C\n");
        }
    
        else if (numericScore >=60)
        { 
            d++; //increment
            printf("grade is D\n");
        }
    
        else if (numericScore>=0 && numericScore <60  )
        {
            f++; //increment
            printf("grade is F\n");
        }
    
        else if (numericScore <0)
            break;
    
        else
        {
            printf("invalid");
        }
          
    }
    printf("%d A's\n",a); // number of letter grade
    printf("%d B's\n",b);
    printf("%d C's\n",c);
    printf("%d D's\n",d);
    printf("%d F's\n",f);
      
    return 0;
}
daniel
  • 1
  • `%d` will only read an integer. You can't use that to input something else and check if it's a number. – Barmar Apr 12 '22 at 01:49
  • you can make use of the fact that scanf( ) has a return value that can be tested for success. If an integer is not entered, it will fail. following will help: `#include ` `int main()` `{` `int number; ` `printf("Enter a number:");` `if(scanf("%d",&number)){` `printf("\n %d is integer",number);` `/Write your other logic here for your program` `}` `else{` `printf("invalid");` `}` `return 0;` `}` Sorry not able to format the code properly in comment. :( – Sarabjeet Singh Apr 12 '22 at 08:14

0 Answers0