Thread: Function Problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    20

    Function Problem

    Hi im a freshman college student in C programming. We are learning how to use functions. For some reason no one in my Lab can get it to work and im the closest. Here is my code:

    Code:
    /*
    Ryan Houlihan
    Lab2
    The goal of this program is to read in a number between 0 and 100 and
    print it out as a letter grade.
    */
    
    #include <stdio.h>
    
    int getScore();                                                         /* Function that converts number score to a letter grade */
    char convertGrade(int numScore);                                        /* Function that converts inputed number grade to a letter grade */
    void showGrade(int numScore, char letterGrade);                         /* Function that prints out the letter grade */
    
    int main()                                                               /* Calls the other three functions */
    {
            getScore();
    }
            int getScore()                                                   /* Asks user to input a grade form 0-100 8/
            {
                    int numScore;
    
                    printf("Please enter a grade from 0-100:\n ");
                    scanf("%d", &numScore);
    
                    return(numScore);
            }
    
            char convertGrade(int numScore)                                  /* Converts number score to a letter grade */
            {
    
                    int numScore;
    
                    if (numScore >= 0 &&  numScore <= 59)
                            showGrade(numScore,'F');
    
                            else if (numScore >= 60 && numScore <= 69)
                                    showGrade(numScore,'D');
    
                            else if (numScore >= 70 &&  numScore <= 79)
                                    showGrade(numScore,'C');
    
                            else if (numScore >= 80 && numScore <= 89)
                                    showGrade(numScore, 'B');
    
                            else if (numScore >= 90 &&  numScore <= 100)
                                    showGrade(numScore, 'A');
    
                            else
                                    showGrade(numScore, 'Z');
    
                    return(0);
            }
    
            void showGrade(int numScore, char letterGrade)                            /* Prints out the letter grade */
            {
                    if(letterGrade == 'Z')
                            printf("%d is not a vaild number. Please enter a number between 0 and 100\n", numScore);
                    else
                            printf("Your grade, %d, is equal to an %c\n", numScore, letterGrade);
    
            }
    It complies fine but it just prints this. :

    -1074170612 is not a vaild number. Please enter a number between 0 and 100

    Any help is appreciated. I have been trying to figure this out for the past 3 hours with no luck. Ugh.
    Last edited by rhouli67; 02-13-2009 at 02:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The code you posted cannot produce the output you claim, since the only function that gets called is getScore. convertScore and showGrade are not called.

    (Also note that "if" requires "==" as opposed to "=".)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    From what I see I don't understand how it prints anything out... in main you only call getscore(). convertgrade (and therefore showgrade) are never called.

    Secondly in showGrade you should have if (letterGrade == 'z'). YOu only have one '='.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    wolverine:~>a.out
    -1078499092 is not a vaild number. Please enter a number between 0 and 100
    wolverine:~>a.out
    -1080232116 is not a vaild number. Please enter a number between 0 and 100
    wolverine:~>a.out
    -1077909124 is not a vaild number. Please enter a number between 0 and 100
    wolverine:~>a.out
    -1076233300 is not a vaild number. Please enter a number between 0 and 100
    wolverine:~>a.out
    -1080088724 is not a vaild number. Please enter a number between 0 and 100
    wolverine:~>a.out
    -1079091108 is not a vaild number. Please enter a number between 0 and 100

    And yes im confused as well. ><

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    in main you should put

    convertScore(getScore());

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    convertGrade redeclares numScore but doesn't initialize it. you shouldn't redeclare it at all because it's passed as a parameter.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by Bladactania View Post
    in main you should put

    convertScore(getScore());
    Im assuming you mean convertGrade(getScore());

    and i did and tried to complie and....:

    wolverine:~>gcc gradelab.c
    /tmp/ccM4cwjx.o: In function `main':
    gradelab.c.text+0x1a): undefined reference to `convertGrade'
    collect2: ld returned 1 exit status
    wolverine:~>

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by Meldreth View Post
    convertGrade redeclares numScore but doesn't initialize it. you shouldn't redeclare it at all because it's passed as a parameter.
    I did this with everything else the same:

    wolverine:~>gcc gradelab.c
    gradelab.c: In function 'getScore':
    gradelab.c:30: error: 'numScore' undeclared (first use in this function)
    gradelab.c:30: error: (Each undeclared identifier is reported only once
    gradelab.c:30: error: for each function it appears in.)

    And no i didnt remove int numScore; from getScore() function just from the convertGrad(int numScore) function

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yeah, my bad... convertGrade

    why does convertGrade return a char? it should be

    void convertGrade( yadda yadda)

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    ConvertGrade is returning char letterGrade to be used in the showGrade function.
    Last edited by rhouli67; 02-13-2009 at 03:19 PM.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    ok... try this...

    Code:
    #include <stdio.h>
    
    int getScore(void);
    char convertScore2Grade(int);
    void showGrade(char);
    
    int main() {
      int numScore = 0;
      char letterGrade = 'Z';
    
      numScore = getScore();
      letterGrade = convertScore2Grade(numScore);
      showGrade(LetterGrade);
    
      return 0;
    }
    
    int getGrade() {
      int numScore = 0;
    
      printf("Enter a number between 0 and 100:");
      scanf("%d", numScore);
    
      return numScore;
    }
    
    char convertScore2Grade(int numScore) {
      char letterGrade = 'Z';
    
      if (numScore >= 0 &&  numScore <= 59) letterGrade = 'F';
      else if (numScore >= 60 && numScore <= 69) letterGrade = 'D';
      else if (numScore >= 70 &&  numScore <= 79) letterGrade = 'C';
      else if (numScore >= 80 && numScore <= 89) letterGrade = 'B';
      else if (numScore >= 90 &&  numScore <= 100) letterGrade = 'A';
    
      // No else needed since we set the initial value
    
      return letterGrade;
    }
    
    void showGrade(char letterGrade) {
      if (letterGrade == 'Z')  printf("%d is not a vaild number. Please enter a number between 0 and 100\n", numScore);
      else printf("Your score, %d, is equal to an %c\n", numScore, letterGrade);
    }
    I haven't compiled that so...

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    my bad... change showGrade to the following

    Code:
    void showGrade(int numScore, char letterGrade) {
      if (letterGrade == 'Z')  printf("%d is not a vaild number. Please enter a number between 0 and 100\n", numScore);
      else printf("Your score, %d, is equal to an %c\n", numScore, letterGrade);
    }
    you will need to change the function prototype to

    void showGrade(int, char);

  13. #13
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Your main function should return a value. Pay more attention when typing:

    Code:
    /* Asks user to input a grade form 0-100 8/
    It should be an * instead of the 8.

    convertGrade function does not return a char! it return an int... but you can declare it void.

    void showGrade(int numScore, char letterGrade) expects a char ("letter") not the ascii number of the letter ('letter').
    I have stopped reading Stephen King novels. Now I just read C code instead.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    good catch ralu... that would be why he's getting the output he's getting.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Changed it around a little cause it wouldnt complie and it still wont:

    wolverine:~>gcc gradelab2.c
    /tmp/ccIa6DPd.o: In function `main':
    gradelab2.c.text+0x1d): undefined reference to `getScore'
    collect2: ld returned 1 exit status
    wolverine:~>

    Code:
    #include <stdio.h>
    
    int getScore(void);
    char convertScore2Grade(int);
    void showGrade(char, int);
    
    int main() {
      int numScore = 0;
      char letterGrade = 'Z';
    
      numScore = getScore();
      letterGrade = convertScore2Grade(numScore);
      showGrade(letterGrade, numScore);
    
      return 0;
    }
    
    int getGrade() {
      int numScore = 0;
    
      printf("Enter a number between 0 and 100:");
      scanf("%d", numScore);
    
      return numScore;
    }
    
    char convertScore2Grade(int numScore) {
      char letterGrade = 'Z';
    
      if (numScore >= 0 &&  numScore <= 59) letterGrade = 'F';
      else if (numScore >= 60 && numScore <= 69) letterGrade = 'D';
      else if (numScore >= 70 &&  numScore <= 79) letterGrade = 'C';
      else if (numScore >= 80 && numScore <= 89) letterGrade = 'B';
      else if (numScore >= 90 &&  numScore <= 100) letterGrade = 'A';
    
      // No else needed since we set the initial value
    
      return letterGrade;
    }
    
    void showGrade(char letterGrade, int numScore) {
      if (letterGrade == 'Z')
             printf("%d is not a vaild number. Please enter a number between 0 and 100\n", numScore);
      else
             printf("Your score, %d, is equal to an %c\n", numScore, letterGrade);
    }

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