guess game
In this game, you play with computer for N rounds (N should be provided by the user and passed into the function as a parameter). In each round, the computer generates a random number between 1 and 3 (including 1 and 3), and the computer asks you to guess the number. If your given number is just the same as the generated number, you win; otherwise you lose. Generate a final report showing how many times you win and how many times you lose.
Function prototype: void guess_game(int N ) ;
Hi i need help with the N rounds part cant seems to call the function for N times.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void print_multitable();
void guess_game();

int main()
{	int x;
	cout << "Press 1 for multiplication table or 2 for guessing game: " << endl;
	cin >> x;
	if (x == 1)
		print_multitable();
	if (x == 2)
		guess_game();
	else
		cout << "You have exited the menu" << endl;

	return 0;
}

void print_multitable(void)
{
	int i,j;
	for (i=0; i<=9; i++){
		for (j=1; j<=i; j++){
			cout << i << "x" << j << "=" << i*j << " ";
		}
	cout << endl << setw(i-9);
	}
	return;
}
void guess_game(int N)
{
    srand((unsigned)time(0));
    int g;
    int y = rand()% 3+1 ;
    cout << "Enter the number of rounds to play: " << endl;
    cin >> N;
    cout << "Guess a number from 1 to 3: "<< endl;
    cin >> g;
    if (g==y)
    	cout<< "You win" << endl;
    if (g!=y)
    	cout << "you lose, the number was " << y << endl;
    return ;
}

Recommended Answers

All 2 Replies

Code tags:

[code=cplusplus] // paste code here

[/code]

You have this call:

guess_game();

and this prototype:

void guess_game();

but the function itself is this:

void guess_game(int N)

The prototype needs to match the implementation and the function call. The function call should be something like this:

int N;
cin >> N;
guess_game (N);

You are asking how many rounds they want to play in the guess_game function, but I am interpreting this line:

In this game, you play with computer for N rounds (N should be provided by the user and passed into the function as a parameter).

as saying that you are supposed to ask that question BEFORE calling the guess_game function.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void print_multitable();
void guess_game();

int main()
{ int x;
cout << "Press 1 for multiplication table or 2 for guessing game: " << endl;
cin >> x;
if (x == 1)
print_multitable();
if (x == 2)
guess_game();
else
cout << "You have exited the menu" << endl;

return 0;
}

void print_multitable(void)
{
int i,j;
for (i=0; i<=9; i++){
for (j=1; j<=i; j++){
cout << i << "x" << j << "=" << i*j << " ";
}
cout << endl << setw(i-9);
}
return;
}
void guess_game(int N)
{
srand((unsigned)time(0));
int g;
int y = rand()% 3+1 ;
cout << "Enter the number of rounds to play: " << endl;
cin >> N;
cout << "Guess a number from 1 to 3: "<< endl;
cin >> g;
if (g==y)
cout<< "You win" << endl;
if (g!=y)
cout << "you lose, the number was " << y << endl;
return ;
}

Side note: You only need to seed the random number generator once to use it for the application.

// . . .
int main()
{
srand((unsigned)time(0));
// . . .
}
// . . .
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.