6 Replies - 512 Views - Last Post: 26 May 2015 - 11:52 AM Rate Topic: -----

#1 thprogrammer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-April 15

How do I fix my grade program?

Posted 25 May 2015 - 08:56 PM

Hey guys, I am a beginner and I am trying to write a program which reads and classify grades.
However, it is not showing the error message if my input isn't a number. How can I fix it. Thank you!
Obs: Suggestions to make it better are really welcome.

int main() {

float grade;

printf("Enter your grade: ");
scanf("%f", &grade);

if (grade == 100){
printf("You scored a perfect grade!\n\n");
       }
            if(grade>= 90) {
            printf("You scored an A.\n\n");
            }
                else if (grade>= 80) {
                printf("You scored an B.\n\n");
                }
                      else if (grade>= 70) {
                      printf("You scored an C.\n\n");
                      }
                             else if (grade>= 60) {
                            printf("You scored an D.\n\n");
                            }
                                     else if (grade>= 0) {
                                    printf("You scored an F.\n\n");
                                    }
                                            else {
                                            printf("Enter a valid number.");
                                            }

return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I fix my grade program?

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5295
  • View blog
  • Posts: 14,133
  • Joined: 18-April 07

Re: How do I fix my grade program?

Posted 25 May 2015 - 09:40 PM

Strange, I run this and most certainly get your error message "Enter a valid number" when I type in a non number. What are you typing for input and what are you getting as a result?

:)
Was This Post Helpful? 0
  • +
  • -

#3 thprogrammer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-April 15

Re: How do I fix my grade program?

Posted 26 May 2015 - 09:25 AM

View PostMartyr2, on 25 May 2015 - 09:40 PM, said:

What are you typing for input and what are you getting as a result?


For example, if I type 'a' my output is 'You scored a F'.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5568
  • View blog
  • Posts: 17,215
  • Joined: 25-December 09

Re: How do I fix my grade program?

Posted 26 May 2015 - 11:12 AM

What you're seeing is the result of invoking undefined behavior. When you try to enter a non-numeric entry the scanf() call fails. When this function fails the variable remains unchanged and because you never initialized the variable the value of the variable is undefined.

If you want to detect this you should check the return value from the scanf() call and insure the returned value matches the number of variables specified in the scanf() call, int this case 1.

You also should find and use a reasonable indentation style. Here is your code reformatted using one style:

#include <stdio.h>

int main()
{

   float grade =-100;

   printf("Enter your grade: ");
   scanf("%f", &grade);

   if (grade == 100)
   {
      printf("You scored a perfect grade!\n\n");
   }
   if(grade>= 90)
   {
      printf("You scored an A.\n\n");
   }
   else if (grade>= 80)
   {
      printf("You scored an B.\n\n");
   }
   else if (grade>= 70)
   {
      printf("You scored an C.\n\n");
   }
   else if (grade>= 60)
   {
      printf("You scored an D.\n\n");
   }
   else if (grade>= 0)
   {
      printf("You scored an F.\n\n");
   }
   else
   {
      printf("Enter a valid number.");
   }

   return 0;
}



And in future be sure you include in your post any and all required #include statements.


Jim
Was This Post Helpful? 2
  • +
  • -

#5 vividexstance   User is offline

  • Tiocfaidh r l
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,877
  • Joined: 31-December 10

Re: How do I fix my grade program?

Posted 26 May 2015 - 11:32 AM

What if, somehow, the grade is more than 100?

Is there a reason you're using a float? Why not an int?

This post has been edited by vividexstance: 26 May 2015 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5568
  • View blog
  • Posts: 17,215
  • Joined: 25-December 09

Re: How do I fix my grade program?

Posted 26 May 2015 - 11:34 AM

Quote

What if, somehow, the grade is more than 100?

Good point, the next if statement should be print it's message.

Jim

This post has been edited by jimblumberg: 26 May 2015 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 vividexstance   User is offline

  • Tiocfaidh r l
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,877
  • Joined: 31-December 10

Re: How do I fix my grade program?

Posted 26 May 2015 - 11:52 AM

View Postjimblumberg, on 26 May 2015 - 02:34 PM, said:

Good point, the next if statement should be print it's message.

Jim

Or the OP could just make the first check a greater-than-or-equal to 100.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1