Having problem with a program for Calculating Grades

This is a discussion on Having problem with a program for Calculating Grades within the C Programming forums, part of the General Programming Boards category; I've been working on this program and wondering why I can't get it to work. Any insight on the issue ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    21

    Having problem with a program for Calculating Grades

    I've been working on this program and wondering why I can't get it to work. Any insight on the issue can be helpful.
    Code:
    // Program is to convert number scores into Grade Scores using if and elseif statements
    
    
    #include <stdio.h>
    #include <conio.h>
    
    
    int getScore;
    char convertGrade(int numscore);
    void showGrade
    	(int numScore,			
    		char letterGrade);
    
    
    int getScore()
    	{
    	int numScore;
    	scanf("%i", &numscore);
    	}
    
    
    void convertGrade()
    {
          		if (numscore <= 100 || numscore >= 90)
             		printf("You got an A");
          else if 
                (numscore <=89 || numscore >=80)
                 	printf("You got a B");
          else if
                (numscore <=79 || numscore >= 70)
                	printf("You go a C");
          else if
               (numscore <=69 || numscore >=60);
               		 printf(" You got a D");
          else if (numscore <= 59);
    	  
              		 printf(" You got a F");
          else
    	  			 printf("You got a Z, This is not a valid score");      
    }
    
    
    void showGrade()
    {
            int numscore
    	char letterGrade;
    	printf("What score did you get in the class?\n");
         printf("%i, %c", &numscore, letterGrade);
        
       }
    
    int main ()
    {
            
        printf(" Welcome to the Grade Conversion Program!\n");
    	 system("Pause");
        return(0);
        
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, your main function doesn't call any of your functions.


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

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    you have to call these functions in main().
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Now getting errors with too few arguements in function convertGrade and showGrade
    Code:
    // Program is to convert number scores into Grade Scores using if and elseif statements
    
    
    #include <stdio.h>
    #include <conio.h>
    
    	
    //int getScore;
    char convertGrade(int numscore);
    void showGrade
    	(int numscore,			
    		char letterGrade);
    
    
    int getScore()
    	{
        printf("What score did you get in the class?\n");
    	int numscore;
    	scanf("%i", &numscore);
    	}
    
    
    char convertGrade(int numscore)
    {
    //      		int numscore;
                  if (numscore <= 100 || numscore >= 90)
             		printf("You got an A");
          else if 
                (numscore <=89 || numscore >=80)
                 	printf("You got a B");
          else if
                (numscore <=79 || numscore >= 70)
                	printf("You go a C");
          else if
               (numscore <=69 || numscore >=60)
               		 printf(" You got a D");
          else if (numscore <= 59)
    	            		 printf(" You got a F");
          else
    	  			 printf("You got a Z, This is not a valid score");      
    }
    
    
    void showGrade(int numscore, char letterGrade)
    {
     	
         printf("%%i, %%c", &numscore, letterGrade, 0);
        
       }
    
    int main ()
    {
           
        printf(" Welcome to the Grade Conversion Program!\n");
    	
           getScore();  
           convertGrade();
            showGrade();
         system("Pause");
        return(0);
        
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem confused about how functions work. Here:
    returntype functionname( argument list )

    If the function does not need to send anything back to where it was called from, then returntype can be void, otherwise, this is some variable type (like "int").

    Then you give the function a name. That's how you call it (make it do its task).

    The arguments are values you pass to the function when you call it. You must pass a value for each argument you said your function is supposed to receive.


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

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quzah hit the nail right on the head. Also, return statements send output from a function to its caller.

    I recommend writing this program as one, big function, before breaking it down into multiple functions.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    I recommend writing this program as one, big function, before breaking it down into multiple functions.
    Granted there is not much code there so it could be just the mainline program.

    However; there is a lot of advantage in breaking larger jobs down to component blocks like the OP did and building explicit functions right from the beginning. If nothing else it compartmentalizes variable and effects on variables making the code easier to debug.

    When I write a program I first think about the best way to solve the problem... then I break it down to it's component parts... Get scores, calculate average, convert to letter grade etc. This breakdown is actually the basis for a list of functions that need to be written.

    Then when working on each of the parts --functions-- I only need to think about what I'm trying to accomplish at the moment, without worrying about the "big picture" at all. My variables are local and don't affect the mains, so if something messes up I don't have to troubleshoot my entire program, just the misbehaving bit.

    The "big blob" concept is fine for simple programs and examples, but when you start writing serious code it more often than not serves to undermine the problem solving effort.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    The program compiles and runs but doesn't give the data I want whenever I enter any data it comes up A2686752

    Code:
    
    // Program is to convert number scores into Grade Scores using if and elseif statements
    
    
    #include <stdio.h>
    #include <conio.h>
    
    	
    int getScore();
    char convertGrade(int numscore, char letterGrade);
    void showGrade(int numscore, char letterGrade);
    
    
    int getScore()
    	{
                  int numscore;
        printf("What score did you get in the class?\n");
    	
    	scanf("%d", &numscore);
    	}
    
    
    char convertGrade(int numscore, char letterGrade)
    {
      		
                  if (numscore <= 100 || numscore >= 90)
             		printf("You got an A");
             	//	letterGrade = ('A');
          else if 
                (numscore <=89 || numscore >=80)
                 	printf("You got a B");
             // letterGrade = ('B');
          else if
                (numscore <=79 || numscore >= 70)
                	printf("You go a C");
             //   letterGrade = ('C');
          else if
               (numscore <=69 || numscore >=60)
               		 printf(" You got a D");
              // 	letterGrade = ('D');
          else if (numscore <= 59)
              // letterGrade = ('F')
    	           		 printf(" You got a F");
          else
    	  	//	letterGrade = ('Z')
                  	 printf("You got a Z, This is not a valid score");      
    }
    
    
    void showGrade(int numscore, char letterGrade)
    {
    
     	     printf("%d, %c",&numscore, letterGrade);
        
       }
    
    int main ()
    {
           
        printf(" Welcome to the Grade Conversion Program!\n\n");
    	int numscore;
    	char letterGrade;
           getScore();  
        convertGrade(numscore, letterGrade);
           showGrade(numscore, letterGrade);
         system("Pause");
       return(0);
        
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In getscore(), the variable numscore is local. As soon as your program returns from getscore(), numscore is out of scope, and goes to "heaven".

    Why not return the value numscore? (or you can send the address of numscore from main, into getscore(), and make your changes "stick".

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Now I get kind of the same results as before the integers are out of scope and it never calls the void Show Grade Function
    Code:
    
    // Program is to convert number scores into Grade Scores using if and elseif statements
    
    
    #include <stdio.h>
    #include <conio.h>
    
    	
    int getScore(int numscore);
    char convertGrade(int numscore, char letterGrade);
    void showGrade(int numscore, char letterGrade);
    
    
    int getScore(int numscore)
    	{
               //   int numscore;
        printf("What score did you get in the class?\n");
    	
    	scanf("%i", &numscore);
    	}
    
    
    char convertGrade(int numscore, char letterGrade)
    {
      		
           if (numscore <= 100 || numscore >= 90)
             		printf("You got an A\n", letterGrade);
             	//	letterGrade = ('A');
          else if 
                (numscore <=89 || numscore >=80)
                 	printf("You got a B\n", letterGrade);
             // letterGrade = ('B');
          else if
                (numscore <=79 || numscore >= 70)
                	printf("You go a C\n", letterGrade);
             //   letterGrade = ('C');
          else if
               (numscore <=69 || numscore >=60)
               		 printf(" You got a D\n", letterGrade);
              // 	letterGrade = ('D');
          else if (numscore <= 59)
              // letterGrade = ('F')
    	           		 printf(" You got a F\n", letterGrade);
          else
    	  	//	letterGrade = ('Z')
                  	 printf("You got a Z, This is not a valid score\n", letterGrade);      
    }
    
    
    void showGrade(int numscore, char letterGrade)
    {
    
          printf("%i, %c", numscore, letterGrade);
        
       }
    
    int main ()
    {
           
        printf(" Welcome to the Grade Conversion Program!\n\n");
    	int numscore;
    	char letterGrade;
           getScore(numscore);  
        convertGrade(numscore, letterGrade);
           showGrade(numscore, letterGrade);
         system("Pause");
       return(numscore);
        
    }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I think you should re-read the parts of your book/tutorial that deal with the way functions work....

    When you pass in a value, internally the function operates on a copy of the variable. Thus changing a variable inside a function does not affect any same-name variables outside the function. Moreover, when the function exits, it's internal variables are destroyed so there is no way to access them.

    In the case of your GetScore() function you are not passing in anything with meaning so the parameter list can be (should be) void. However you are generating a value that you need outside the function so you want to return that value from the function.

    In main() you would call GetScore and assign it's returned value to a variable that you can then pass into your other functions to display results...

    I fixed the GetScore() function for you... now you get to fix the rest.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int getScore(void)
       { int numscore;
          printf("What score did you get in the class?\n");
         scanf("%i", &numscore);
         return numscore;  }
    
    // rest of code
    
    
    int main ()
    { int score;
       char letterGrade;
        
        printf(" Welcome to the Grade Conversion Program!\n\n");
        score = getScore();      
    
        system("Pause");
       return(0);
        
    }

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to review the meaning of AND and OR.
    Code:
    if (numscore <= 100 || numscore >= 90)
    should be using &&. Same with the others.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I think the more and more I work on it I get confused. Score is returned in the int getScore(void). Then when it goes through main that function score becomes the value of the getscore(). Should I have my convertGrade to read the numscore integer or the score integer? The problem I'm still having is my character that has to be returned through letterGrade is not displaying when I call the function showGrade().
    Code:
    // Program is to convert number scores into Grade Scores using if and elseif statements
    
    
    #include <stdio.h>
    #include <conio.h>
    
    	
    int getScore(void);
    char convertGrade(int numscore, char letterGrade);
    void showGrade(int numscore, char letterGrade);
    
    
    int getScore(void)
    	{
                 int numscore = 0;
        printf("What score did you get in the class?\n");
    	
    	scanf("%i", &numscore);
    	return numscore;
    	}
    
    
    char convertGrade(int numscore, char letterGrade)
    {
      		
           if (numscore <= 100 || numscore >= 90)
        //   		printf("You got an A\n", letterGrade);
      		letterGrade = ('A');
          else if 
                (numscore <=89 || numscore >=80)
         //      	printf("You got a B\n", letterGrade);
          letterGrade = ('B');
          else if
                (numscore <=79 || numscore >= 70)
        //      	printf("You go a C\n", letterGrade);
             letterGrade = ('C');
          else if
               (numscore <=69 || numscore >=60)
           //  		 printf(" You got a D\n", letterGrade);
            	letterGrade = ('D');
          else if (numscore <= 59)
            //	           		 printf(" You got a F\n", letterGrade);
    	       		 letterGrade = ('F');
          else
    	  	          //  	 printf("You got a Z, This is not a valid score\n", letterGrade);
                       	letterGrade = ('Z');
                       return letterGrade;  
    }
    
    
    void showGrade(int score, char letterGrade)
    {
    score = getScore();
    
          printf("You received a score of %i and letter grade of %c", score, letterGrade);
        
       }
    
    int main ()
    {
           
        printf(" Welcome to the Grade Conversion Program!\n\n");
    	int score;
    	int numscore;
    	char grade;
    	char letterGrade;
    
           score = getScore();
            grade = convertGrade(numscore, letterGrade);
          showGrade(numscore, letterGrade);
         system("Pause");
       return(0);
        
    }

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magmadragoon View Post
    I think the more and more I work on it I get confused. Score is returned in the int getScore(void). Then when it goes through main that function score becomes the value of the getscore(). Should I have my convertGrade to read the numscore integer or the score integer? The problem I'm still having is my character that has to be returned through letterGrade is not displaying when I call the function showGrade().
    Ok... set that code aside for a few minutes. Try a couple of simple exercises to understand how functions work. Of course they work the same way in your code as they do anywhere else, it's just a matter of what you feed them and what comes back...

    But first a big hint... trying to fix your code with scoop and poop is just a big waste of time and it teaches you nothing... to understand, you have to do it for yourself.

    Code:
    #include <stdio.h>
    
    // example 1 ... nothing in, nothing returned
    // this function does it's thing all by itself
    
    void SayHello(void)
      { printf("Hello\n"); }
    
    // example 2... something in nothing returned
    // this function uses data but does not return a value
    
    void PrintValue( int a)
       { printf("%d  ", a); }
    
    // example 3 ... nothing in something out
    // here we don't give it anything but we get a random number back
    
    int GetRand(void)
      { return rand(); }
    
    // example 4 ... stuff in, stuff returned
    // this one does an operation on data fed in and returns the result
    
    int GetDouble (int a)
      { return a *2 }
    
    // now to see what all this does...
    int main (void)
      { int x;
         int y;
    
         SayHello();                // nothing in, nothing back
         x = GetRand();             // nothing in result in x
         PrintValue(x);             //  x goes in nothing comes back
         y = GetDouble(x);          //   x goes in y gets what comes back
         PrintValue(y);             //    y in nothing back
    
         return 0; }
    Play with this until you understand it... then apply what you've learned to your own code.

  15. #15
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    It's obvious the OP is confused about functions.

    OP, put all your code in the main function, and read up on functions before breaking down your code into multiple functions.

    Cprogramming.com Tutorial: Functions
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  4. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 09:37 PM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

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