return function

#include <iostream>
using namespace std;

int getScore(int test1, int test2, int test3, int test4, int test5)
{

cout << "Enter 5 test scores(Between 1 - 100): ";
cin >> test1 >> test2 >> test3 >> test4 >> test5;

}

int main ()
{
int test1, test2, test3, test4, test5;

for(int i = 0; i < 5; i++)
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();

}

return 0;
}

im suppose to write a int function with no parameters, to ask the user for a test score, validate it and return it. the function should be called by the main function once for each of the five scores entered. Not allowed to accept scores more than a 100 or less than 0. can any1 help me out and tell me where i went wrong? i dont really understand the return function. where can i improve or change?
Last edited on
Generally I think the outline is a bit off. It seems unnatural to me to ask the user for the score, validate and return the value all within the function. It seems more natural to ask the user for a value and then send that value to a function for validation. However, a spec is a spec and that's what you've been give so let's deal with it.

So you don't understand the return function?

Ok, return isn't a function. It's a keyword that will send whatever value it's used with back from the function that it's in.

Let's take a look at a function:
1
2
3
4
int Double(int x)
{
   return x * x;
}

That's a simple function that will double whatever value is passed into it (the parameter) and return it back from the function. So we'll break this down. The first int there is the return type. This lets the program know that this function should return an integer value. Double is the name of the function. Anything within the () operators are the parameters - the values that are passed in and used within the function. In our example, we're passing in an integer, which, within the scope of the function, will be named 'x'.

Functions don't need to take parameters. Nor do they need to return values. A function that doesn't take parameters will look like this:
1
2
3
4
5
6
7
8
9
10
11
int MyFunction()
{
   return 0;
}

// or

int MyFunction(void)
{
   return 0;
}


Down to your example. Your function there has parameters. Five of them to be exact. They shouldn't be there as per the program specification. The function calls are in line with the specification but don't match any of the functions you've written.

Here's a brief outline of how I'd go about doing this:
1
2
3
4
5
6
// Create an integer returning function, without parameters
// Create a variable in there to store the user input
// Get the input for the user
// If it's valid, return that value.  If not, return some error value.
// In main, create an array of integers for each test score
// Loop through the array, calling that function for each score 


Give it a go, see how it goes. If you get stuck implementing any of that, pop back with what you've got so far.
Last edited on
So if u say that a function with no parameters, will look like this? According to my function: int getScore()
{
Cout << "Enter 5 test scores (between 1 - 100) : ";
Cin >> test1 >> test2 >> test3 >> test4 >> test5;

Return (what eva needs to be returned);
}

Is that right? But what needs to be returned in my case? The test score? I really don't want u to right my function for me, that's the only thing I don't understand.
to ask the user for a test score, validate it and return it.
Last edited on
Hi
looks like we are sitting with the same problem, but how do I get the lowest of the 5 scores and in the findLowest func? Your help will be apreciated.
Topic archived. No new replies allowed.