My teacher assigned the class this:

Create a simple Game Loop for the game Mastermind. (Search the Internet to refresh your mind on how Mastermind is set up.)
Have your computer generate a random 5 digit number, as a string. Then the user must guess the number. Using the code:
1 = implies right Number, Right Place.
2 = implies right Number, Wrong Place.
0 = implies Wrong Number.
So the computer decides on 12345; the user guesses 11235; the computer should respond with 10221. Hint: Watch out for a double number like 11 when there is only one.

I am stuck though. I have my random numbers for the computer and I have my player guesses. I have also turned them into string. My problem is that idk how to access them from the convert_int_to_string(int computer_number) function. can anyone help me?

#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

void Welcome_Screen()
{
cout << "You are playing the text version of Mastermind." << endl << endl;
cout << "The rules are to guess the 5 digit number before your number of guesses run out. " << endl << endl;
cout << "The numbers run from 1 - 6 and there can be multiple numbers of the same number." << endl << endl;
cout << "1 implies the right number in the right place." << endl;
cout << "2 implies the right number but in the wrong place." << endl;
cout << "0 implies wrong number." << endl << endl;
cout << "Ready?? Here we go." << endl << endl;
system("pause");
system("cls");
}

string convert_int_to_string(int computer_number)
{
stringstream computer_numbers;

computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;

return computer_numbers.str();
};

string convert_player_guess_int_to_string(int player_guess)
{
stringstream player_numbers;

player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;

return player_numbers.str();
};

int main()
{
Welcome_Screen();

srand(time(0));

int computer_number, player_guess;

int random_number1 = rand() % 6 + 1;
int random_number2 = rand() % 6 + 1;
int random_number3 = rand() % 6 + 1;
int random_number4 = rand() % 6 + 1;
int random_number5 = rand() % 6 + 1;

computer_number = random_number1;
computer_number = random_number2;
computer_number = random_number3;
computer_number = random_number4;
computer_number = random_number5;

int number_of_guesses = 12;

cout << "The computer has made it's choices and set it's 5 numbers down. " << endl << endl;
cout << "What are your number guesses? (press enter after each number)" << endl << endl;
cout << "Guess 1: ";
cin >> player_guess;
cout << endl;
cout << "Guess 2: ";
cin >> player_guess;
cout << endl;
cout << "Guess 3: ";
cin >> player_guess;
cout << endl;
cout << "Guess 4: ";
cin >> player_guess;
cout << endl;
cout << "Guess 5: ";
cin >> player_guess;
cout << endl << endl;

convert_int_to_string(computer_number);
convert_player_guess_int_to_string(player_guess);

// how can I do the below??

// cout << "Computer numbers: " << computer_numbers << computer_numbers << computer_numbers << computer_numbers << computer_numbers << endl;
// cout << "Player numbers: " << player_numbers << player_numbers << player_numbers << player_numbers << player_numbers << endl;

//so this way I can make an if statement for my number of guesses loop????

system("pause");
return 0;
}

Why do you need to convert the integer array to a string array?
can't you use an if statement for the original array already

Why don't you store the player guess directly from input?

string strPlayerGuess;
getline( cin, strPlayerGuess);

And then compare both strings in a for loop

string Response;

for( int i = 0; i < strPlayerGuess.length(); i++ )
{

if( strPlayerGuess[i] …

Is this what are you trying to do?

string returnStringComputer = convert_int_to_string( computer_numbers );
string returnStringPlayer = convert_player_guess_int_to_string( player_guess );

if( returnStringComputer == returnStringPlayer )
.......

All 9 Replies

Here is what I want to do.

#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

void Welcome_Screen()
{
cout << "You are playing the text version of Mastermind." << endl << endl;
cout << "The rules are to guess the 5 digit number before your number of guesses run out. " << endl << endl;
cout << "The numbers run from 1 - 6 and there can be multiple numbers of the same number." << endl << endl;
cout << "1 implies the right number in the right place." << endl;
cout << "2 implies the right number but in the wrong place." << endl;
cout << "0 implies wrong number." << endl << endl;
cout << "Ready?? Here we go." << endl << endl;
system("pause");
system("cls");
}

string convert_int_to_string(int computer_number)
{
stringstream computer_numbers;

computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;
computer_numbers << computer_number;

return computer_numbers.str();
};

string convert_player_guess_int_to_string(int player_guess)
{
stringstream player_numbers;

player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;
player_numbers << player_guess;

return player_numbers.str();
};

int main()
{
Welcome_Screen();

srand(time(0));

int computer_number, player_guess;

int random_number1 = rand() % 6 + 1;
int random_number2 = rand() % 6 + 1;
int random_number3 = rand() % 6 + 1;
int random_number4 = rand() % 6 + 1;
int random_number5 = rand() % 6 + 1;

computer_number = random_number1;
computer_number = random_number2;
computer_number = random_number3;
computer_number = random_number4;
computer_number = random_number5;

int number_of_guesses = 12;

cout << "The computer has made it's choices and set it's 5 numbers down. " << endl << endl;
cout << "What are your number guesses? (press enter after each number)" << endl << endl;
cout << "Guess 1: ";
cin >> player_guess;
cout << endl;
cout << "Guess 2: ";
cin >> player_guess;
cout << endl;
cout << "Guess 3: ";
cin >> player_guess;
cout << endl;
cout << "Guess 4: ";
cin >> player_guess;
cout << endl;
cout << "Guess 5: ";
cin >> player_guess;
cout << endl << endl;

convert_int_to_string(computer_number);
convert_player_guess_int_to_string(player_guess);

cout << "Computer numbers: " << convert_int_to_string(computer_numbers) << endl;
cout << "Player numbers: " << convert_player_guess_int_to_string(player_numbers) << endl;

system("pause");
return 0;
}

Why do you need to convert the integer array to a string array?
can't you use an if statement for the original array already

my teacher wants me to use strings instead of integers because ints would be too easy to do. I coded the 98 lines in like 15 minutes.

I think that i can do the if number == 1 then number == "1" and do that for all numbers, but my problem lies again in the "how do I access single arrays from return functions?" like how do i do this:

cout << "Computer numbers: " << convert_int_to_string(computer_numbers) << endl; but to do that for all of the computers numbers and then for the players so i can make an if-else statement so i can finish this program.

Can anyone help me out please???

Why don't you store the player guess directly from input?

string strPlayerGuess;
getline( cin, strPlayerGuess);

And then compare both strings in a for loop

string Response;

for( int i = 0; i < strPlayerGuess.length(); i++ )
{

if( strPlayerGuess[i] == strComputerGeneratedNumber[i] )
{

response += '1';

}
else if ...

.......

}
And so...

I think it is easier to solve with strings than with integers.

I don't quite follow...

Is this what are you trying to do?

string returnStringComputer = convert_int_to_string( computer_numbers );
string returnStringPlayer = convert_player_guess_int_to_string( player_guess );

if( returnStringComputer == returnStringPlayer )
.......

yes. Is that exact way possible????

If this works I'm voting, commenting, and saying that you are genius.

Be a part of the DaniWeb community

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