My code has a successful build, but when I run it and type in my 5 numbers, it breaks. Any clue as to how I can fix it??? Much help will be appreciated.

#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;
	int response[3], help_numbers[5];

	response[0] = 0;
	response[1] = 1;
	response[2] = 2;
     
    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;
     
    string string_computer_numbers = convert_int_to_string(computer_number);
    string string_player_numbers = convert_player_guess_int_to_string(player_guess);

	if (string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[1] == string_player_numbers[1] && string_computer_numbers[2] == string_player_numbers[2] && string_computer_numbers[3] == string_player_numbers[3] && string_computer_numbers[4] == string_player_numbers[4])
	{
		cout << "You got the computer's numbers!!!! =)" << endl << endl;
	}

	else if (string_player_numbers[0] < 1 && string_player_numbers[0] > 6 && string_player_numbers[1] < 1 && string_player_numbers[1] > 6 && string_player_numbers[2] < 1 && string_player_numbers[2] > 6 && string_player_numbers[3] < 1 && string_player_numbers[3] > 6 && string_player_numbers[4] < 1 && string_player_numbers[4] > 6)
	{
		help_numbers[0] = response[0];
		help_numbers[1] = response[0];
		help_numbers[2] = response[0];
		help_numbers[3] = response[0];
		help_numbers[4] = response[0];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] != string_player_numbers[3] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[2])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[2])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[1])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[3] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[4])
	{
		help_numbers[0] = response[1];
	}

	cout << "Computer Numbers: " << computer_number[0] << computer_number[1] << computer_number[2] << computer_number[3] << computer_number[4] << endl << endl;
	cout << "Player Guesses: " << string_player_numbers[0] << string_player_numbers[1] << string_player_numbers[2] << string_player_numbers[3] << string_player_numbers[4] << endl << endl;

    system("pause");
    return 0;
    }

Well I'm not looking at all of this code that's what using a debugger is for but at first glance line 47 is wrong. You are returning an object that doesn't exist. What are you using to compile your code? If you are using an IDE they normally have built in feature to step through your code and you can see what is going wrong. .It is very helpful and you can really learn from it.

I'm very new at this and I am in college for Computer Programming. My teacher wants us to turn ints into string. So I did. I thought stringstream was a declaration and then player_numbers[5] was the name.

@thines: This code ends at the bottom of the screen. where it says system("pause); return 0;

This is my recent code. It has the same problem. Can anyone please help me fix this???

This is header1

#include <iostream>
    #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");
    }

This is header2

#include "header1.h"

string convert_int_to_string(int computer_number[5])
    {
		string computer_numbers[5];


		if (computer_number[0] == 1)
		{
			computer_numbers[0] = '1';
		}

		if (computer_number[0] == 2)
		{
			computer_numbers[0] = '2';
		}

		if (computer_number[0] == 3)
		{
			computer_numbers[0] = '3';
		}

		if (computer_number[0] == 4)
		{
			computer_numbers[0] = '4';
		}

		if (computer_number[0] == 5)
		{
			computer_numbers[0] = '5';
		}

		if (computer_number[0] == 6)
		{
			computer_numbers[0] = '6';
		}



		if (computer_number[1] == 1)
		{
			computer_numbers[1] = '1';
		}

		if (computer_number[1] == 2)
		{
			computer_numbers[1] = '2';
		}

		if (computer_number[1] == 3)
		{
			computer_numbers[1] = '3';
		}

		if (computer_number[1] == 4)
		{
			computer_numbers[1] = '4';
		}

		if (computer_number[1] == 5)
		{
			computer_numbers[1] = '5';
		}

		if (computer_number[1] == 6)
		{
			computer_numbers[1] = '6';
		}



		if (computer_number[2] == 1)
		{
			computer_numbers[2] = '1';
		}

		if (computer_number[2] == 2)
		{
			computer_numbers[2] = '2';
		}

		if (computer_number[2] == 3)
		{
			computer_numbers[2] = '3';
		}

		if (computer_number[2] == 4)
		{
			computer_numbers[2] = '4';
		}

		if (computer_number[2] == 5)
		{
			computer_numbers[2] = '5';
		}

		if (computer_number[2] == 6)
		{
			computer_numbers[2] = '6';
		}



		if (computer_number[3] == 1)
		{
			computer_numbers[3] = '1';
		}

		if (computer_number[3] == 2)
		{
			computer_numbers[3] = '2';
		}

		if (computer_number[3] == 3)
		{
			computer_numbers[3] = '3';
		}

		if (computer_number[3] == 4)
		{
			computer_numbers[3] = '4';
		}

		if (computer_number[3] == 5)
		{
			computer_numbers[3] = '5';
		}

		if (computer_number[3] == 6)
		{
			computer_numbers[3] = '6';
		}



		if (computer_number[4] == 1)
		{
			computer_numbers[4] = '1';
		}

		if (computer_number[4] == 2)
		{
			computer_numbers[4] = '2';
		}

		if (computer_number[4] == 3)
		{
			computer_numbers[4] = '3';
		}

		if (computer_number[4] == 4)
		{
			computer_numbers[4] = '4';
		}

		if (computer_number[4] == 5)
		{
			computer_numbers[4] = '5';
		}

		if (computer_number[4] == 6)
		{
			computer_numbers[4] = '6';
		}

    return computer_numbers[5];
    }


	 string convert_player_guess_int_to_string(int player_guess[5])
    {

		string player_numbers[5];


		if (player_guess[0] == 1)
		{
			player_numbers[0] = '1';
		}

		if (player_guess[0] == 2)
		{
			player_numbers[0] = '2';
		}

		if (player_guess[0] == 3)
		{
			player_numbers[0] = '3';
		}

		if (player_guess[0] == 4)
		{
			player_numbers[0] = '4';
		}

		if (player_guess[0] == 5)
		{
			player_numbers[0] = '5';
		}

		if (player_guess[0] == 6)
		{
			player_numbers[0] = '6';
		}



		if (player_guess[1] == 1)
		{
			player_numbers[1] = '1';
		}

		if (player_guess[1] == 2)
		{
			player_numbers[1] = '2';
		}

		if (player_guess[1] == 3)
		{
			player_numbers[1] = '3';
		}

		if (player_guess[1] == 4)
		{
			player_numbers[1] = '4';
		}

		if (player_guess[1] == 5)
		{
			player_numbers[1] = '5';
		}

		if (player_guess[1] == 6)
		{
			player_numbers[1] = '6';
		}



		if (player_guess[2] == 1)
		{
			player_numbers[2] = '1';
		}

		if (player_guess[2] == 2)
		{
			player_numbers[2] = '2';
		}

		if (player_guess[2] == 3)
		{
			player_numbers[2] = '3';
		}

		if (player_guess[2] == 4)
		{
			player_numbers[2] = '4';
		}

		if (player_guess[2] == 5)
		{
			player_numbers[2] = '5';
		}

		if (player_guess[2] == 6)
		{
			player_numbers[2] = '6';
		}



		if (player_guess[3] == 1)
		{
			player_numbers[3] = '1';
		}

		if (player_guess[3] == 2)
		{
			player_numbers[3] = '2';
		}

		if (player_guess[3] == 3)
		{
			player_numbers[3] = '3';
		}

		if (player_guess[3] == 4)
		{
			player_numbers[3] = '4';
		}

		if (player_guess[3] == 5)
		{
			player_numbers[3] = '5';
		}

		if (player_guess[3] == 6)
		{
			player_numbers[3] = '6';
		}



		if (player_guess[4] == 1)
		{
			player_numbers[4] = '1';
		}

		if (player_guess[4] == 2)
		{
			player_numbers[4] = '2';
		}

		if (player_guess[4] == 3)
		{
			player_numbers[4] = '3';
		}

		if (player_guess[4] == 4)
		{
			player_numbers[4] = '4';
		}

		if (player_guess[4] == 5)
		{
			player_numbers[4] = '5';
		}

		if (player_guess[4] == 6)
		{
			player_numbers[4] = '6';
		}

		return player_numbers[5];
    }

this is my main.cpp

#include "header2.h"
     
    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;
	int response[3], help_numbers[5];

	response[0] = 0;
	response[1] = 1;
	response[2] = 2;
     
    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;
     
    string string_computer_numbers = convert_int_to_string(computer_number);
    string string_player_numbers = convert_player_guess_int_to_string(player_guess);

	if (string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[1] == string_player_numbers[1] && string_computer_numbers[2] == string_player_numbers[2] && string_computer_numbers[3] == string_player_numbers[3] && string_computer_numbers[4] == string_player_numbers[4])
	{
		cout << "You got the computer's numbers!!!! =)" << endl << endl;
	}

	else if (string_player_numbers[0] < '1' && string_player_numbers[0] > '6' && string_player_numbers[1] < '1' && string_player_numbers[1] > '6' && string_player_numbers[2] < '1' && string_player_numbers[2] > '6' && string_player_numbers[3] < '1' && string_player_numbers[3] > '6' && string_player_numbers[4] < '1' && string_player_numbers[4] > '6')
	{
		help_numbers[0] = response[0];
		help_numbers[1] = response[0];
		help_numbers[2] = response[0];
		help_numbers[3] = response[0];
		help_numbers[4] = response[0];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] != string_player_numbers[3] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[2])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[4])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[3])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[2])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] != string_player_numbers[0] && string_computer_numbers[0] == string_player_numbers[1])
	{
		help_numbers[0] = response[2];
	}

	else if (string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[1] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[2] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[3] && string_computer_numbers[0] == string_player_numbers[0] && string_computer_numbers[0] != string_player_numbers[4])
	{
		help_numbers[0] = response[1];
	}

	cout << "Computer Numbers: " << computer_number[0] << computer_number[1] << computer_number[2] << computer_number[3] << computer_number[4] << endl << endl;
	cout << "Player Guesses: " << string_player_numbers[0] << string_player_numbers[1] << string_player_numbers[2] << string_player_numbers[3] << string_player_numbers[4] << endl << endl;

    system("pause");
    return 0;
    }

In convert_int_to_string you're declaring an array of 5 strings, but at the end of the function you're returning the 6th-string in the array, which doesn't exist. Remember that index starts at 0.

Edited 4 Years Ago by Iwansomney: n/a

It says my code is assertive and string subscript is out of range. can anyone help me out here please????????

What line? The compiler will tell you the exact line causing the problem.

Nevermind
-- Line 166

return computer_numbers[5]; //one too many

Edited 4 Years Ago by thines01: clarity

This article has been dead for over six months. Start a new discussion instead.