C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 13th, 2013, 12:35 AM
jaimeribg jaimeribg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 jaimeribg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 31 m 27 sec
Reputation Power: 0
Homework help with IF ELSE please

Hey I'm new here and was hoping somebody could help me with an assignment for my C programming class.

I'm still pretty new at this so this mistake might be pretty obvious to most of you, but I can't seem to find it and have spent pretty much all day troubleshooting.

I need the program to assign the correct letter grade to the final average assuming all the assignment, test, and final exam entries are valid entries (>=0 && <=100) but every time I run the program, even if the average is below a 40, the program assigns a grade of A.

Please help me! I've run out of ideas and the class notes don't detail the use of IF ELSE and FLOAT so I have no idea if FLOAT is even necessary or how to properly use it.

Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int A1, A2, A3, A4, A5, AssAverage;
    int T1, T2, T3, TestAverage;
    int E1,E1Average,Final, Grade; 
    
    
    
    bool A1Good;
    bool A2Good;
    bool A3Good;
    bool A4Good;
    bool A5Good;
    bool AssAverageGood;
    bool T1Good;
    bool T2Good;
    bool T3Good;
    bool TestAverageGood;
    bool E1Good;
    bool AllGood;
    
    char A, B, C, D, F;
    
    printf("Please input Assignment 1:");
    scanf("%d",&A1);
    A1Good=(0<=A1 && A1<=100); 
    if (!A1Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", A1); 
         }
         
    
    
    printf("Please input Assignment 2:");
    scanf("%d",&A2);
    A2Good=(0<=A2 && A2<=100);
    if (!A2Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", A2); 
         }
    
    
    printf("Please input Assignment 3:");
    scanf("%d",&A3);
    A3Good=(0<=A3 && A3<=100);
    if (!A3Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", A3); 
         }
   
    
    printf("Please input Assignment 4:");
    scanf("%d",&A4);
    A4Good=(0<=A4 && A4<=100);
    if (!A4Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", A4); 
         }
    
    
    printf("Please input Assignment 5:");
    scanf("%d",&A5);
    A5Good=(0<=A5 && A5<=100);
    if (!A5Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", A5); 
         }
    
    AssAverage=.2*((A1+A2+A3+A4+A5)/5);
    AssAverageGood=(0<=AssAverage && AssAverage<=100);
    
    if (!A1Good || !A2Good || !A3Good || !A4Good || !A5Good)
    {
                printf("At least one entry is invalid\n");
                }
                else 
                {
                     printf("Points Awarded=%d\n", AssAverage);
                     }
    
    printf("Please input Test 1:");
    scanf("%d",&T1);
    T1Good=(0<=T1 && T1<=100);
    if (!T1Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", T1); 
         }
   
    
    printf("Please input Test 2:");
    scanf("%d",&T2);
    T2Good=(0<=T2 && T2<=100);
    if (!T2Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", T2); 
         }
    
    
    printf("Please input Test 3:");
    scanf("%d",&T3);
    T3Good=(0<=T3 && T3<=100);
    if (!T3Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", T3); 
         }
    
    TestAverage=.4*((T1+T2+T3)/3);
    TestAverageGood=(0<=TestAverage && TestAverage<=100);
    if (!T1Good || !T2Good || !T3Good)
    {
                printf("At least one entry is invalid\n");
                }
                else 
                {
                     printf("Points Awarded=%d\n", TestAverage);
                     }
    
  
    
    printf("Please input Exam Score:");
    scanf("%d",&E1);
    E1Good=(0<=E1 && E1<=100);
    E1Average=.4*(E1);
    if (!E1Good)
    {
                printf("invalid entry\n");              
    }
    else 
    {
         printf("input=%d\n", E1); 
         printf("Points Awarded=%d\n", E1Average);
         }
   
    
    Final=AssAverage+TestAverage+E1Average;
    printf("Final Grade=%d\n", Final);
    
    
    AllGood=(A1Good && A2Good && A3Good && A4Good && A5Good && AssAverageGood
    && T1Good && T2Good && T3Good && TestAverageGood && E1Good );
    
    if(!AllGood)
    {
                printf("Some scores are invalid\n");
                }
                else if(85<=Final<=100)
                    {
                    Grade='A';
                    printf("A");
                    }
                    else if (73<=Final)
                    {
                    Grade='B';
                    printf("B");
                    }
                    else if (61<=Final)
                    {
                    Grade='C';
                    printf("C");
                    }
                    else if (49<=Final)
                    {
                    Grade='D';
                    printf("D");
                    }
                    else
                    {
                    Grade='F';
                    printf("F");
                    }
                printf("grade=%c\n", Grade);
    }
    
    
    system("pause");
    return 0;
}

Reply With Quote
  #2  
Old February 13th, 2013, 01:36 AM
DRK82 DRK82 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 25 DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 2 h 41 m 58 sec
Reputation Power: 0
This expression is causing all the trouble:
Code:
if (85 <= Final <= 100)
The compiler evaluates it like this (in 2 steps):
Code:
if ((85 <= Final) <= 100)
This part of expression
Code:
(85 <= Final)
returns value of 0 or 1 depending on Final value, but it dosen't matter because the second evaluation
Code:
((0) <= 100)
((1) <= 100)
will be always true.

What you need is to use && opereator
Code:
if (85 <= Final && Final <= 100)

Reply With Quote
  #3  
Old February 13th, 2013, 10:23 AM
jaimeribg jaimeribg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 jaimeribg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 31 m 27 sec
Reputation Power: 0
Thank you soooo much DRK82!!! Just a little tweaking here and there and I got it running smoothly! Now I just need help with one last part. My teacher said I need to have the program terminate after an invalid entry is made. Here are the instructions, all of wich I believe I've followed thus far:


Write a program to compute the grade earned by a (one) student.
The program will initially request 5 assignment scores, 3 test scores, and 1 final examination score. *All scores are out of 100. *If any input score is out of range (negative or above 100), output an error statement and terminate the program.
Use bool (in C++) to record whether a score is out-of-range or not.
Calculate the score of the student based on the following weights:
Assignments ***20%
Tests *************40%
Exam ************40%

Based on the weighted score, assign the grade for the student as follows:
85<= score <= 100 *******A
73 *<= score < 85 **********B
61 *<= score < 73 **********C
49 <= score < 61 **********D
0 **<= score < 49 **********F

Output the computed score, and the grade earned by the student.

:
He did not explain in class anything about terminating the program after an entry and to make it more fun, he adds this in the list of things that he will take off points or:

:

**any use of: break, exit statements (non-structured program) - 20 pts off
There should only be one place to terminate the program!!!



Please help me finish this thing! I have no idea how to even attempt this!

Thanks!

Reply With Quote
  #4  
Old February 13th, 2013, 10:29 AM
jaimeribg jaimeribg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 jaimeribg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 31 m 27 sec
Reputation Power: 0
Sorry I forgot I post the code I have so far!
(Couldn't get it to all show up correctly in the code text wrap)



#include <stdlib.h>
#include <stdio.h>

int main()
{
float A1, A2, A3, A4, A5, AssAverage;
float T1, T2, T3, TestAverage;
float E1,E1Average,Final, Grade;


bool A1Good;
bool A2Good;
bool A3Good;
bool A4Good;
bool A5Good;
bool AssAverageGood;
bool T1Good;
bool T2Good;
bool T3Good;
bool TestAverageGood;
bool E1Good;
bool AllGood;

char A, B, C, D, F;

printf("Please input Assignment 1:");
scanf("%f",&A1);
A1Good=(0<=A1 && A1<=100);
if (!A1Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", A1);
}



printf("Please input Assignment 2:");
scanf("%f",&A2);
A2Good=(0<=A2 && A2<=100);
if (!A2Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", A2);
}


printf("Please input Assignment 3:");
scanf("%f",&A3);
A3Good=(0<=A3 && A3<=100);
if (!A3Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", A3);
}


printf("Please input Assignment 4:");
scanf("%f",&A4);
A4Good=(0<=A4 && A4<=100);
if (!A4Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", A4);
}


printf("Please input Assignment 5:");
scanf("%f",&A5);
A5Good=(0<=A5 && A5<=100);
if (!A5Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", A5);
}

AssAverage=.2*((A1+A2+A3+A4+A5)/5);
AssAverageGood=(0<=AssAverage && AssAverage<=100);

if (!A1Good || !A2Good || !A3Good || !A4Good || !A5Good)
{
printf("At least one entry is invalid\n");
}
else
{
printf("Points Awarded=%f\n", AssAverage);
}

printf("Please input Test 1:");
scanf("%f",&T1);
T1Good=(0<=T1 && T1<=100);
if (!T1Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", T1);
}


printf("Please input Test 2:");
scanf("%f",&T2);
T2Good=(0<=T2 && T2<=100);
if (!T2Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", T2);
}


printf("Please input Test 3:");
scanf("%f",&T3);
T3Good=(0<=T3 && T3<=100);
if (!T3Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", T3);
}

TestAverage=.4*((T1+T2+T3)/3);
TestAverageGood=(0<=TestAverage && TestAverage<=100);
if (!T1Good || !T2Good || !T3Good)
{
printf("At least one entry is invalid\n");
}
else
{
printf("Points Awarded=%f\n", TestAverage);
}



printf("Please input Exam Score:");
scanf("%f",&E1);
E1Good=(0<=E1 && E1<=100);
E1Average=.4*(E1);
if (!E1Good)
{
printf("invalid entry\n");
}
else
{
printf("input=%f\n", E1);
printf("Points Awarded=%f\n", E1Average);
}


Final=AssAverage+TestAverage+E1Average;
printf("Final Average=%f\n", Final);


AllGood=(A1Good && A2Good && A3Good && A4Good && A5Good && AssAverageGood
&& T1Good && T2Good && T3Good && TestAverageGood && E1Good );

if(!AllGood)
{
printf("Some scores are invalid\n");
}
else if(85<=Final && Final <=100)
{
Grade='A';
printf("Final Grade=A\n");
}
else if (73<=Final)
{
Grade='B';
printf("Final Grade=B\n");
}
else if (61<=Final)
{
Grade='C';
printf("Final Grade=C\n");
}
else if (49<=Final)
{
Grade='D';
printf("Final Grade=D\n");
}
else
{
Grade='F';
printf("Final Grade=F\n");
}




system("pause");
return 0;
}

Reply With Quote
  #5  
Old February 14th, 2013, 01:34 AM
DRK82 DRK82 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 25 DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 2 h 41 m 58 sec
Reputation Power: 0
Initialize all bool variables with false. Check value of A1Good..E1Good variables before prompting next input.

Note that you should also check value returned by scanf() function which indicates number of correctly read objects.

Code:
int objects;

printf("Please input Assignment 1:");
objects = scanf("%f", &A1);
A1Good = (objects == 1 && 0 <= A1 && A1 <= 100); 
if (!A1Good)
{
    printf("invalid entry\n"); 
}
else 
{
    printf("input=%f\n", A1); 
}

if (A1Good)
{
    printf("Please input Assignment 2:");
    objects = scanf("%f", &A2);
    A2Good = (objects == 1 && 0 <= A2 && A2 <= 100);
    if (!A2Good)
    {
        printf("invalid entry\n"); 
    }
    else
    {
        printf("input=%f\n", A2); 
    }
}

if (A2Good)
{
    // ...
}


Have you noticed that some part of your code is being repeated? That's when in structural programming functions come in handy.
Code:
bool getScore(float *score)
{
    int objects = scanf("%f", score);
    bool good = (objects == 1 && 0 <= *score && *score <= 100);
    if (!good)
    {
        printf("invalid entry\n"); 
    }
    else
    {
        printf("input=%f\n", *score); 
    }
    return good;
}

printf("Please input Assignment 1:");
A1Good = getScore(&A1);

if (A1Good)
{
    printf("Please input Assignment 2:");
    A2Good = getScore(&A2);
}

if (A2Good)
{
    // ...
}
Comments on this post
tredway agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Homework help with IF ELSE please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap