Having trouble with guessing number game.

I am using a stack and an array. The array is used to random shuffle the order, and then I put the elements of the array into the stack. Then the user has to guess the order. Since the stack is first in, first out, and I am using the STL, it is easy to find out if the user entered in a correct guess. I just call the top function and if it is pointing to what the user enters in, I pop it.

But here comes the problem, I want to make it so that if the player guesses wrong, the player has to start all over. From the beginning. And the loops and if/else statements I have been trying have not been working. There is also the problem of when I pop the element. If the user guesses the second number wrong, then I cannot start from the beginning because I popped the previous element. Is there a way to point to the next element without popping anything?

How can I set up a loop that will make the player start from the beginning if the player guesses wrong? If I can figure that out, then maybe I can just pop all the elements from my stack, and then take the elements that are stored in my array and push them into my stack again whenever the user guesses wrong.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 #include <iostream>
#include <stack>
#include <array>
#include <set>
#include <iomanip>
#include <random> // to generate random values
#include <ctime> // to use time 
#include <algorithm> // to use random shuffle 

using namespace std; // to avoid using std:: all over the place

class Array // array class
{
public:
	default_random_engine engine{ static_cast<unsigned int>(time(0)) };
	uniform_int_distribution<unsigned int> randomInt1{ 6, 10 }; // first random int
	uniform_int_distribution<unsigned int> randomInt2{ 11, 15 }; // second random int 
	uniform_int_distribution<unsigned int> randomInt3{ 16, 20 }; // third random int
	//array<unsigned int, 3>myArray = { randomInt1(engine), randomInt2(engine), randomInt3(engine) };
vector<unsigned int>myArray = { randomInt1(engine), randomInt2(engine), randomInt3(engine) };
	void displayArray(); // display array
	void shuffleArray(); // shuffle the array
};

class Stack // stack class
{
public:
	void pushS(unsigned int);
	void popS();
	void topS(unsigned int);
	void sizeS();
	stack<unsigned int>myStack;
	//bool gameOver = false;
};

void Array::displayArray() // display array
{
	for (auto array : myArray)
	{
		cout << array << ' ';
	}
}

void Array::shuffleArray() // shuffle the array
{
	random_shuffle(myArray.begin(), myArray.end()); // shuffle the order of elements
}

// STACK FUNCTIONS BELOW

void Stack::pushS(unsigned int pushValue) // push values from array into stack
{
	myStack.push(pushValue);
}

void Stack::popS() // display then remove values from stack
{
	while (!myStack.empty())
	{
		cout << myStack.top() << ' ';
		myStack.pop();
	}
}

void Stack::topS(unsigned int guess) // checking to see if guess is the correct order
{
	if (guess == myStack.top())
	{
		cout << "Found! ";
		cout << myStack.top() << ' ';
		myStack.pop();
	}
}

void Stack::sizeS() // checking the size of the stack
{
	cout << myStack.size();
}

int main()
{
	srand(unsigned(time(0))); // so random_shuffle can shuffle the array
	Stack stackTest; // stack class object
	Array arrayTest; // array class object
	cout << "This is the array: ";
	arrayTest.displayArray(); // display array
	cout << endl;
	//cout << "This is array after shuffle: ";
	//arrayTest.shuffleArray(); // shuffle array
	//arrayTest.displayArray(); // display new order of array
	//cout << endl;
	for (auto array : arrayTest.myArray) // putting shuffled array into stack
	{
		stackTest.pushS(array);
	}
	//cout << "This is the stack with the elements from the array: ";
	//stackTest.pop(); // displaying stack that has the shuffled array
	//cout << endl;

	unsigned int guess;
	cout << "Guess the first number: ";
	cin >> guess;
	stackTest.topS(guess);
	cout << "Current size: ";
	stackTest.sizeS();
	cout << endl;

	cout << "Guess the second number: ";
	cin >> guess;
	stackTest.topS(guess);
	cout << "Current size: ";
	stackTest.sizeS();
	cout << endl;

	cout << "Guess the third number: ";
	cin >> guess;
	stackTest.topS(guess);
	cout << "Current size: ";
	stackTest.sizeS();
	cout << endl;

	//cout << "This is the correct order: ";
	//stackTest.popS();
	//cout << endl;
	//cout << "Current size: ";
	//stackTest.sizeS();
	//cout << endl;

	/*if (stackTest.myStack.empty())
	{
		cout << "You have won! ";
	}
	cout << endl;*/
	
	return 0;
}


I already put up the answer in the program. I was testing all kinds of things, but I comment out all the lines that I do not need right now. All you have to do is enter the order in reverse, because the values that I show you are what I take from the array and push into the stack. In the actual game I won't show you those values. Just the values before I sort them. And I am using a stack because I plan on popping the order in reverse after the user wins the game. I will shuffle the order every time the user wins.

Line 19 doesn't compile using the the cpp.sh website. I am guessing it does not like std arrays. It complies on my MS VS 2015 without any warnings or errors. If you change the line to
 
vector<unsigned int>myArray = { randomInt1(engine), randomInt2(engine), randomInt3(engine) };

it will run
Last edited on
> How can I set up a loop that will make the player start from the beginning if the player guesses wrong?
Why are you using a stack?

Because with an array, it's a doddle.
1
2
3
4
5
6
7
while ( guessIndex != endIndex ) {
  if ( guessCorrect ) {
    guessIndex++;
  } else {
    guessIndex = 0;
  }
}
I was trying to get creative with the stack. I wanted to display the order in reverse after the user wins the game. I just thought a stack would be good for this since you cannot search it and whatever is at the top has to be next.
Last edited on
Push the correct guesses onto a new stack.

1
2
3
4
5
6
7
8
9
10
while ( !guess.empty() ) {
  x = guess.pop();
  if ( x == input ) {
    correct.push(x);
  } else {
    while ( !correct.empty() ) {
      guess.push(correct.pop());
    }
  }
}
Topic archived. No new replies allowed.