# Need Help understanding arrays and pointers

• 10-04-2005
flicka
Need Help understanding arrays and pointers
Hi guys! I have this homework to do and I am not sure how to get this done, so if anyone could help it would be great.
Design and run a program that takes a numerical score and outputs a letter grade. Specific numerical scores and letter grades are listed below:
In this program, create 2 void functions titled getScore and printGrade with an int argument. The function getScore should have a Reference parameter and printGrade should have a Value parameter.

The function getScore will prompt the user for the numerical score, get the input from the user, and print the numerical score. The function printGrade will calculate the course grade and print the course grade. (Be careful and note that the assignment requires you to input the grade into getScore and not directly into the main function.)

This is what I did so far. It compiles without giving me any error but the output stays blank.
Code:

#include "stdafx.h" #include <iostream> #include <stdlib.h> using std::cout; using std::cin; using std::endl; using namespace std; int score; void getScore(); void printGrades(); void getScore (int&) {  std::cout << "Please input the grade score: ";         std::cin >> score;         std::cout << "You entered the score of"<<score<<endl;                 return; } void printGrades(int ) {     if (score >= 0 && score <= 59)     {         std::cout << "The grade is an F.";     }     else if (score >= 60 && score <= 69)     {         std::cout << "The grade is a D.";     }     else if (score >= 70 && score <= 79)     {         std::cout << "The grade is a C.";     }     else if (score >= 80 && score <= 89)     {         std::cout << "The grade is a B.";     }     else if (score >= 90 && score <= 100)     {            std::cout << "The grade is an A.";     } return;} int main() { void getScore(); void printGrades();  return 0; }
• 10-04-2005
Daved
Instead of making score a global variable, make it a local variable inside main and pass it to your functions. You should then name the variable parameters that your functions use. Everything else looks ok.

BTW, this has nothing to do with arrays or pointers.
• 10-04-2005
flicka
Daved,
Thanks. I'll try that.
Quote:

BTW, this has nothing to do with arrays or pointers.
funny since this is given after the array & pointer chapter reading
:D :rolleyes:
• 10-04-2005
flicka
Ok I have made some changes to the program but I still have a problem.
I get the first function working fine but the second function gives a grade of F whatever the score I enter. Something is definitely not done \right, but don't know what. Could anyone help me point out where I'm making a mistake.
Code:

#include "stdafx.h" #include <iostream> #include <stdlib.h> using std::cout; using std::cin; using std::endl; using namespace std; int score; int *grade; void getScore(); void printGrades(); int getScore (int score) { std::cout << "Please input the grade score: ";           std::cin >> score;         std::cout << "You entered the score of "<<score<<endl;                 return score; } void printGrades(int *grade) {     grade = &score;     if (*grade >= 0 && *grade <= 59)     {         std::cout << "The grade is an F.";     }     else if (*grade >= 60 && *grade <= 69)     {         std::cout << "The grade is a D.";     }     else if (*grade >= 70 && *grade <= 79)     {         std::cout << "The grade is a C.";     }     else if (*grade >= 80 && *grade <= 89)     {         std::cout << "The grade is a B.";     }     else if (*grade >= 90 && *grade <= 100)     {            std::cout << "The grade is an A.";     } return;} int main() { getScore(score); printGrades(grade);  return 0; }
• 10-04-2005
nickodonnell
I'm not entirely sure, but change
Code:

if (*grade >= 0 && *grade <= 59)     {         std::cout << "The grade is an F.";     }
to
Code:

if (((*grade) >= 0) && ((*grade) <= 59)))     {         cout << "The grade is an F.";     }
and see if that helps.

Edit: Oh, and the same for D-A as well.
• 10-04-2005
flicka
nickodonnell ,

Thanks! Good idea but I just tried that but i still get the same result of F.
worth a try though.
• 10-04-2005
rockytriton
if the teacher says to use a reference, use a reference, not a pointer, they are not the same thing.
• 10-04-2005
rockytriton
by the way, the problem is that you have a local variable in your getScore function named score, so when you use it in cin, that's where it gets stored, in the local variable, not in the global. Get rid of the global variable. Use a reference to int in your getScore function, and use an int value as a parameter to your other function, that's what makes sense.
• 10-04-2005
flicka
rockytriton. That helps me really ! Thanks.
• 10-04-2005
rockytriton
and make getScore() return void because since you are using a reference to return the value, you don't need to return it as a return value.
• 10-04-2005
flicka
Well I must be really dumb because even with all your help, I'm not getting this right.
I took out the global variable, put a local one. But I'm still not understanding how to pass this variable from one function to another.
Here's what I did:
Code:

void getScore(int& score) { std::cout << "Please input the grade score: ";           std::cin >> score;                   score= score + score;         std::cout << "You entered the score of "<<score<<endl;                 } void printGrades(int score) {         if (score >= 0 && score <= 59)     {         std::cout << "The grade is an F.";     }     else if (score >= 60 && score <= 69)     {         std::cout << "The grade is a D.";     }     else if (score >= 70 && score <= 79)     {         std::cout << "The grade is a C.";     }     else if (score >= 80 && score <= 89)     {         std::cout << "The grade is a B.";     }     else if (score >= 90 && score <= 100)     {            std::cout << "The grade is an A.";     } } int main() { int score; score = 0; getScore(); printGrades(int score);  return 0; }
And these are the error messages I'm getting:
Code:

(45) : error C2144: syntax error : 'void' should be preceded by ')' (45) : error C2660: 'getScore' : function does not take 0 arguments 45) : error C2059: syntax error : ')' (46) : error C2144: syntax error : 'int' should be preceded by ')' (46) : error C2660: 'printGrades' : function does not take 0 arguments (46) : error C2059: syntax error : ')'  - 6 error(s), 0 warning(s)
Is there a tutorial or something that could explain this to me clearly. I have seen a bunch of them but they always use main and not 2 functions.
• 10-04-2005
rockytriton
you forgot to pass score when you call getScore();

getScore(score);

and with printScore(); when you call it, you don't put the type in the call, it's just:

printScore(score);
• 10-04-2005
flicka
Thanks rockytriton!
It's working.....