Posted: Wed Jan 23, 2008 3:56 pm Post subject: Program using nested ifs
Hey all. So i'm writing a program that asks the user for four marks.
a)All the marks are out of 100, and are equally weighted in the average.
b)Grades are assigned based on A = 80 ? 100, B = 70 ? 79, C = 60 ? 69, D = 50 ? 59, F = 0 ? 49
c)If the student fails (i.e. mark < 50) either or both assignments, the grade is reduced by 1 (e.g. B becomes C).
d)If the student fails either or both tests, the mark will be F.
So here is my program. I am having some trouble with part c) and was wondering if anyone could let me know what I need to do for the program to reduce the mark by one. Also, how do I get the program to make sure a number above 100 is not entered for the test or assignment marks.
code:
#include <stdio.h>
#include <math.h>
int
main(void)
{
double test1, test2, assignment1, assignment2;
printf("Please enter the first test mark: ");
scanf("%f", &test1);
printf("You entered: %f \n", test1);"
printf("Please enter the second test mark: ");
scanf("%f", &test2);
printf("You entered: %f \n",test2);
printf("Please enter the first assignment mark: ");
scanf("%f", &assignment1);
printf("You entered: %f \n", assignment1);
printf("Please enter the second assignment mark: ");
scanf("%f", &assignment2);
printf("You entered: %f \n", assignment2);
/* Now using nested ifs the program will use the calculate the average of the entered numbers and will
determine what the final mark is for the student */
if ((test1+test2+assignment1+assignment2)/4 == '100>80')
printf ("You got an A! \n");
else
if ((test1+test2+assignment1+assignment2)/4 == '79>70')
printf ("You got a B! \n");
else
if ((test1+test2+assignment1+assignment2)/4 == '69>60')
printf ("You got a C! \n");
else
if ((test1+test2+assignment1+assignment2)/4 == '59>50')
printf ("You got a D! \n");
else
if ((test1+test2+assignment1+assignment2)/4 == '49>0')
printf ("You got an F! \n");
else
if (test1,test2 == '50>0')
printf ("You got an F! \n");
else
if ((assignment1, assignment2) == '50>0')
printf ("You got a ! \n");
else
printf ("INVALID MARK! \n");
return (0);
}
So that is what I have. So any help would be great! Thanks.
Sponsor Sponsor
OneOffDriveByPoster
Posted: Wed Jan 23, 2008 9:26 pm Post subject: Re: Program using nested ifs
stef_ios @ Wed Jan 23, 2008 3:56 pm wrote:
code:
printf("You entered: %f \n", test1);"
...
if (test1,test2 == '50>0')
Please try to compile your code as you go. You have an unclosed string literal and '50>0' is not a comparison. Either that or you have intensionally obfuscated code.
md
Posted: Thu Jan 24, 2008 1:04 am Post subject: RE:Program using nested ifs
No, the code is not even close to being correct.
I sugest looking up "else if" and comparisons to start. And as OneOffDriveByPoster said, try compiling your code and fixing all the errors.
stef_ios
Posted: Fri Jan 25, 2008 6:38 pm Post subject: RE:Program using nested ifs
Okay, i'll start with that. Thanks!
stef_ios
Posted: Wed Jan 30, 2008 2:06 pm Post subject: Re: Program using nested ifs
Okay, so thanks to a few great sites and my C books, I have managed to get my program to work. Now the only problem I am running into is that the program is being terminated even when the statement is not true. I got the testing of the test marks to work, and print "You got an F!" if one of the tests is failed. But if the tests were passed, and say the student got a %60 average, it is still printing "You got an A!". Can someone tell me why this is happening?
Here is my code:
printf("Please enter the first test mark: ");
scanf("%lf", &test1);
printf("Please enter the second test mark: ");
scanf("%lf", &test2);
printf("Please enter the first assignment mark: ");
scanf("%lf", &assignment1);
printf("Please enter the second assignment mark: ");
scanf("%lf", &assignment2);
average = ((test1 + test2 + assignment1 + assignment2)/4);
printf("Your average is: %f \n\n", average);
/* Now using nested ifs the program will use the calculate the average of the entered numbers and will
determine what the final mark is for the student. There are also if statements which check that the
given marks are within the restrictions (that both tests and both assignments were passed). */
if (test1<50)
{
printf ("You got an F! \n");
}
else if (test2<50)
{
printf ("You got an F! \n");
}
else if(80<=(average)<=100)
{
printf ("You got an A! \n");
}
else if (70<=(average)<=79)
{
printf ("You got a B! \n");
}
else if (60<=(average)<=69)
{
printf ("You got a C! \n");
}
else if (50<=(average)<=59)
{
printf ("You got a D! \n");
}
else if (0<=(average)<=49)
{
printf ("You got an F! \n");
}
else if (0<=(assignment1,assignment2)<=49)
{
printf ("You got a ! \n");
}
return (0);
}
OneOffDriveByPoster
Posted: Wed Jan 30, 2008 3:33 pm Post subject: Re: Program using nested ifs
code:
80<=(average)<=100
should be
code:
80 <= average && average <=100
stef_ios
Posted: Thu Jan 31, 2008 2:05 pm Post subject: RE:Program using nested ifs
Okay so I put that in, but do I need to put that in for each case? Or just for the 80-100? And what does that bit of code mean? Why average && average, and not just average? Thanks
Clayton
Posted: Thu Jan 31, 2008 2:13 pm Post subject: RE:Program using nested ifs
&& is equivalent to and
Sponsor Sponsor
stef_ios
Posted: Fri Feb 01, 2008 5:01 pm Post subject: RE:Program using nested ifs
Oaky, I put it in, but it's still terminating the program even if the condition is false.
stef_ios
Posted: Sun Feb 03, 2008 1:10 pm Post subject: RE:Program using nested ifs
Any other suggestions as to why my program is not working, and how I can make it work?
md
Posted: Sun Feb 03, 2008 1:39 pm Post subject: RE:Program using nested ifs
Can you post your new code? Then we could point out possible errors in it.
It would also be a good idea to cut out everything except the first if for now; get one small part working and them move on to the rest.
stef_ios
Posted: Sun Feb 03, 2008 4:06 pm Post subject: Re: Program using nested ifs
Okay so here is the if statements part of my code, because I know the first part works.
code:
if (test1<50)
{
printf ("You got an F! \n");
}
else if (test2<50)
{
printf ("You got an F! \n");
}
lse if(80<=(average && average)<=100)
{
printf ("You got an A! \n");
}
else if (70<=(average)<=79)
{
printf ("You got a B! \n");
}
else if (60<=(average)<=69)
{
printf ("You got a C! \n");
}
else if (50<=(average)<=59)
{
printf ("You got a D! \n");
}
else if (0<=(average)<=49)
{
printf ("You got an F! \n");
}
return (0);
}
So basically, it has to scan the average, but even if the average is below 80, it is still saying "You got an A". It's not even scanning the other if statements, even if the condition is false.
Tony
Posted: Sun Feb 03, 2008 4:13 pm Post subject: Re: Program using nested ifs
Lets try this one again...
OneOffDriveByPoster @ Wed Jan 30, 2008 3:33 pm wrote:
code:
80<=(average)<=100
should be
code:
80 <= average && average <=100
or more specifically
(80 <= average) && (average <=100)
boolean logic is calculated one piece at a time, connected together by and, or, and other boolean statements.
Posted: Sun Feb 03, 2008 4:23 pm Post subject: Re: Program using nested ifs
Thanks so much! It works! Okay, so I have one more thing to do for my program. If the student fails (i.e. mark < 50) either or both assignments, the grade is reduced by 1 (e.g. B becomes C). So how do get my program to do this?
md
Posted: Mon Feb 04, 2008 12:36 am Post subject: RE:Program using nested ifs
the way you have your code now you could just add some more if statements like