Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program using nested ifs
Index -> Programming, C -> C Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
stef_ios




PostPosted: 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! Very Happy Thanks.
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: Fri Jan 25, 2008 6:38 pm   Post subject: RE:Program using nested ifs

Okay, i'll start with that. Thanks!
stef_ios




PostPosted: 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:


code:

#include <stdio.h>

int
main(void)

{

        double test1, test2, assignment1, assignment2;
        double average;

        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




PostPosted: 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




PostPosted: 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




PostPosted: Thu Jan 31, 2008 2:13 pm   Post subject: RE:Program using nested ifs

&& is equivalent to and
Sponsor
Sponsor
Sponsor
sponsor
stef_ios




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
stef_ios




PostPosted: 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




PostPosted: 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

code:

if( average >= whatever_A_is)
{
    if( test1 < 50 && test2 < 50)
        printf("lesser grade");
    else
        printf("greater grade");
}


Clearly that's a basic example; and you'd need to expand upon it
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: