### Thread: Homework help with IF ELSE please

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
11
Rep 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;

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;

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)
{
printf("A");
}
else if (73<=Final)
{
printf("B");
}
else if (61<=Final)
{
printf("C");
}
else if (49<=Final)
{
printf("D");
}
else
{
printf("F");
}
}

system("pause");
return 0;
}```
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2009
Posts
45
Rep Power
8
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)`
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
11
Rep 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!
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
11
Rep 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;

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)
{
}
else if (73<=Final)
{
}
else if (61<=Final)
{
}
else if (49<=Final)
{
}
else
{
}

system("pause");
return 0;
}
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2009
Posts
45
Rep Power
8
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