Sentinel while loop and errors.

This is a discussion on Sentinel while loop and errors. within the C Programming forums, part of the General Programming Boards category; hello: I have to write a program that calculates a student's term GPA. inputs- the letter grade and the credits ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Post Sentinel while loop and errors.

    hello:
    I have to write a program that calculates a student's term GPA.
    inputs- the letter grade and the credits for each course.
    but , the letter grade has two character the letter and the suffix ex. B+, i'm not sure how to enter these two characters. Also , I have to add 1/3 or subtract 1/3 depending on the suffix.
    the specific requirements are to use a sentinel-terminated while loop, at least one switch statement and if-else statements.
    Any number of grades may be entered, ending with a sentinel 0 for the course credits.

    i tried testing it and it said "unexpected end of file while looking for precompiled header directive ??? what does it mean?

    this is what i have so far, please help!!

    Code:
     #include "stdio.h"		/* printf and scanf definitions				*/
    
    #define SENTINEL 0		/* value for the program to terminate		*/
    
    int main()
    {
        char grade;			/* input-letter grade per class				*/
    	char suffix;		/* input-suffix of the letter grade, if any	*/
    	int credits=0;		/* credits per class						*/
    	int sumcredits=0;	/* total credits for the term				*/
    	double classGPA=0;	/* gpa of a single class					*/
    	double termGPA=0;	/* output-gpa of the term					*/
    	
    	
    		/* Get grade and credits			*/		
    	printf("Enter first grade and corresponding credits or %d to finish > ", SENTINEL);
    	scanf("%c%c %d", &grade, &suffix, &credits);
    	
    		while !(grade == SENTINEL && credits == SENTINEL)
    		{
    			switch (grade)
    			{
    			case:'a'
    			case:'A' 
    			 grade=4;break;
    			case:'b'
    			case:'B'
    			 grade=3;break;
    			case:'c'
    			case:'C'
    			 grade=2;break;
    			case:'d'
    			case:'D'
    			 grade=1;break;
    			default 
    			printf("GPA cannot be calculated with the grade entered");
    			break;
    			}
    		if(suffix == '+')
    			grade = grade + (1/3);
    		else if(suffix == '-')
    			grade = grade - (1/3);
    		else
    			grade = grade;
    
    		classGPA = (classGPA)* credits;
    		sumcredits = sumcredits + credits;
    		}
    
    		termGPA = classGPA / sumcredits;
    
    		printf(" the student's GPA for the term is %.3f \n", termGPA);
            
    	
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    scanf("%c%c %d", &grade, &suffix, &credits);
    A better way would to be to read into a buffer using fgets, and then do something like:
    Code:
    switch( toupper( buffer[0] ) )
    {
        case 'A': ... break;
        ...
        case 'F': ... break;
        default: message about an invalid grade...
    }
    if( buf[1] == '+' )
        ...
    else
    if( buf[1] == '-' )
        ...
    The next problem you have is here, and this is your main problem:
    Code:
    while !(grade == SENTINEL && credits == SENTINEL)
    This should be, at the very least, done this way:
    Code:
    while( !(grade == SENTINEL && cretids == SENTINEL) )
    You need parenthesis around the true/false statement.

    And finally, you can't subtract decimal ammounts from a character. You should be using a double or float and giving that the value.
    Code:
    double grade = 0.0;
    ...
        case 'A': grade = 4.0; break;
    ...
        if( ...plus suffix... )
            grade += 0.33;
    Try that on for size and see what you come up with. Oh, and you likely want "#include <stdio.h>" instead of with quotes.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Arrow

    thank you for the fast response, i'll try that now, but i'm not sure what u meant about the buffer, we haven't done anything using buffers and fgets. i'll read and try anyways, but can u tell me if there is a different way? please.
    thanks.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    One thing quzah didn't mention is that your equation:
    grade = grade + (1/3);
    won't work. (1/3) will equal zero. You will need to use 1.0/3.0 or as he mentioned, use .333 instead.

    By the way, I would assume you have not learned fgets() yet. He's right, it's better, but use what you know.

    -----------------------------------
    Ahh... I suspected as much
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Unhappy

    i changed a few things in the code:
    Code:
    while (credits != SENTINEL)
    		{
    			switch (grade)
    			{
    			case'a':
    			case'A':
    			 grade=4.0;break;
    			case'b':
    			case'B':
    			 grade=3.0;break;
                                       ........
    and also

    Code:
    	
    	if(suffix == '+')
    			grade = grade + (0.33);
    		else if(suffix == '-')
    			grade = grade - (0.33);
    		else
    			grade = grade;
    
    		........
    but i'm still getting the same error message :
    "unexpected end of file while looking for pricompiled header directive".
    and i did change the quotes from the stdio.
    Any clue of what's happening?
    Is the order of my statements correct? the loop with the switch and so on? in order for my program to run?
    thanks in advance.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    hello:
    after all my changes , my program is still not runnig. Can anyone tell how to fix it. I 've tried but nothing.
    Any help would be appreciated.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by lollypop
    hello:
    after all my changes , my program is still not runnig. Can anyone tell how to fix it. I 've tried but nothing.
    Any help would be appreciated.
    Let's see -- changes, no new code posted -- nope. But after your next post I'll bet we can help.

    Be sure to describe the problem fully.

    By the way, the switch and if statements look much better now.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Use <> to include stdio.h in your code:
    Code:
    #include <stdio.h>
    $ENV: FreeBSD, gcc, emacs

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    hi again
    i changed the quotes from the stdio, but still my program doesn't work. i get the same error: "unexpected end of file while looking for pricompiled header directive".
    can you tell me if there is something else wrong? i really don't know. here is the whole program with the changes, thanks.

    Code:
    	*Program that calculates a student's term GPA.
    */
    
    #include <stdio.h>		/* printf and scanf definitions				*/
    
    #define SENTINEL 0		/* value for the program to terminate		*/
    
    int main()
    {
        char grade;			/* input-letter grade per class				*/
    	char suffix;		/* input-suffix of the letter grade, if any	*/
    	int credits=0;		/* credits per class						*/
    	int sumcredits=0;	/* total credits for the term				*/
    	double classGPA=0;	/* gpa of a single class					*/
    	double termGPA=0;	/* output-gpa of the term					*/
    	
    	
    		/* Get grade and credits			*/		
    	printf("Enter first grade and corresponding credits or %d to finish > ", SENTINEL);
    	scanf("%c%c %d", &grade, &suffix, &credits);
    	
    		while (credits != SENTINEL)
    		{
    			switch (grade)
    			{
    			case'a':
    			case'A':
    			 grade=4.0;break;
    			case'b':
    			case'B':
    			 grade=3.0;break;
    			case'c':
    			case'C':
    			 grade=2.0;break;
    			case'd':
    			case'D':
    			 grade=1.0;break;
    			case'f':
    			case'F':
    			 grade=0.0;break;
    			default 
    			printf("GPA cannot be calculated with the grade entered");
    			break;
    			}
    		if(suffix == '+')
    			grade = grade + (0.33);
    		else if(suffix == '-')
    			grade = grade - (0.33);
    		else
    			grade = grade;
    
    		classGPA = (grade)* credits;
    		sumcredits = sumcredits + credits;
    		}
    
    		termGPA = classGPA / sumcredits;
    
    		printf(" The student's GPA for the term is %.3f \n", termGPA);
            
    	
    	return 0;
    }

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    This is not all of your troubles, but it would help:

    Code:
     grade=0.0;break;
     default 
     printf("GPA cannot be calculated with the grade entered");
    break;
    You need a : after default.

    Also, if your comments look like they do in your last post here, that would definitely give you some trouble.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by lollypop
    Code:
    	
    /*Program that calculates a student's term GPA.
    */
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    lollypop wrote:
    hello:
    I have to write a program that calculates a student's term GPA.
    inputs- the letter grade and the credits for each course.
    I am a little confused as to how the credits variable figures in. At first I thought it was a way of entering multiple grades - for example if the student had three courses where an A was obtained, you could enter 'A' and then '3' for credits, and then the program could tally that and figure it out for the GPA later. I now think this is not the idea though. So what are the credits about?

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    hi
    the credits for my program is for each class , For example if you took 2 different classes u would have to enter one grade for each and the amount of credits for each individually
    A 4
    B+ 3

    or whatever , depending on the number of classes. then the GPA would be the sum of all the grades times amount of credits , divided by the sum of all the credits; with previous numbers:
    ( (4.0*4 + (3+0.33)*3 ) / (4+3) )

    those are the specifics of the program, eventhoug there are easier ways to calcultae the gpa.
    my program has to repeat the process for each class , that's why I use the loop, but i'm still not sure if i used it the right way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why Errors in Prime Number Loop
    By wco5002 in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2008, 11:49 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. PLEASE help with loop, this has logic errors
    By cerealstone in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2003, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21