Help with my program??

Discussion in 'C Programming' started by Mikaila, Sep 30, 2016.

  1. Mikaila

    Mikaila

    Joined:
    Sep 30, 2016
    Messages:
    1
    Likes Received:
    0
    Hi,

    I am having trouble with just one detail of my code and can't figure out how to fix it... any help would be greatly appreciated!


    In C:

    #include <stdio.h>
    #pragma warning (disable: 4996);

    int StudentID = 0;
    float Percentage = 0.0;
    char Grade = 'a';

    int main()
    {
    printf("Enter the student's id: ");
    scanf(" %d" , &StudentID);

    if (StudentID > 9999999999 || StudentID < 0)
    {
    printf("Invalid student number program terminating");
    return 1;
    }
    else
    {
    printf("Enter the student's percentage score: ");
    scanf(" %1.1f" , &Percentage);




    if (Percentage >= 0 && Percentage <= 100)

    {
    if (StudentID <= 100 && StudentID > 79.5)
    {
    Grade = 'A';
    }
    else if (StudentID <=79.5 && StudentID > 64.5)
    {
    Grade = 'B';
    }
    else if (StudentID <= 64.5 && StudentID > 55.5)
    {
    Grade = 'C';
    }
    else
    {
    Grade = 'F';
    }

    printf("The student with id %d" , StudentID);
    printf(" scored %1.1f" , Percentage);
    printf("%% on the exam\nThe student will recieve a %c" , Grade);
    }
    }

    return 0;
    }

    The output is (with the user input italicized):

    Enter the student's id: 123456
    Enter the student's percentage score: 89
    The student with id 123456 scored 0.0% on the exam
    The student will receive a F

    I don't understand why it is keeping the percentage as 0.0...
     
    Mikaila, Sep 30, 2016
    #1
    1. Advertisements

  2. Mikaila

    bmitchell2407

    Joined:
    Oct 10, 2016
    Messages:
    1
    Likes Received:
    1
    Hi,

    Your problem is in your if statement at the bottom of the code. You're assigning a grade (A, B, C, F) based on the StudentID when you need to check the Percentage instead.

    like:

    if ( Percentage <= 100 && Percentage > 79.5 )
    {
    Grade = 'A';
    }
    etc.

    Also, I think you're getting 0.0 for percentage because you're constraining the percentage in the scanf() line. Try and see if this works:

    scanf("%f", &Percentage);
     
    bmitchell2407, Oct 10, 2016
    #2
    Ian likes this.
    1. Advertisements

  3. Mikaila

    Thiefcom

    Joined:
    Jun 1, 2017
    Messages:
    40
    Likes Received:
    3
    Yea thats right
     
    Thiefcom, Jun 2, 2017
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    897
    Guest
    Apr 11, 2007
  2. M-SH
    Replies:
    1
    Views:
    631
  3. Rashnak
    Replies:
    1
    Views:
    479
    Rashnak
    Jun 22, 2016
  4. John Fri
    Replies:
    0
    Views:
    775
    John Fri
    Aug 21, 2016
  5. hakeem122
    Replies:
    1
    Views:
    389
    hakeem122
    Sep 27, 2016
  6. Ankit Tayal
    Replies:
    0
    Views:
    1,011
    Ankit Tayal
    Oct 1, 2016
  7. NewCureForAnger

    Google analytics doesn't work with google forms

    NewCureForAnger, Nov 3, 2016, in forum: HTML and CSS
    Replies:
    0
    Views:
    275
    NewCureForAnger
    Nov 3, 2016
  8. Baktusbror

    Help with a basic C# program?

    Baktusbror, Nov 8, 2016, in forum: C Programming
    Replies:
    0
    Views:
    239
    Baktusbror
    Nov 8, 2016
Loading...