# Thread: Need Help understanding arrays and pointers

1. ## 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.
This is what is asked:
Design and run a program that takes a numerical score and outputs a letter grade. Specific numerical scores and letter grades are listed below:
90-100 = Grade A
80-89 = Grade B
70-79 = Grade C
60-69 = Grade D
0-59 = Grade F
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 getScore (int&)
{
std::cout << "Please input the grade score: ";
std::cin >> score;
std::cout << "You entered the score of"<<score<<endl;
return;
}
{
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();
return 0;
}
Thanks for your help.

2. 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.

3. Daved,
Thanks. I'll try that.
BTW, this has nothing to do with arrays or pointers.
funny since this is given after the array & pointer chapter reading

4. 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;

void getScore();

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;
}
{

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);
return 0;
}

5. 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.

6. nickodonnell ,

Thanks! Good idea but I just tried that but i still get the same result of F.
worth a try though.

7. if the teacher says to use a reference, use a reference, not a pointer, they are not the same thing.

8. 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.

9. rockytriton. That helps me really ! Thanks.

10. 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.

11. 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;
}
{
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();
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.
Thanks yfor your help.

12. 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);

13. Thanks rockytriton!
It's working.....

Popular pages Recent additions