Thread: Help me please with functions.

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

    Help me please with functions.

    Ok I am trying to make it look like this but with functions and I am not sure I have this right.

    This is my code that I have and not sure if right. Could y'all help?

    Code:
    #include <iostream>
    
    void Lab10identification() {
    cout << "Section" << endl
         << "Student: Name email"
         << endl << endl;
           }
    
    
    char getletterGrade (int gradeIn) {
    cout << "Enter a numeric grade (0-100, -1 to stop): ";
    cin << gradeIn; 
    return gradeIn;
     if (0 > gradeIn && 100 < gradeIn)
     cout << "The grade is out of range; try again." << endl;   
      }
      
    if (gradeIn >=0 && gradeIn <= 59) 
     letterGrade = 'F';
    if (gradeIn >=60 && gradeIn <= 69)
     letterGrade = 'D';
    if (gradeIn >=70 && gradeIn <= 79)
     letterGrade = 'C';
    if (gradeIn >=80 && gradeIn <= 89)
     letterGrade = 'B';
    if (gradeIn >=90 && gradeIn <= 100)
     letterGrade = 'A';
     
     
     
     return LetterGrade;
    
      
    int main() {
      int grade;
      char letterGrade;
      
      Lab10identification();
      
      do {
        grade = getNumericGrade();
        if (grade != -1) {
          letterGrade = getLetterGrade(grade);
          cout << "The student's letter grade is: " << letterGrade << endl;
          }
        } while (grade != -1);
      
      cout << "Good-bye" << endl;
      return 0;
    }
    But this is what is printing out. It is errors.

    Code:
     
     #include <iostream>
     
     void Lab10identification() {
     cout << "section" << endl
          << "Student: Name email"
          << endl << endl;
            }
     
     
     char getletterGrade (int gradeIn) {
     cout << "Enter a numeric grade (0-100, -1 to stop): ";
     cin << gradeIn; 
        no match for `webistream & << int &'
     return gradeIn;
      if (0 > gradeIn && 100 < gradeIn)
      cout << "The grade is out of range; try again." << endl;   
       }
       
     if (gradeIn >=0 && gradeIn <= 59) 
       parse error before `if'
      letterGrade = 'F';
     if (gradeIn >=60 && gradeIn <= 69)
      letterGrade = 'D';
     if (gradeIn >=70 && gradeIn <= 79)
      letterGrade = 'C';
     if (gradeIn >=80 && gradeIn <= 89)
      letterGrade = 'B';
     if (gradeIn >=90 && gradeIn <= 100)
      letterGrade = 'A';
      
      
      
      return LetterGrade;
     
       
     int main() {
       int grade;
       char letterGrade;
       
       Lab10identification();
       
       do {
         grade = getNumericGrade();
         implicit declaration of function `int getNumericGrade(...)'
         if (grade != -1) {
          letterGrade = getLetterGrade(grade);
         implicit declaration of function `int getLetterGrade(...)'
           cout << "The student's letter grade is: " << letterGrade << endl;
           }
         } while (grade != -1);
      
       cout << "Good-bye" << endl;
       return 0;
     }
    I am trying to make it look like this:

    Code:
    Section Name email
    
    Enter a numeric grade (0-100, -1 to stop): 120
    The grade is out of range; try again.
    
    Enter a numeric grade (0-100, -1 to stop): -6
    The grade is out of range; try again.
    
    Enter a numeric grade (0-100, -1 to stop): 84
    The letter grade is B
    
    Enter a numeric grade (0-100, -1 to stop): 77
    The letter grade is C
    
    Enter a numeric grade (0-100, -1 to stop): -1
    Good-bye

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Hi. I reworked some of your code for you because you had some of your logic a little screwy. I'll try to explain some of them.
    Code:
    #include <iostream>
    //when using cout/cin/endl, you must either specify you are using this namespace
    //or you must prepend std:: to each of them
    using namespace std;
    
    void Lab10identification (void) 
    {
        cout << "Section" << endl << "Student: Name email" << endl << endl;
    }
    //You never specified this function in your previous code, but had it 
    //mixed up with getLetterGrade
    int getNumericGrade (void) 
    {
        int gradeIn;
    
        cout << "Enter a numeric grade (0-100, -1 to stop): ";
        //for cin, make sure you reverse the operators to go the other way
        cin >> gradeIn;
        //check here if the input abides by your rules (0-100)
        //if it doesn't, loop until it does
    
        return gradeIn;
    }
    
    char getLetterGrade (int grade)
    {
        char lettergrade;
    
        //Put all of your checking in here.
    
         return lettergrade;
    }
    
    int main (void) 
    {
        int grade;
        char letterGrade;
      
        Lab10identification();
      
        do 
        {
            grade = getNumericGrade();
            if (grade != -1) 
            {
                letterGrade = getLetterGrade(grade);
          	    cout << "The student's letter grade is: " << letterGrade << endl;
            }
        } 
        while (grade != -1);
      
        cout << "Good-bye" << endl;
        return 0;
    }
    To be honest, it seems you have a few basics you need to keep working on. In your previous code, you have some random code completely out of any scope, which obviously won't do you any good, or compile. Take a look at the green blocks of text to see where you went wrong and what type of stuff to add.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      if (0 > gradeIn && 100 < gradeIn)
      cout << "The grade is out of range; try again." << endl;   
       
     if (gradeIn >=0 && gradeIn <= 59) 
      letterGrade = 'F';
     if (gradeIn >=60 && gradeIn <= 69)
      letterGrade = 'D';
     if (gradeIn >=70 && gradeIn <= 79)
      letterGrade = 'C';
     if (gradeIn >=80 && gradeIn <= 89)
      letterGrade = 'B';
     if (gradeIn >=90 && gradeIn <= 100)
      letterGrade = 'A';
    The first if-statement should prevent the next set of code from being executed, e.g by returning as a result of the bad input (or, if you do what CarrotCake suggest, and check the input before passing it on - which I think is a better suggestion).

    If we have the above in place, your if-statements can be chained together, and made into ONE comparison, rather than two -
    Code:
    if (grade <= 59) letterGrade = 'F';
    else if (grade <= 69) letterGrade = 'D'; 
    ....
    Not only does this make the code a bit easier to read, but it also doesn't do a bunch of comparisons that will NEVER be true, since you have already determined the facts before you got there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM