Thread: Function Problem

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Got it to work!! Thanks for the help

    Code:
    #include <stdio.h>
    
    /* Function Prototypes */
    
    int getScore();
    char convertGrade(int numScore);
    void showGrade(int numScore, char letterGrade);
    
    int main()
    {
    
            int numScore;
            int letterGrade;
    
            getScore();
            convertGrade(numScore);
            showGrade(numScore, letterGrade);
    
            return (0);
    }
    
    int getScore()
    {
            int numScore;                                           /*Numeric exam score*/
    
                                                                    /* User entry of numeric exam score*/
            printf("Please enter your numeric exam score and press return> \n");
            scanf("%d",&numScore);
    
            return(numScore);
    }
    
    char convertGrade(int numScore)                                  /* Converts numScore number grade into a letter grade */
    {
            if (numScore >=90 && numScore <=100)
                    showGrade(numScore,'A');
    
                     else if (numScore >=80 && numScore <=89)
                            showGrade(numScore,'B');
    
                    else if (numScore >=70 && numScore <=79)
                             showGrade(numScore,'C');
    
                    else if(numScore >=60 && numScore <=69)
                            showGrade(numScore,'D');
    
                    else if (numScore >= 0 && numScore <= 59)
                             showGrade(numScore,'F');
                    else
                           showGrade(numScore,'Z');
    
    return(0);
    }
    
    void showGrade(int numScore, char letterGrade)
    {
            if (letterGrade=='Z')
                    printf("%d is not a valid number. Please enter a number between 0 and 100.\n", numScore);
    
    else
                      printf("Your original numeric score of %d is a(n) %c grade.\n",numScore,letterGrade);
    }
    storm:~/lab2>a.out
    Please enter your numeric exam score and press return>
    78
    Your score of 78 is a C grade.

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Got it to work!! Thanks for the help
    I do not beleive you. You ignoring the return value of getScore and convertScore functions and the not initialized variables
    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

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by vart View Post
    I do not beleive you. You ignoring the return value of getScore and convertScore functions and the not initialized variables
    Your completely right. Our lab assignment was to write the program normally and get it to function properly. Then we had to re-write it with functions. So when we ran it i accidentally ran the first version, the old working version without functions, but not the new version that included functions. I finally got it to work though. It was a simple fix having to do with the main function. Cant believe i spent 4 hours and didn't realize the problem.

    And here it is for anyone interested in how it looks completed.
    Code:
    /* Ryan Houlihan   
       Lab 2
    
       The purpose of this program is to use functions to convert a number between 0 and 100 to a
       letter grade of either A, B, C, D, or F.
    */
    
    #include <stdio.h>
    
    int getScore();                                         /*Declaring the first function */
    char convertGrade(int numScore);                        /*Declaring the second function*/
    void showGrade(int numScore, char letterGrade);         /*Declaring the third function*/
                             
    int main()                                              /*Calls on the other 3 functions*/
    {
    
            int numScore;
            int letterGrade;
    
            numScore = getScore();
            
            letterGrade = convertGrade(numScore);
                    
            return (0);
    }
            
    int getScore()                                          /*Asks users to input a number*/
    {
            int numScore;
    
            printf("Please enter your numeric exam score and press return> \n");
            scanf("%d",&numScore);
     
            return(numScore);
    }
     
    char convertGrade(int numScore)                         /*Converts inputed number into a letter*/
    {
            if (numScore >=90 && numScore <=100)
                    showGrade(numScore,'A');
                            
                    else if (numScore >=80 && numScore <=89)
                            showGrade(numScore,'B');
            
                    else if (numScore >=70 && numScore <=79)
                             showGrade(numScore,'C');
    
                    else if(numScore >=60 && numScore <=69)
                   
                    else if (numScore >= 0 && numScore <= 59)
                             showGrade(numScore,'F');
     
                             else
                                     showGrade(numScore,'Z');
    
            return(0);
    }
    
    void showGrade(int numScore, char letterGrade)          /*Prints out the letter coresponding to the inputed number*/
    {
            if (letterGrade=='Z')
                    printf("%d is not a valid number. Please enter a number between 0 and 100.\n", numScore);
    
                    else
                            printf("Your original numeric score of %d is a(n) %c grade.\n",numScore,letterGrade);
    }
    Last edited by rhouli67; 02-14-2009 at 02:44 PM.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should not call showGrade from convertGrade function

    you are messing the main functionality and report functionality in this way.

    return the caonverted grade to main

    and call ShowGrade from main
    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

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by vart View Post
    you should not call showGrade from convertGrade function

    you are messing the main functionality and report functionality in this way.

    return the caonverted grade to main

    and call ShowGrade from main
    Thanks for the advice. Ill be sure to change that.

  6. #21
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Sorry. I answered to an old post here :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM