Thread: Another problem with a program

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    17

    Angry Another problem with a program

    I'm trying to make a program that will take a number grade and turn it into a letter grade. As I said in another post, this is my first day programming. Here is my program:

    Code:
    #include<stdio.h>
    #pragma warning(disable:4996)
    
    int getNumGrade ();
    int calcLetterGrade();
    int outputToScreen (int, int);
    
    int main (void)
    {
    int numgrade = 0;
    int lettergrade = 0;
    char cgoAgain = 'y';
    printf ("Enter numerical grade value now:");
    numgrade = getNumGrade ();
    lettergrade = calcLetterGrade();
    int ouputToScreen (numgrade, lettergrade);
    
    while (cgoAgain =='y' || cgoAgain == 'Y');
    {
    printf("\nEnter numerical grade value now:");
    numgrade = getNumGrade(numgrade);
    
    return 0;
    }
    
    int getNumGrade()
    {
    int numgrade = 0;
    scanf("%d%*c",&numgrade);
    return numgrade;
    }
    
    int calcLetterGrade (numgrade)
    {
    	int lettergrade;
    	if (numgrade < 60)
    		lettergrade = 'F';
    	if (numgrade <70)
    		lettergrade = 'D';
    	if(numgrade < 80)
    		lettergrade = 'C';
    	if (numgrade <90)
    		lettergrade = 'B';
    	if (numgrade <100)
    		lettergrade = 'A';
    	return lettergrade;
    }
    
    int outputToScreen (int numgrade, int lettergrade)
    {
    	printf ("\n When Numerical grade is %d, letter grade is %d", numgrade, lettergrade);
    }

    I get the following error:


    (16) : error C2143: syntax error : missing ';' before 'type'
    (26) : error C2143: syntax error : missing ';' before 'type'

    How can I fix those?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here is one of the problems:

    Code:
    while (cgoAgain =='y' || cgoAgain == 'Y');
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Thank you, I corrected that one. Where does the line 16 error come from, though?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, since you are eagerly learning, check out this piece of your code:
    Code:
    int calcLetterGrade (numgrade)
    {
    	int lettergrade;
    	if (numgrade < 60)
    		lettergrade = 'F';
    	if (numgrade <70)
    		lettergrade = 'D';
    	if(numgrade < 80)
    		lettergrade = 'C';
    	if (numgrade <90)
    		lettergrade = 'B';
    	if (numgrade <100)
    		lettergrade = 'A';
    	return lettergrade;
    }
    If the numgrade is 50, lettergrade will be set to 'F'.

    However, your logic will continue to check for numgrade being equal to other values. What can you do to your "if" ladder to make it more efficient?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You've defined a function...
    Code:
    int ouputToScreen (numgrade, lettergrade);
    in the middle of main()!
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Could I make else if statements?

    I tried that earlier, but it said they were illegal else statements.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    I thought I defined outputToScreen earlier in the prototype? Should it be

    outputToScreen (numgrade, lettergrade);

    in the prototype?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tristangreer View Post
    Could I make else if statements?

    I tried that earlier, but it said they were illegal else statements.
    Yes, that is what you should do. Try again and post if you get errors.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Is this any better or am I taking steps backward? I'll bold the changes.



    Code:
    #include<stdio.h>
    #pragma warning(disable:4996)
    
    int getNumGrade ();
    int calcLetterGrade();
    int outputToScreen (int, int);
    
    int main (void)
    {
    int numgrade = 0;
    int lettergrade = 0;
    char cgoAgain = 'y';
    printf ("Enter numerical grade value now:");
    numgrade = getNumGrade ();
    lettergrade = calcLetterGrade();
    ouputToScreen (numgrade, lettergrade);
    printf ("\nWould you like to enter another grade? ");
    
    while (cgoAgain =='y' || cgoAgain == 'Y')
    {
    printf("\nEnter numerical grade value now:");
    numgrade = getNumGrade(numgrade);
    }
    return 0;
    }
    
    int getNumGrade()
    {
    int numgrade = 0;
    scanf("&#37;d%*c",&numgrade);
    return numgrade;
    }
    
    int calcLetterGrade (numgrade)
    {
    	int lettergrade;
    	if (numgrade < 60)
    		lettergrade = 'F';
    	if (numgrade <70)
    		lettergrade = 'D';
    	if(numgrade < 80)
    		lettergrade = 'C';
    	if (numgrade <90)
    		lettergrade = 'B';
    	if (numgrade <100)
    		lettergrade = 'A';
    	return lettergrade;
    }
    
    int outputToScreen (int numgrade, int lettergrade)
    {
    	printf ("\n When Numerical grade is %d, letter grade is %d", numgrade, lettergrade);
    	return 0;
    }
    That is to attempt to fix the line 16 error, this is the new error code I get.

    1>(16) : warning C4013: 'ouputToScreen' undefined; assuming extern returning int
    1>Linking...
    1>Lab2a.obj : error LNK2019: unresolved external symbol _ouputToScreen referenced in function _main

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tristangreer View Post
    I thought I defined outputToScreen earlier in the prototype? Should it be

    outputToScreen (numgrade, lettergrade);

    in the prototype?
    You sure did. Get rid of the errant one in the middle of main().
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    1>(16) : warning C4013: 'ouputToScreen' undefined; assuming extern returning int
    1>Linking...
    1>Lab2a.obj : error LNK2019: unresolved external symbol _ouputToScreen referenced in function _main

    How do I fix this one? I'll make the else statements and see if they work.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Here's the code with the else statements, no errors on those:

    Code:
    #include<stdio.h>
    #pragma warning(disable:4996)
    
    int getNumGrade ();
    int calcLetterGrade();
    int outputToScreen (int, int);
    
    int main (void)
    {
    int numgrade = 0;
    int lettergrade = 0;
    char cgoAgain = 'y';
    printf ("Enter numerical grade value now:");
    numgrade = getNumGrade ();
    lettergrade = calcLetterGrade();
    ouputToScreen (numgrade, lettergrade);
    printf ("Would you like to enter another grade?");
    
    while (cgoAgain =='y' || cgoAgain == 'Y')
    {
    printf("\nEnter numerical grade value now:");
    numgrade = getNumGrade(numgrade);
    }
    return 0;
    }
    
    int getNumGrade()
    {
    int numgrade = 0;
    scanf("&#37;d%*c",&numgrade);
    return numgrade;
    }
    
    int calcLetterGrade (numgrade)
    {
    	int lettergrade;
    	if (numgrade < 60)
    		lettergrade = 'F';
    	else if (numgrade <70)
    		lettergrade = 'D';
    	else if(numgrade < 80)
    		lettergrade = 'C';
    	else if (numgrade <90)
    		lettergrade = 'B';
    	else if (numgrade <100)
    		lettergrade = 'A';
    	return lettergrade;
    }
    
    int outputToScreen (int numgrade, int lettergrade)
    {
    	printf ("\n When Numerical grade is %d, letter grade is %d", numgrade, lettergrade);
    	return 0;
    }
    Here's the error again:

    (16) : warning C4013: 'ouputToScreen' undefined; assuming extern returning int
    1>Linking...
    1>Lab2a.obj : error LNK2019: unresolved external symbol _ouputToScreen referenced in function _main

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You do seem to be doing well for a newbie
    Code:
    int ouputToScreen (numgrade, lettergrade);
    This is a function prototype.
    I'm not sure if you are trying to call the function or declare a prototype here?

    Code:
    while (cgoAgain =='y' || cgoAgain == 'Y');
    As others have pointed out, you must remove the ; because otherwise it's interpreted as a single-line while statement, which isn't what you want.

    Code:
    {
    printf("\nEnter numerical grade value now:");
    numgrade = getNumGrade(numgrade);
    You have a {, but no }, thus causing all sorts of loads of problems. Essentially, you're putting everything inside that while block. And you can't define a function inside a function.
    Indent properly and you'll quickly and easily find mis-matched brackets!

    http://cpwiki.sourceforge.net/Indentation
    http://cpwiki.sourceforge.net/User:Elysia/Indentation

    Code:
    int outputToScreen (int numgrade, int lettergrade)
    {
    	printf ("\n When Numerical grade is %d, letter grade is %d", numgrade, lettergrade);
    }
    Unfortunately, you're invoking undefined behavior here. You say the function returns an int, but you never return anything.
    Since you are returning nothing, you might as well make it return void - aka, nothing.

    And I believe I told you before, but... don't strip argument names from your function prototypes. If anything, they destroy a lot of things.
    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.

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    In the outputToScreen function, I'm wanting the program to write that information on screen. I'm not sure I understand where the information should be returned, if it should be returned at all.

    As far as the prototype goes, I'm wanting to declare the prototype. Is that the wrong way to do it?

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    It is saying outputToScreen must return a value, but where should it be returned, I'm totally confused on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM