The default in my switch statement is looping.

Hello I'm a complete beginner in c++.I tried to make a tic-tac-toe game.but in a switch statement, in default to check invalid user input,it gets messed up when running.its a bit long.It would be great if anyone could tell me am more efficient way to do this.



#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
//initiating global variables
int slotch = 0;
int playc = 1;
int times = 0;
int playcx;
string sign;
string slot1 = "slot1";
string slot2 = "slot2";
string slot3 = "slot3";
string slot4 = "slot4";
string slot5 = "slot5";
string slot6 = "slot6";
string slot7 = "slot7";
string slot8 = "slot8";
string slot9 = "slot9";
//declaring void functions
void display();
void play();
//main function
int main()
{
display();
}
//display function for drawing on screen
void display() {
if (times == 9) {
times = 0;
system("cls");
cout << " | |" << endl << " | | " << endl;
cout << " " << slot1 << " | " << slot2 << " | " << slot3 << endl << " | | " << endl << "_________ | __________ | __________" << endl;
cout << " | |" << endl << " | | " << endl;
cout << " " << slot4 << " | " << slot5 << " | " << slot6 << endl << " | | " << endl << "_________ | __________ | __________" << endl;
cout << " | |" << endl << " | | " << endl;
cout << " " << slot7 << " | " << slot8 << " | " << slot9 << endl << " | | " << endl << " | | " << endl;
system("pause");
slot1 = "slot1";
slot2 = "slot2";
slot3 = "slot3";
slot4 = "slot4";
slot5 = "slot5";
slot6 = "slot6";
slot7 = "slot7";
slot8 = "slot8";
slot9 = "slot9";
playc = 1;
}
system("cls");
cout << " | |" << endl << " | | " << endl;
cout << " " << slot1 << " | " << slot2 << " | " << slot3 << endl << " | | " << endl << "_________ | __________ | __________" << endl;
cout << " | |" << endl << " | | " << endl;
cout << " " << slot4 << " | " << slot5 << " | " << slot6 << endl << " | | " << endl << "_________ | __________ | __________" << endl;
cout << " | |" << endl << " | | " << endl;
cout << " " << slot7 << " | " << slot8 << " | " << slot9 << endl << " | | " << endl << " | | " << endl;
play();
}
//play function for gameplay
void play() {
if (playc == 1) {
sign = " X ";
playc = 2;
playcx = 1;
}
else if (playc == 2) {
sign = " O ";
playc = 1;
playcx = 2;
}

cout << "It's player"<< sign <<"'s chance." << endl << " Enter the number of the slot you want to put your"<< sign <<"in and press enter." << endl;
cin >> slotch;

switch (slotch) {
case 1:if (slot1 == "slot1") {
slot1 = sign;
}else {
playc = playcx;
times--;
}
break;
case 2:if (slot2 == "slot2") {
slot2 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 3:if (slot3 == "slot3") {
slot3 = sign ;
}
else {
playc = playcx;
times--;
}
break;
case 4:if (slot4 == "slot4") {
slot4 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 5:if (slot5 == "slot5") {
slot5 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 6:if (slot6 == "slot6") {
slot6 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 7:if (slot7 == "slot7") {
slot7 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 8:if (slot8 == "slot8") {
slot8 = sign;
}
else {
playc = playcx;
times--;
}
break;
case 9:if (slot9 == "slot9") {
slot9 = sign;
}
else {
playc = playcx;
times--;
}
break;
default:cout << "invalid input";
playc = playcx;
times--;
slotch = 0;
}

times++;
display();
}

Please edit to add code tags for readability.
https://www.cplusplus.com/articles/jEywvCM9/
Hello I'm a complete beginner in c++.I tried to make a tic-tac-toe game.

That’s usually a suggested exercise to learn about arrays and loops.
You can solve it even without loops, because the winning and losing conditions are a reasonable finite number, but I don’t know if it’s useful, because the code becomes just a list of statements which repeat several times with tiny variations.

I’d wait to learn about arrays and loops before undertaking this exercise, but if you really want to try:

1) Do not care about graphic at the beginning. You data just need to be readable, not perfect. You can adjust it later.

So you have 9 variables (chars or std::strings) you need to display into a 3 x 3 table. Something like this could be a tolerable start:
1
2
3
std::cout << pos_0_0 << ' ' << pos_0_1 << ' ' << pos_0_2 << '\n'
          << pos_1_0 << ' ' << pos_1_1 << ' ' << pos_1_2 << '\n'
          << pos_2_0 << ' ' << pos_2_1 << ' ' << pos_2_2 << '\n';


2) Now you need to ask the user about their move.
This is a far harder challenge.
You could start asking which row s/he wants to modify. According to their answer, you can divide your variables in three groups:
- pos_0_0, pos_0_1 and pos_0_2 if the user wants to assign in the first row
- pos_1_0, pos_1_1 and pos_1_2 if the user wants to assign in the second row
- pos_2_0, pos_2_1 and pos_2_2 if the user wants to assign in the third row.

Once you’ve selected the right group, you can ask about the column, which tells you exactly which variable to update.

A bit awkward, isn’t it? ;-)

3) The winning conditions are:
- compare with each other pos_0_0, pos_0_1 and pos_0_2
If no winner:
- compare with each other pos_1_0, pos_1_1 and pos_1_2
If no winner:
- compare with each other pos_2_0, pos_2_1 and pos_2_2
If no winner:
- compare with each other pos_0_0, pos_1_0 and pos_2_0
If no winner:
- compare with each other pos_0_1, pos_1_1 and pos_2_1
If no winner:
- compare with each other pos_0_2, pos_1_2 and pos_2_2
If no winner:
- compare with each other pos_0_0, pos_1_1 and pos_2_2
If no winner:
- compare with each other pos_2_0, pos_1_1 and pos_0_2

You need to perform the above check after every move.

4) If you want to implement an AI to let the user play against the pc… There are good tutorials on internet :-)

Happy coding!
Topic archived. No new replies allowed.