# Help me please with functions.

• 04-15-2009
walkonwater
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```
• 04-16-2009
carrotcake1029
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.
• 04-16-2009
matsp
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