# Thread: Homework: calculate test scores

1. ## Homework: calculate test scores

Hello all,
I'm in my first C++ class and in this chapter we are learning about functions. my assignment is to create a program that uses 3 functions and calls these functions in main. the functions are getScore, calcAverage and findLowest; they should be self explanatory.

void getScore should ask the user for a test score, store it in a reference parameter variable and validate it. this function should be called by main once for each of the five test score to be entered

void calcAverage should calculate and display the aver of the four highest scores. This function should be called just once by main, and should be passed the five scores.

int findLowest should find and return the lowest of the five scores passed to it. It should be called by calcAverage, which uses the function to determine which of the five scores to drop

below is my code. When i run it i get an

error LNK2019: unresolved external symbol "int __cdecl findLowest(int,int,int,int,int)" (?findLowest@@YAHHHHHH@Z) referenced in function "void __cdecl calcAverage(int,int,int,int,int)" (?calcAverage@@YAXHHHHH@Z)

Code:
```#include <iostream>
using namespace std;

void getScore(int&);
void calcAverage(int, int, int, int, int);
int findLowest(int, int, int, int, int);

int main()
{
int score1,score2,score3,score4,score5;
getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);
calcAverage(score1, score2, score3, score4, score5);

system("pause");
return 0;
}

void getScore(int &score)
{
cout << "Please enter a test score. ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Valid test scores range from 0 - 100.\n";
cout << "Please enter a test score. ";
cin >> score;
}
}

void calcAverage(int score1, int score2, int score3, int score4, int score5)
{
float average;

int low = findLowest(score1, score2, score3, score4, score5);
if ( low == score1 )
{average = static_cast<float> (( score2 + score3 + score4 + score5 ) / 4 );}
else if ( low == score2 )
{average = static_cast<float> (( score1 + score3 + score4 + score5 ) / 4 );}
else if ( low == score3 )
{average = static_cast<float> (( score1 + score2 + score4 + score5 ) / 4 );}
else if ( low == score4 )
{average = static_cast<float> (( score1 + score2 + score3 + score5 ) / 4 );}
else
{average = static_cast<float> (( score1 + score2 + score3 + score4 ) / 4 );}

cout << "The Average of the scores is: " << average << endl;
}

int findlowest( int score1, int score2, int score3, int score4, int score5 )
{
if ( score1 <= score2 && score1 <= score3 && score1 <= score4 && score1 <= score5 )
{return score1;}
else if ( score2 <= score1 && score2 <= score3 && score2 <= score4 && score2 <= score5 )
{return score2;}
else if ( score3 <= score1 && score1 <= score2 && score3 <= score4 && score3 <= score5 )
{return score3;}
else if ( score4 <= score1 && score4 <= score2 && score4 <= score3 && score4 <= score5 )
{return score4;}
else
{return score5;}
}```

2. Check you function name spellings. Remember C/C++ is case sensitive.

Jim

3. Originally Posted by Elysia
That article needs some work, thanks for posting it.

It's a documentation article that isn't about documentation.

Anyone had who reads the prototype will have no idea what kind of parameters it takes.
I strongly disagree. I think the stronger argument is that the purpose of arguments is not imparted.

The example is horrible.
Code:
`void Allocate(char lastName[], char firstName[], char middleName[], char* ptrName);`
PEBKAC! Whoever wrote this function this way just doesn't know what they're doing. The array syntax can communicate what's expected if the programmer uses it consistently, so I really don't see the use of types only as the problem.

Code:
```void Display (String* name);
void Display (String name[]);```
Part of the mystery here is what String could be. Parameter names aren't the silver bullet. Documentation is.

Some tools ... parses the prototypes to give additional information about the function the programmer is trying to call. Hence, when the names of the parameters are missing from the prototypes, IntelliSense cannot display what parameters the function takes, not being very helpful to the programmer.
This is the same argument as before so it should probably be gone. Intellisense does not stop working, it just has less to say.

Typically, when a function header changes, so must the prototype. A typical resolution to this problem is to copy the function head and paste it and use it as a prototype, as well. If then the programmer proceeds to strip the names of the parameters, that incurs extra work.
That doesn't count. Typing arguments never count, since typing is a massive part of our discipline. You can do the same things in a different order and the only work is required work.

The article doesn't mention probably the best thing about parameter names. They make documentation -- in the source file in comments or elsewhere -- easy to follow. No one wants to write "argument 1" or "argument 2" to explain their function.

4. I've made some minor changes. I don't know if they're to your taste or not.
Regarding the functions, yes, I know they're horrible, so if want to change them, give a better example.
And you know you can change the wiki yourself, yes? If you disagree with how it's written, feel free to update it yourself.

5. Originally Posted by jimblumberg
Check you function name spellings. Remember C/C++ is case sensitive.

Jim

thank you, that was it. Its good to know it was something simple; yet embarrassing at the same time... i should've seen that. Again, thanks for the help

6. I have another homework assignment i need help with. I'm asked to build a program that uses a function called isPrime. The function should do a primality check against a user given number. I'm in the process of writing out psuedo-code for this assignment and can't wrap my head around how to do the check. I know prime numbers can only be divisible by themselves and 1 only... but how do i write that into a function? it sounds inefficient to do a for loop but its really the only thing i can think of. any help would be greatly appreciated.

Thanks

7. There isn't really a better way. And pray that no one finds any, because then security we take for granted to day will be compromised.
Anyway, this page mentions some ways of doing it: How to Check if a Number Is Prime: 3 Methods - wikiHow

8. Originally Posted by Elysia
There isn't really a better way. And pray that no one finds any, because then security we take for granted to day will be compromised.
Anyway, this page mentions some ways of doing it: How to Check if a Number Is Prime: 3 Methods - wikiHow

Thank You Elysia for your quick reply. I guess I will be going with that then.

9. Hi Guys,

I have another homework assignment that lets the user play rock paper scissors with the computer. Please see my code; I had it running until i added in my winner function. I am getting an error saying i am running an unsafe bool combination. The compiler threw and error when i tried to make these string types and suggested i change the to bool... so i did and it still doesn't like it. Can someone advise what i did wrong?

Code:
```#include<iostream>
#include<ctime>
#include <string>
using namespace std;

void playerChoice();
void compChoice();
void winner(bool playerChoice, bool compChoice);

int main()
{
srand((unsigned)time(0));
cout<<"Let's Play Rock, Paper, Scissors.\n"
<<"You will be playing against the computer!\n"
<<"Enter R for rock, P for paper, and S for scissors!\n";
playerChoice();
compChoice();
system("pause");
return 0;
}

void playerChoice()
{
char playerChoice;
cin>>playerChoice;

switch(playerChoice)
{
case 'r': case 'R':
cout<<"Player: Rock"<<endl;
break;
case 'p': case 'P':
cout<<"Player: Paper"<<endl;
break;
case 's': case 'S':
cout<<"Player: Scissors"<<endl;
break;
default:
cout<<"Play option does not exist!"<<endl;
break;
}
}

void compChoice()
{
int compChoice;
compChoice = rand()%3;
if(compChoice == 1)
{cout<<"Computer: Rock"<<endl;}
else if(compChoice == 2)
{cout<<"Computer: Paper"<<endl;}
else if(compChoice == 3)
{cout<<"Computer: Scissors"<<endl;}
}

void  winner (bool playerChoice, bool compSelection)
{

if (playerChoice ="Rock")
{
if (compSelection = "Rock")
cout << "It's a tie!\n\n\n\n";
else if (compSelection = "Paper")
cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
else if (compSelection == "Scissors")
cout << "Rock beats scissors! You win!\n\n\n\n";
}

else if (playerChoice ="Paper")
{
if (compSelection = "Rock")
cout << "Paper beats rock! You win!\n\n\n\n";
else if (compSelection = "Paper")
cout << "It's a tie!\n\n\n\n";
else if (compSelection = "Scissors")
cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
}
else if (playerChoice ="Scissors")
{
if (compSelection = "Rock")
cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
else if (compSelection = "Paper")
cout << "Scissors beat paper! You win!\n\n\n\n";
else if (compSelection = "Scissors")
cout << "It's a tie!\n\n\n\n";
}
}```

10. Well, a bool can only be true of false (ie, it's a boolean). Yet, you treat it as if it were a string.
I'd look up enums here if I were you (pay special heed to enum class if your compiler supports it).
Oh, and you are using = instead of == for comparison.

11. Originally Posted by Elysia
Well, a bool can only be true of false (ie, it's a boolean). Yet, you treat it as if it were a string.
I'd look up enums here if I were you (pay special heed to enum class if your compiler supports it).
Oh, and you are using = instead of == for comparison.

Elysia,

you were correct, changing = to == fixed the error. while running I encountered a new error it says, no suitable constructor exists to convert void() to basic string()

12. Like I mentioned, you are comparing booleans to strings.

13. I'm sorry. I should've stated that i changed the function back to strings. see my updated code below.

Code:
```#include<iostream>
#include<ctime>
#include <string>
using namespace std;

void playerChoice();
void compChoice();
void winner(string playerChoice, string compChoice);

int main()
{
srand((unsigned)time(0));
cout<<"Let's Play Rock, Paper, Scissors.\n"
<<"You will be playing against the computer!\n"
<<"Enter R for rock, P for paper, and S for scissors!\n";
playerChoice();
compChoice();
winner(playerChoice, compChoice);
system("pause");
return 0;
}

void playerChoice()
{
char playerChoice;
cin>>playerChoice;

switch(playerChoice)
{
case 'r': case 'R':
cout<<"Player: Rock"<<endl;
break;
case 'p': case 'P':
cout<<"Player: Paper"<<endl;
break;
case 's': case 'S':
cout<<"Player: Scissors"<<endl;
break;
default:
cout<<"Play option does not exist!"<<endl;
break;
}
}

void compChoice()
{
int compChoice;
compChoice = rand()%3;
if(compChoice == 1)
{cout<<"Computer: Rock"<<endl;}
else if(compChoice == 2)
{cout<<"Computer: Paper"<<endl;}
else if(compChoice == 3)
{cout<<"Computer: Scissors"<<endl;}
}

void  winner (string playerChoice, string compChoice)
{

if (playerChoice =="Rock")
{
if (compChoice == "Rock")
cout << "It's a tie!\n\n\n\n";
else if (compChoice == "Paper")
cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
else if (compChoice == "Scissors")
cout << "Rock beats scissors! You win!\n\n\n\n";
}

else if (playerChoice =="Paper")
{
if (compChoice == "Rock")
cout << "Paper beats rock! You win!\n\n\n\n";
else if (compChoice == "Paper")
cout << "It's a tie!\n\n\n\n";
else if (compChoice == "Scissors")
cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
}
else if (playerChoice =="Scissors")
{
if (compChoice == "Rock")
cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
else if (compChoice ==  "Paper")
cout << "Scissors beat paper! You win!\n\n\n\n";
else if (compChoice == "Scissors")
cout << "It's a tie!\n\n\n\n";
}
}```

14. So first of all, playerChoice and compChoice aren't defined in main. No such variables exist in main. So why do you think you can pass some non-existing variables to a function?
Secondly, playerChoice and compChoice are names of functions. It's just bad practice to name local variables with the same name.
As a consequence of your naming rules, you are passing function pointers to winner, which aren't strings, and so the compiler complains.