Thread: Help with C program

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Help with C program

    I am having a problem with this program assiging a letter grade given a numeric score. The program should continue accepting a value until a negative number(ANY negative number) is input. Have to use the If-else, if-else construct. The program should print how many As, Bs, etc. were assigned. It is not working properly. When I compile the program it has no errors. When I run it, it prints the same line over and over and won't stop. Please help. What am I doing wrong?

    Code:
    #include<stdio.h>
    void main(void)
    ////////////////////*	Declare and Intialize variable	*///////////////////////////////
    
    {	int score,  a_s, b_s, c_s, d_s, f_s, grades_total;
    
    	a_s = 0; b_s = 0; c_s = 0; d_s = 0; f_s = 0;
    
    ////////////////////*   User Input Values  *////////////////////////////////////////////
    
    	printf("Enter a score:	");
    	scanf("%i", &score);
    	while(score != -1)
    	{
    		if(score >=93)
    		{	printf("  A score of 100-93 is a(n) A\n");
    			++a_s;
    		}
    		else if(score >= 92)
    		{	printf("  A score of 92-90 is a(n) A-\n");
    			++a_s;
    		}
    		else if(score <= 89)
    		{	printf("  A score of 89-87 is a(n) B+\n");
    			++b_s;
    		}
    		else if(score <= 86)
    		{	printf("  A score of 86-83 is a(n) B\n");
    			++b_s;
    		}
    		else if(score <= 82)
    		{	printf(" A score of 82-80 is a(n) B-\n");
    			++b_s;
    		}
    		else if(score  <= 79)
    		{	printf(" A score of 79-77 is a(n) C+\n");
    			++c_s;
    		}
    		else if(score <= 76)
    		{	printf(" A score of 76-73 is a(n) C\n");
    			++c_s;
    		}
    		else if(score <= 72)
    		{	printf(" A score of 72-70 is a(n) C-\n");
    			++c_s;
    		}		
    		else if(score <= 69)
    		{	printf(" A score of 69-67 is a(n) D+\n");
    			++d_s;
    		}
    		else if(score <= 66)
    		{	printf(" A score of 66-63 is a(n) D\n");
    			++d_s;
    		}
    		else if(score <= 62)
    		{	printf(" A score of 62-60 is a(n) D-\n");
    			++d_s;
    		}
    		else if(score <= 59)
    		{	printf(" A score of 59 and below is a(n) F\n");
    			++f_s;
    		}
    		printf("Enter a score: ");
    		scanf("i%", &score);
    	}
    
    		printf("Grade Summary\n");
    		printf("-------------\n");
    		printf("%i	A's (A, A-)\n", a_s);
    		printf("%i	B's (B+, B, B-)\n", b_s);
    		printf("%i	C's (C+, C, C-)\n", c_s);
    		printf("%i	D's (D+, D, D-)\n", d_s);
    		printf("%i	F's\n", f_s);
    		printf("-------------------\n");
    		grades_total = a_s + b_s + c_s + d_s + f_s;
    		printf("%i	(grades total)", grades_total);
    
    }
    Last edited by renniegq; 10-18-2003 at 10:42 PM.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    The program should continue accepting a value until a negative number(ANY negative number) is input.
    Your only checking for -1, ( "while(score != -1)" ) check for something less than zero.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I really don't understand the concept yet of entering any negative value and how to code it. My program still won't run right. If I enter any score it will not do the printf like it is suppose to. Any help?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    maybe you'd like to try the condition for < 0 instead of -1 only so that ALL negative scores will be reflected as error?

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Many are your problems with this program, grasshopper. You must seek to clean up the mess. Allow me to give you some hints, but first let's start with the preliminaries.

    Code:
    void main(void)
    
    is incorrect. Use:
    
    int main(void)
    There is a ton of stuff on the cprogramming board regarding the matter, which you can find in the faq and by doing a search, so I won't go into more detail - you can start by looking here .
    When I run it, it prints the same line over and over and won't stop. Please help. What am I doing wrong?
    It would be helpful for you if you would step through this program with a debugger, just to see what is going on when you run it, however it is sufficient to think of what you are saying to the program. In essence, you are telling it to:

    get a number (score)
    while that number is not -1
    check it against a value
    when you find a match, execute the statement, as long as it is not -1.

    In your case, the statement is to print a line to the screen, which the program will do over and over again.

    So you need to rearrange things a little. I suspect this is more what you want:

    while a condtion is not true (eg, while score != 0)
    scan for a number
    compare the number
    execute the statements (print a line, increment the corresponding variable)

    Code:
    while( number != 0 ){
    scanf( for a number);
      if(number is such and such)
          statements;
    }
    In this way, you will keep scanning for a number until the number 0 is entered. BTW I used 0 as a convenient way to kill this particular loop to move on to the rest of the program. 0 is arbitrary. If you do use 0 you will have to change your conditional if statements to reflect it, or when you press 0 it will count it as a grade which you don't want if you are using zero as a terminating signal.

    edit: I missed the part about the program terminating with any negative number. Like I said, 0 was an arbitrary number - you can use a negative number to terminate the loop too.


    Ok, so if you manage to get the control all worked out, you then have to address the next main problem. Take a look at your conditional tests.
    Code:
    else if(score <= 89)
    		{	printf("  A score of 89-87 is a(n) B+\n");
    			++b_s;
    What this is saying is that any number unter 89 will get a B. So you need to word the test to reflect that any number less than 89 but higher than the next lower number (eg 86) is a B

    Make sense? I am sure you will be able to get it all figured out now.

    Some other things that you might want to check out:

    This one on getting numbers from the user.

    Good luck!

    ~/
    Last edited by kermit; 10-19-2003 at 09:43 AM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    Thanks for the tips I will try them and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM