I will try to point out some things.
1. It looks like you created a user-defined function, int rand ( int& ), which takes
param: Referenced integer variable
return: integer
However, in the main () function, you have the following lines
rand(player1_score);
rand(player2_score);
Basically, you called a value-returning function as if they were void functions.
Do something like
int random = rand ( player1_score); // Without referencing
OR
void rand ( player1_score ); // Just turn it into a void function
Also on that note, take a look at the definition of int rand ( int& ) function:
1 2 3 4 5 6 7 8 9 10
|
int rand ( int &score )
{
int random;
random=rand()%6;
cout<<"Your random number is:"<<random<<endl;
score=random+score;
return score;
}
|
You simply find a random number using rand () % 6 from the Library, display the random number, and return random + score
The code inside the function is so simple that it could be better off to just use the logic in the main function without having this user-defined function at all.
2. For the rand () to return a random number, you need to provide seed (integer) using the following:
srand ( time ( NULL ) ); // time ( NULL ) can be replaced by an integer
3. Why do you have rows and cols variables in the main function?
Couldn't you do just
int score [4][5];
instead of
int rows=4;
int cols=5;
int score[rows][cols];
4. You might want to initialize player1_score and player2_score variables before you use them for comparison for the first while loop
So, I rewrote the main function. But please understand that I am not on a computer that has a compiler / IDE, so I was never able to test the code. It might not compile and you might have to make some adjustment. See if this is what you wanted to accomplish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
ifstream a; // Not Used in this program
int score [4][5];
int key; // This will be used to store key (an integer) from player and used as seed for srand ();
string player1;
string player2;
// Initialize scores
int player1_score = 0;
int player2_score = 0;
cout << "Enter the name of player 1: " << endl;
cin >> player1;
cout << "Enter the name of player 2: " << endl;
cin >> player2;
while ( player1_score < 20 && player2_score < 20 )
{
cout << player1 << " Please enter a key to generate a random number: " << endl;
cin >> key;
srand ( key );
int random = rand ( ) % 6;
player1_score += random;
cout << "Your random number is: " << random << endl;
cout << "Your score is: " << player1_score << endl;
cout << player2 << " Please enter a key to generate a random number: " << endl;
cin >> key;
srand ( key );
random = rand () % 6;
player2_score += random;
cout << "Your random number is: " << random << endl;
cout << "Your score is: " << player2_score << endl;
}
if ( player1_score > player2_score )
cout << player1 << " is the winner." << endl;
else if ( player2_score > player1_score )
cout << player2 << " is the winner." << endl;
else
cout << "The match is a draw." << endl;
return 0;
}
|