Thread: beginner function problems

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    beginner function problems

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    int getScore();
    char convertGrade();
    void showGrade();
    
    
    int main(void)
    {
    int numGrade;
    char letterGrade;
    
     numGrade=getScore();
     convertGrade();
     lettergrade=convertGrade();
     showGrade();
    
    return (0);
    
    }
    
    int getScore()
    	{
    	
    	int numGrade;
    
    	printf ("Please enter your numeric grade> ");
    	scanf ("%d", &numGrade);
    	return numGrade;
    
    	}
    
    
    
    char convertGrade(int numGrade)
    
    	{
    	char lettergrade;
    
    
    	if  ((numGrade >= 90) && (numGrade <= 100))
    	{	lettergrade = 'A';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=80) && (numGrade <=89))
    	{	lettergrade = 'B';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=70) && (numGrade <=79))
    	{	lettergrade = 'C';
    		return lettergrade;
    	}
    
    
    	else if ((numGrade >=60) && (numGrade <=79))
    	{	
    		lettergrade = 'D';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=59) && (numGrade <=0))
    	{	
    		lettergrade = 'F';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=100) || (numGrade <=0))
    	{	lettergrade = 'Z';
    		return lettergrade;
    	}
    
    	}
    
    void showGrade(int numGrade, char lettergrade);
    {
    if (lettergrade==Z)
    	printf ("invalid grade");
    
    }
    I'm trying to get this program work. I need to have three functions


    Code:
    int getScore();
    char convertGrade();
    void showGrade();
    I'm receiving bunch of error and i'd post it here, but I can't get to my server right now.

    What i'm trying to accomplish is to convert numeric grades to Lettergrades.

    I remember an error that i kept receiving. It says "Previous declarion of showGrade was here".

    Can someone help me to figure out what is wrong with my code?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The prototype differs from the function...
    Code:
    prototype = void showGrade();
    function = void showGrade(int numGrade, char lettergrade);
    They should be the same, you're also including a ; on the end of the function declaration -- which is a no, no.

    Code:
    void showGrade(int numGrade, char lettergrade);
    {
    if (lettergrade==Z)
    	printf ("invalid grade");
    
    }
    Also, try work on your indenting -- it doesn't matter what style you use, only that you're consistant.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    k, thanks...

    but How do i show converted grades by having that function?

    something like this?

    Code:
    void showGrade();
    {
    if (lettergrade==Z)
    	printf ("invalid grade");
    
    else 
    
    printf ("Your score of %d is &lf grade", numGrade, lettergreade);
    
    }
    how do i get the lettergrade and numgrade value from above functions?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to pass the data into the function. In other words, change the function prototype to reflect reality, rather than the other way around.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void showGrade();
    Note the ; at the end? Remove it. You should only use them on function prototypes, not the actual definitions.
    And I suggest you also try to indent your code as in the example at http://cpwiki.sf.net/Indentation
    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I think zacs7 meant for you to change the prototype to match the function declaration.
    [edit]as opposed to changing the function declaration to match the prototype[/edit]
    Last edited by Brad0407; 02-27-2008 at 08:00 PM.
    Don't quote me on that... ...seriously

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the function declarations and definitions must match, so see that they do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getScore();
    char convertGrade();
    void showGrade(int numGrade, char lettergrade);
    
    
    int main(void)
    {
    int numGrade;
    char letterGrade;
    
     numGrade=getScore();
     convertGrade();
     lettergrade=convertGrade();
     showGrade();
    
    return (0);
    
    }
    
    int getScore()
    {
    int numGrade;
    
    printf ("Please enter your numeric grade> ");
    scanf ("%d", &numGrade);
    return numGrade;
    
    }
    
    
    
    char convertGrade(int numGrade)
    
    {
    char lettergrade;
    
    
    if  ((numGrade >= 90) && (numGrade <= 100))
    {	lettergrade = 'A';
    		return lettergrade;
    }
    
    else if ((numGrade >=80) && (numGrade <=89))
    {	
    lettergrade = 'B';
    return lettergrade;
    }
    
    else if ((numGrade >=70) && (numGrade <=79))
    {
    lettergrade = 'C';
    return lettergrade;	
    }
    
    
    else if ((numGrade >=60) && (numGrade <=79))
    {		
    lettergrade = 'D';
    return lettergrade;
    }
    
    else if ((numGrade >=59) && (numGrade <=0))
    {	
    lettergrade = 'F';
    return lettergrade;
    }
    
    else if ((numGrade >=100) || (numGrade <=0))
    {	
    lettergrade = 'Z';
    return lettergrade;
    }
    
    }
    
    void showGrade(int numGrade, char lettergrade)
    {
    if (lettergrade==Z)
         printf ("Your score of %d is invalid grade", numGrade);
    else
         printf ("Your score of %d is &lf grade", numGrade, lettergrade);
    }
    so will this be working fine?
    I practice C using my school server(using PuTTy). They are down. I won't be able to test it till tomorrow. Can someone tell me if there is a thing going to give me an error?
    or can someone tell me how else i can check my code online or on windows?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you should work on your indentetion - choose some editor that will do it for you, for example VS can do it when pressing Alt+F8 on selected text

    2. Here are some compilation errors:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getScore();
    char convertGrade();
    void showGrade(int numGrade, char lettergrade);
    
    
    int main(void)
    {
    	int numGrade;
    	char letterGrade;
    
    	numGrade=getScore();
    	convertGrade();
    	lettergrade=convertGrade();//Error	1	error C2065: 'lettergrade' : undeclared identifier
    
    	showGrade(); //Error	2	error C2198: 'showGrade' : too few arguments for call	
    
    
    	return (0);
    
    }
    
    int getScore()
    {
    	int numGrade;
    
    	printf ("Please enter your numeric grade> ");
    	scanf ("&#37;d", &numGrade);
    	return numGrade;
    
    }
    
    
    
    char convertGrade(int numGrade)
    
    {
    	char lettergrade;
    
    
    	if  ((numGrade >= 90) && (numGrade <= 100))
    	{	lettergrade = 'A';
    	return lettergrade;
    	}
    
    	else if ((numGrade >=80) && (numGrade <=89))
    	{	
    		lettergrade = 'B';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=70) && (numGrade <=79))
    	{
    		lettergrade = 'C';
    		return lettergrade;	
    	}
    
    
    	else if ((numGrade >=60) && (numGrade <=79))
    	{		
    		lettergrade = 'D';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=59) && (numGrade <=0))
    	{	
    		lettergrade = 'F';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=100) || (numGrade <=0))
    	{	
    		lettergrade = 'Z';
    		return lettergrade;
    	}
    
    }
    
    void showGrade(int numGrade, char lettergrade)
    {
    	if (lettergrade==Z) //Error	3	error C2065: 'Z' : undeclared identifier
    
    		printf ("Your score of %d is invalid grade", numGrade);
    	else
    		printf ("Your score of %d is &lf grade", numGrade, lettergrade);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    how do i fix them?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Error 1 - fix the name of the variable to match (including case) to the declared one
    Error 2 - pass correct argumants to the function
    Error 3
    look at the code
    Code:
    else if ((numGrade >=100) || (numGrade <=0))
    	{	
    		lettergrade = 'Z';
    		return lettergrade;
    	}
    And try to figure out the difference

    Also pay attantion to your format
    Code:
    "Your score of &#37;d is &lf grade"
    You do not have a %c for printing lettergrade in it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getScore();
    char convertGrade();
    void showGrade(int numGrade, char lettergrade);
    
    
    int main(void)
    {
    	int numGrade;
    	char lettergrade;
    
    	numGrade=getScore();
    	convertGrade();
    	lettergrade=convertGrade();
    	showGrade(int numGrade, char lettergrade);
    
    
    
    	return (0);
    
    }
    
    int getScore()
    {
    	int numGrade;
    
    	printf ("Please enter your numeric grade> ");
    	scanf ("%d", &numGrade);
    	return numGrade;
    
    }
    
    
    
    char convertGrade(int numGrade)
    
    {
    	char lettergrade;
    
    
    	if  ((numGrade >= 90) && (numGrade <= 100))
    	{	lettergrade = 'A';
    	return lettergrade;
    	}
    
    	else if ((numGrade >=80) && (numGrade <=89))
    	{	
    		lettergrade = 'B';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=70) && (numGrade <=79))
    	{
    		lettergrade = 'C';
    		return lettergrade;	
    	}
    
    
    	else if ((numGrade >=60) && (numGrade <=79))
    	{		
    		lettergrade = 'D';
    		return lettergrade;
    	}
    
    	else if ((numGrade <=59) && (numGrade >=0))
    	{	
    		lettergrade = 'F';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=100) || (numGrade <=0))
    	{	
    		lettergrade = 'Z';
    		return lettergrade;
    	}
    
    }
    
    void showGrade(int numGrade, char lettergrade)
    {
    	if (lettergrade==Z)
    
    		printf ("Your score of %d is invalid grade", numGrade);
    	else
    		printf ("Your score of %d is %c grade", numGrade, lettergrade);
    }
    this should fix 1st, 3rd errors. not sure about 2nd. can you check or try to compile now?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    showGrade(int numGrade, char lettergrade);
    this is declaration, not a call

    call function:
    Code:
    showGrade(numGrade, lettergrade);

    About 3rd error - you fixed format, but did nothing about Z variable. It should be 'Z' constant

    also in your last
    Code:
    else if ((numGrade >=100) || (numGrade <=0))
    you can jsut drop the if part leavin it as
    Code:
    else
    {	
    	lettergrade = 'Z';
    	return lettergrade;
    }
    Or even without else (and without temp var):

    Code:
    char convertGrade(int numGrade)
    {
    
    	if  ((numGrade >= 90) && (numGrade <= 100))
    	{	
    		return 'A';
    	}
    	else if ((numGrade >=80) && (numGrade <=89))
    	{	
    		return 'B';
    	}
    	else if ((numGrade >=70) && (numGrade <=79))
    	{
    		return 'C';	
    	}
    	else if ((numGrade >=60) && (numGrade <=79))
    	{		
    		return 'D';
    	}
    	else if ((numGrade <=59) && (numGrade >=0))
    	{	
    		return 'F';
    	}
    
    	/* incorrect grade */
    	return 'Z';
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getScore();
    char convertGrade();
    void showGrade(int numGrade, char lettergrade);
    
    
    int main(void)
    {
    	int numGrade;
    	char lettergrade;
    
    	numGrade=getScore();
    	convertGrade();
    	lettergrade=convertGrade();
    	showGrade(numGrade, lettergrade);
    
    	return (0);
    
    }
    
    int getScore()
    {
    	int numGrade;
    
    	printf ("Please enter your numeric grade> ");
    	scanf ("%d", &numGrade);
    	return numGrade;
    
    }
    
    
    
    char convertGrade(int numGrade)
    {
    	char lettergrade;
    
    
    	if  ((numGrade >= 90) && (numGrade <= 100))
    	{	lettergrade = 'A';
    	return lettergrade;
    	}
    
    	else if ((numGrade >=80) && (numGrade <=89))
    	{	
    		lettergrade = 'B';
    		return lettergrade;
    	}
    
    	else if ((numGrade >=70) && (numGrade <=79))
    	{
    		lettergrade = 'C';
    		return lettergrade;	
    	}
    
    
    	else if ((numGrade >=60) && (numGrade <=79))
    	{		
    		lettergrade = 'D';
    		return lettergrade;
    	}
    
    	else if ((numGrade <=59) && (numGrade >=0))
    	{	
    		lettergrade = 'F';
    		return lettergrade;
    	}
    
    	else
    	{	
    		lettergrade = 'Z';
    		return lettergrade;
    	}
    
    }
    
    void showGrade(int numGrade, char lettergrade)
    {
    	if (lettergrade=='Z')
    
    		printf ("Your score of %d is invalid grade", numGrade);
    	else
    		printf ("Your score of %d is %c grade", numGrade, lettergrade);
    }
    how about now?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    look at this:
    Code:
    char convertGrade();
    char convertGrade(int numGrade)
    Notice the difference?
    The place where you call the fucntion should be also fixed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with virtual function calls, please help
    By e66n06 in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2007, 05:12 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM