Problem with a game (Class project)

This is my first time posting here, so I'm not really sure if this is the right section for this, but...

I've run into a bit of a problem with a function for a slot machine program I'm making for my C++ class.

The function does "exactly what is says on the tin", so to speak: it's intended to allow a user to delete other users from a struct array.

The problem comes in when entering a value for the variable sureDelete. For some reason, after inputting a value for it, the program goes back to the first while loop within my "valid slot check" do/while loop; that is, it repeatedly executes the output statement in line 24.

Near the bottom of the code (lines 57-62), I've also commented out another while loop that also just infinitely loops output.

The common theme here is that I can't really see why this is happening; in both cases, my test input was such that the loop condition check should've evaluated to false, thus not executing the loop. After attempting to debug for hours, I'm too frustrated to come up with any more ideas, so here I am.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  void deleteUser(playerData players[], const int NUM_OF_PLAYERS, int playerIndex){

	int index, deletionIndex, sureDelete, choice;
	bool validSlot = false;

	do{

		std::cout << "PLAYER DATA LIST: " << std::endl << std::endl;

		for (index = 0; index < playerIndex; index++){

			std::cout << "PLAYER " << (index + 1) << ": " << std::setw(8) << players[index].name << std::setw(8) << players[index].score << std::endl;
		}

		std::cout << "\n\nEnter the slot number of the player whose data you wish to delete: ";
		std::cin >> deletionIndex;

		deletionIndex -= 1; // Adjusts for the user's "slot number" entry; Data slot 1 = index 0, etc.

		do{

			while (deletionIndex < 0 || deletionIndex > NUM_OF_PLAYERS){ // Checks whether the user picked a slot with the range of the players array. If not, ask them to pick another slot

				std::cout << "Invalid slot number. Please enter a slot between 1 and " << NUM_OF_PLAYERS << ": ";
				std::cin >> deletionIndex;
				std::cout << std::endl;

				deletionIndex -= 1;
			}

			while (players[deletionIndex].name == "" && players[deletionIndex].score == 0){ // Checks whether the slot the user picked is already empty. If it is, ask them to pick another slot

				std::cout << "That slot is already empty! Please select another slot: ";
				std::cin >> deletionIndex;
				std::cout << std::endl;

				deletionIndex -= 1; // Same adjustment mentioned above

			}

			// Checks whether the deletion index entered after the second while loop still satisfies the condition of the first while loop.
			// If it does, end the "slot validity check" loop

			if (deletionIndex >= 0 && deletionIndex < NUM_OF_PLAYERS)
				validSlot = true;

		} while (!validSlot);

		std::cout << "\nAre you sure you wish to delete the data in slot " << (deletionIndex + 1) << "? (Y/N): "; // Ensures there will be no "accidental deletion" of data due to typos, etc
		std::cin >> sureDelete;


		// Checks whether the user entered valid input BEFORE executing the switch structure.
		// If they didn't, it requests new input until they do so, at which point the program continues to the switch structure.
		// This negates the need for a nested do/while loop and a "default" case in the switch

		//while ((sureDelete != 'y' && sureDelete != 'Y') && (sureDelete != 'n' && sureDelete != 'N')){
		//std::cout << "Invalid input." << std::endl << std::endl
		//<< "Are you sure you wish to delete the data in slot " << (deletionIndex + 1) << "? (Y/N): ";

		//std::cin >> sureDelete;
		//}

		switch (sureDelete){
		case 'Y':
		case 'y':
			players[deletionIndex].name = "";
			players[deletionIndex].score = 0;
			std::cout << "Player data deleted." << std::endl;
			break;

		case 'N':
		case 'n':
			break; // If user does not wish to delete data in slot, do nothing
		default:
			std::cout << "Invalid input.";
		}

		std::cout << "\n\nWould you like to delete another player's data? (Y/N): ";
		std::cin >> choice;

	} while (choice != 'n' && choice != 'N');

}
Last edited on
Topic archived. No new replies allowed.