Thread: C Programming If statements Help (Newbie sorry)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    4

    C Programming If statements Help (Newbie sorry)

    How would you create a program in the language 'C' that ask for user's Letter grade in five classes with input "A" "B" "C" ETC. With A=4 quality points B =3, C=2, D=1, F=0 and then calculating the user's GPA. Then also he would like us to output statements such as if the gpa > 3.5 it should say "you are on the dean's list"
    if gpa is < 2.0 you are on academic probation
    if gpa is == 4 "Excellent"
    if gpa is ==3 "Above average"
    if gpa is ==2"below average"
    ***It also is noted that the instructor would like us to you if statements or switch statements as this chapter is focused on them. This is the code I have so far but it only is able to calculate the GPA. It does not output the statements for specific GPAs it is like it ignores the entire bottom half of my code. I am not looking for anyone to do my work, but more for suggestions on maybe what to research or what I should tweak in my code. The examples in the book are simple If statement examples, unfortunately.


    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    char grade_one;
    char grade_two;
    char grade_three;
    char grade_four;
    char grade_five;
    float gpa;
    float grade_1,grade_2,grade_3,grade_4,grade_5;
    printf("Please enter grade one:\n");
    scanf("%c", &grade_one);
    printf("Please enter grade two:\n");
    scanf(" %c", &grade_two);
    printf("Please enter grade three:\n");
    scanf(" %c", &grade_three);
    printf("Please enter grade four:\n");
    scanf(" %c", &grade_four);
    printf("Please enter grade five:\n");
    scanf(" %c", & grade_five);
    if (grade_one == 'A')
    {
    grade_1 = 4.0;
    }
    else if (grade_one == 'B')
    {
    grade_1 = 3.0;
    }
    else if (grade_one == 'C')
    {
    grade_1 = 2.0;
    }
    else if (grade_one == 'D')
    {
    grade_1 = 1.0;
    }
    else if (grade_one == 'F')
    {
    grade_1 = 0.0;
    }
    if (grade_two == 'A')
    {
    grade_2 = 4.0;
    }
    else if (grade_two == 'B')
    {
    grade_2 = 3.0;
    }
    else if (grade_two == 'C')
    {
    grade_2 = 2.0;
    }
    else if (grade_two == 'D')
    {
    grade_2 = 1.0;
    }
    else if (grade_two == 'F')
    {
    grade_2 = 0.0;
    }
    if (grade_three == 'A')
    {
    grade_3 = 4.0;
    }
    else if (grade_three == 'B')
    {
    grade_3 = 3.0;
    
    }
    else if (grade_three == 'C')
    {
    grade_3 = 2.0;
    }
    else if (grade_three == 'D')
    {
    grade_3 = 1.0;
    }
    else if (grade_three == 'F')
    {
    grade_3 = 0.0;
    }
    if (grade_four == 'A')
    {
    grade_4 = 4.0;
    }
    else if (grade_four == 'B')
    {
    grade_4 = 3.0;
    }
    else if (grade_four == 'C')
    {
    grade_4 = 2.0;
    }
    else if (grade_four == 'D')
    {
    grade_4 = 1.0;
    }
    else if (grade_four == 'F')
    {
    grade_4 = 0.0;
    }
    if (grade_five == 'A')
    {
    grade_5 = 4.0;
    }
    else if (grade_five == 'B')
    {
    grade_5 = 3.0;
    }
    else if (grade_five == 'C')
    {
    grade_5 = 2.0;
    }
    else if (grade_five == 'D')
    {
    grade_5 = 1.0;
    }
    else if (grade_five == 'F')
    {
    grade_5 = 0.0;
    }
    else
    {
    printf("Please enter grade one");
    }
    gpa = (grade_1 + grade_2 + grade_3 + grade_4 + grade_5) / 5;
    printf("Your GPA is %.2f\n", gpa);
    if (gpa > 3.5)
    {
    printf("You are on the dean's list");
    }
    else if (gpa < 2.0)
    {
    printf("You are on academic probation");
    }
    else if (gpa == 4)
    {
    printf("Excellent");
    }
    else if (gpa == 3)
    {
    printf("Above Average");
    }
    else if (gpa == 2)
    {
    printf("Average");
    }
    else if (gpa == 1)
    {
    printf("Below Average");
    }
    else if (gpa == 0)
    {
    printf("Poor");
    }
    return 0;
    }
    
    

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Using "==" with floating point numbers is often a bad idea.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please paste formatted code in the future; or, you will likely be ignored.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        char grade_one;
        char grade_two;
        char grade_three;
        char grade_four;
        char grade_five;
        float gpa;
        float grade_1,grade_2,grade_3,grade_4,grade_5;
        printf("Please enter grade one:\n");
        scanf("%c", &grade_one);
        printf("Please enter grade two:\n");
        scanf(" %c", &grade_two);
        printf("Please enter grade three:\n");
        scanf(" %c", &grade_three);
        printf("Please enter grade four:\n");
        scanf(" %c", &grade_four);
        printf("Please enter grade five:\n");
        scanf(" %c", & grade_five);
        if (grade_one == 'A')
        {
            grade_1 = 4.0;
        }
        else if (grade_one == 'B')
        {
            grade_1 = 3.0;
        }
        else if (grade_one == 'C')
        {
            grade_1 = 2.0;
        }
        else if (grade_one == 'D')
        {
            grade_1 = 1.0;
        }
        else if (grade_one == 'F')
        {
            grade_1 = 0.0;
        }
        if (grade_two == 'A')
        {
            grade_2 = 4.0;
        }
        else if (grade_two == 'B')
        {
            grade_2 = 3.0;
        }
        else if (grade_two == 'C')
        {
            grade_2 = 2.0;
        }
        else if (grade_two == 'D')
        {
            grade_2 = 1.0;
        }
        else if (grade_two == 'F')
        {
            grade_2 = 0.0;
        }
        if (grade_three == 'A')
        {
            grade_3 = 4.0;
        }
        else if (grade_three == 'B')
        {
            grade_3 = 3.0;
    
        }
        else if (grade_three == 'C')
        {
            grade_3 = 2.0;
        }
        else if (grade_three == 'D')
        {
            grade_3 = 1.0;
        }
        else if (grade_three == 'F')
        {
            grade_3 = 0.0;
        }
        if (grade_four == 'A')
        {
            grade_4 = 4.0;
        }
        else if (grade_four == 'B')
        {
            grade_4 = 3.0;
        }
        else if (grade_four == 'C')
        {
            grade_4 = 2.0;
        }
        else if (grade_four == 'D')
        {
            grade_4 = 1.0;
        }
        else if (grade_four == 'F')
        {
            grade_4 = 0.0;
        }
        if (grade_five == 'A')
        {
            grade_5 = 4.0;
        }
        else if (grade_five == 'B')
        {
            grade_5 = 3.0;
        }
        else if (grade_five == 'C')
        {
            grade_5 = 2.0;
        }
        else if (grade_five == 'D')
        {
            grade_5 = 1.0;
        }
        else if (grade_five == 'F')
        {
            grade_5 = 0.0;
        }
        else
        {
            printf("Please enter grade one");
        }
        gpa = (grade_1 + grade_2 + grade_3 + grade_4 + grade_5) / 5;
        printf("Your GPA is %.2f\n", gpa);
        if (gpa > 3.5)
        {
            printf("You are on the dean's list");
        }
        else if (gpa < 2.0)
        {
            printf("You are on academic probation");
        }
        else if (gpa == 4)
        {
            printf("Excellent");
        }
        else if (gpa == 3)
        {
            printf("Above Average");
        }
        else if (gpa == 2)
        {
            printf("Average");
        }
        else if (gpa == 1)
        {
            printf("Below Average");
        }
        else if (gpa == 0)
        {
            printf("Poor");
        }
        return 0;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    Thank you for the Tip, I've really only posted in chat forums but not programming forums.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Note: Only one branch of an if statement is normally executed.
    This should help you fix the logic bug in your program.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    When you say "branch" are you referring to the fact my program should not have two different if statements in one program doing two different functions?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by julia888 View Post
    When you say "branch" are you referring to the fact my program should not have two different if statements in one program doing two different functions?
    No! I am saying only one part/branch of a if statement is used.

    Edit: Add link If Statements in C - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by julia888 View Post
    When you say "branch" are you referring to the fact my program should not have two different if statements in one program doing two different functions?
    I was trying to imply it should have 3 or more if statements.
    As in one of your if statements needs broken into two if statements.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Feb 2018
    Posts
    6
    Take a look at this.... Maybe it'll help you out as you seem to be confused about as to how and what the if-else statement is doing.
    if-else construct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with if else statements - new to programming
    By LarissaM in forum C Programming
    Replies: 5
    Last Post: 03-19-2014, 07:10 PM
  2. can anyone help newbie in c programming
    By tears04 in forum C Programming
    Replies: 6
    Last Post: 08-11-2010, 12:44 PM
  3. Newbie at programming (for now) and in need of help
    By cleepy in forum C Programming
    Replies: 19
    Last Post: 10-30-2008, 02:40 PM
  4. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  5. newbie on programming
    By FRO212 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2007, 09:13 PM

Tags for this Thread