Ok, so in this program, the user enters a number and it assigns that number to "answer". Now I have a bunch of strings already named by numbers, it starts out with "one", "two", "three" and so on being valued as a dash sign "-".

So say they enter "five", "answer" is now equal to "five".

In the code below, it says "while (answer !="... and I want it instead to say "while (five !=", but if I did that I'd have to enter a different while function for every number.

So is there anyway to have the value of "answer" show up as it's own variable?

Sorry if this doesn't make sense, I'm really new to programming C++.

while (answer != "-") {
     cout << "This spot is already taken!" << endl;
     getch();
     cin >> answer;
     }

Here's the entire thing so far (I'm making a tic-tac-toe game to try and understand C++):

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main () {    
    string one, two, three, four, five, six, seven, eight, nine, answer;
	 one = "-";
	 two = "-";
	 three = "-";
	 four = "-";
	 five = "-";
	 six = "-";
	 seven = "-";
	 eight = "-";
	 nine = "-";
	 
     cout << endl << " " << one << two << three << endl << " " << four << five << six << endl << " " << seven << eight << nine << endl << endl;
	 cout << "Xs turn, type out which space you want (one to nine)." << endl;
	 cin >> answer;

	 while (answer != "one" && answer != "two" && answer != "three" && answer != "four" && answer != "five" && answer != "six" && answer != "seven" && answer != "eight" && answer != "nine") {
	 cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
	 getch();
	 cin >> answer;
	 }

	 if (answer == "one") {
	 one = "X";
	 }
	 if (answer == "two") {
	 two = "X";
	 }
	 if (answer == "three") {
	 three = "X";
	 }
	 if (answer == "four") {
	 four = "X";
	 }
	 if (answer == "five") {
	 five = "X";
	 }
	 if (answer == "six") {
	 six = "X";
	 }
	 if (answer == "seven") {
	 seven = "X";
	 }
	 if (answer == "eight") {
	 eight = "X";
	 }
	 if (answer == "nine") {
	 nine = "X";
	 }

     cout << endl << " " << one << two << three << endl << " " << four << five << six << endl << " " << seven << eight << nine << endl << endl;
     cout << "Xs chose spot " << answer << "!" << endl;
     cout << "Os turn, type out which space you want (one to nine)." << endl;
	 cin >> answer;

	 while (answer != "one" && answer != "two" && answer != "three" && answer != "four" && answer != "five" && answer != "six" && answer != "seven" && answer != "eight" && answer != "nine") {
	 cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
	 getch();
	 cin >> answer;
	 }

     while (answer != "-") {
     cout << "This spot is already taken!" << endl;
     getch();
	 cin >> answer;
     }

	 if (answer == "one") {
	 one = "O";
	 }
	 if (answer == "two") {
	 two = "O";
	 }
	 if (answer == "three") {
	 three = "O";
	 }
	 if (answer == "four") {
	 four = "O";
	 }
	 if (answer == "five") {
	 five = "O";
	 }
	 if (answer == "six") {
	 six = "O";
	 }
	 if (answer == "seven") {
	 seven = "O";
	 }
	 if (answer == "eight") {
	 eight = "O";
	 }
	 if (answer == "nine") {
	 nine = "O";
	 }

     cout << endl << " " << one << two << three << endl << " " << four << five << six << endl << " " << seven << eight << nine << endl << endl;
	 cout << "Xs turn, type out which space you want (one to nine)." << endl;
	 cin >> answer;

	 while (answer != "one" && answer != "two" && answer != "three" && answer != "four" && answer != "five" && answer != "six" && answer != "seven" && answer != "eight" && answer != "nine") {
	 cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
	 getch();
	 cin >> answer;
	 }

	 if (answer == "one") {
	 one = "X";
	 }
	 if (answer == "two") {
	 two = "X";
	 }
	 if (answer == "three") {
	 three = "X";
	 }
	 if (answer == "four") {
	 four = "X";
	 }
	 if (answer == "five") {
	 five = "X";
	 }
	 if (answer == "six") {
	 six = "X";
	 }
	 if (answer == "seven") {
	 seven = "X";
	 }
	 if (answer == "eight") {
	 eight = "X";
	 }
	 if (answer == "nine") {
	 nine = "X";
	 }

    return 0;
}

Storing your board as nine strings overcomplicates things. Each spot can have three possible states: taken by x, taken by y, or not taken. So you should probably represent the board as a 3 x 3 array of chars or a regular old one dimensional array of 9 chars.

'-' means not taken, 'x' means taken by x, 'y' means taken by y.

If you really want to have the user enter the number spelled out, you'll want to convert that number to an integer, then check whether the spot is taken. If not, stick the appropriate new value in. It would be easier to simply have the user input a number that isn't spelled out into a word.

The point is that what you take in as input and how you store everything are separate. Variables like "five" are a particulary bad name. "five" isn't supposed to vary. You can have a CONSTANT string literal with the VALUE "five", buy you shouldn't have a VARIABLE NAMED "five".

Something like this perhaps...

#include <string>
#include <iostream>
using namespace std;

const int NUM_NUMBERS = 4;
const string NUMBER_STRINGS[NUM_NUMBERS] = {"zero", "one", "two", "three"};

// return -1 for error
int LiteralToInt(const string& literal)
{
    for(int i = 0; i < NUM_NUMBERS; i++)
    {
        if(literal == NUMBER_STRINGS[i])
	{
            return i;
	}
    }
	
    return -1;
}

int main()
{
    cout << "three = " << LiteralToInt("three") << endl;
    cout << "2 = " << NUMBER_STRINGS[2] << endl;
    return 0;
}

Now you can very clickly go from 2 to "two" and vice versa.

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.