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();
}``````

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;

cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
getch();
}

one = "X";
}
two = "X";
}
three = "X";
}
four = "X";
}
five = "X";
}
six = "X";
}
seven = "X";
}
eight = "X";
}
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;

cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
getch();
}

cout << "This spot is already taken!" << endl;
getch();
}

one = "O";
}
two = "O";
}
three = "O";
}
four = "O";
}
five = "O";
}
six = "O";
}
seven = "O";
}
eight = "O";
}
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;

cout << "You didn't answer correctly, spell out a number from one to nine." << endl;
getch();
}

one = "X";
}
two = "X";
}
three = "X";
}
four = "X";
}
five = "X";
}
six = "X";
}
seven = "X";
}
eight = "X";
}
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 learning and sharing knowledge.