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;
}