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[5]) 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[5])
{
	stringstream computer_numbers[5];

	computer_numbers[0] << computer_number[0];
	computer_numbers[1] << computer_number[1];
	computer_numbers[2] << computer_number[2];
	computer_numbers[3] << computer_number[3];
	computer_numbers[4] << computer_number[4];

	return computer_numbers[5].str();
};

string convert_player_guess_int_to_string(int player_guess[5])
{
	stringstream player_numbers[5];

	player_numbers[0] << player_guess[0];
	player_numbers[1] << player_guess[1];
	player_numbers[2] << player_guess[2];
	player_numbers[3] << player_guess[3];
	player_numbers[4] << player_guess[4];

	return player_numbers[5].str();
};

int main()
{
	Welcome_Screen();

	srand(time(0));

	int computer_number[5], player_guess[5];

	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[0] = random_number1;
	computer_number[1] = random_number2;
	computer_number[2] = random_number3;
	computer_number[3] = random_number4;
	computer_number[4] = 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[0];
	cout << endl;
	cout << "Guess 2: ";
	cin >> player_guess[1];
	cout << endl;
	cout << "Guess 3: ";
	cin >> player_guess[2];
	cout << endl;
	cout << "Guess 4: ";
	cin >> player_guess[3];
	cout << endl;
	cout << "Guess 5: ";
	cin >> player_guess[4];
	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[0] << computer_numbers[1] << computer_numbers[2] << computer_numbers[3] << computer_numbers[4] << endl;
	// cout << "Player numbers: " << player_numbers[0] << player_numbers[1] << player_numbers[2] << player_numbers[3] << player_numbers[4] << endl;

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


	system("pause");
	return 0;
}

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[5])
{
	stringstream computer_numbers[5];

	computer_numbers[0] << computer_number[0];
	computer_numbers[1] << computer_number[1];
	computer_numbers[2] << computer_number[2];
	computer_numbers[3] << computer_number[3];
	computer_numbers[4] << computer_number[4];

	return computer_numbers[5].str();
};

string convert_player_guess_int_to_string(int player_guess[5])
{
	stringstream player_numbers[5];

	player_numbers[0] << player_guess[0];
	player_numbers[1] << player_guess[1];
	player_numbers[2] << player_guess[2];
	player_numbers[3] << player_guess[3];
	player_numbers[4] << player_guess[4];

	return player_numbers[5].str();
};

int main()
{
	Welcome_Screen();

	srand(time(0));

	int computer_number[5], player_guess[5];

	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[0] = random_number1;
	computer_number[1] = random_number2;
	computer_number[2] = random_number3;
	computer_number[3] = random_number4;
	computer_number[4] = 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[0];
	cout << endl;
	cout << "Guess 2: ";
	cin >> player_guess[1];
	cout << endl;
	cout << "Guess 3: ";
	cin >> player_guess[2];
	cout << endl;
	cout << "Guess 4: ";
	cin >> player_guess[3];
	cout << endl;
	cout << "Guess 5: ";
	cin >> player_guess[4];
	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[0]) << endl;
	cout << "Player numbers: " << convert_player_guess_int_to_string(player_numbers[0]) << 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[0] == 1 then number[0] == "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[0]) << 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.

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

string strPlayerGuess;
cout << "Your number: ";
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.

Edited 4 Years Ago by Iwansomney: n/a

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[0] == returnStringPlayer[0] )
 .......
This article has been dead for over six months. Start a new discussion instead.