So you are picking #'s and this program needs to the ten last numbers. When you press 'enter' the program will check if the input is a #. So if the input 'a' then output wrong input. And if it is valid number then add to queue. When the user presses N, then the last ten #'s need to print out.
Use std::queue and when the user enters 'N', then call pop() until size() == 10.
Then the rest of the queue will contain the last 10 numbers the user entered.
(You can access the next one by calling front() and then pop it with pop())
whoovian11 didn't say they had to use a queue, that was what I suggested. From what I've seen in other posts though you seem to be more knowledgeable about C++ than me, so I'd go with whatever you come up with.
Start by getting input from the user until the queue contains 10 numbers
Any further input you get, simply pop from the queue first then push the new number into the queue.
If you don't need the 10 most recent and instead want to keep a randomly selected amount from the user inputs, then using a randomized queue will greatly help here. This is one I wrote sometime this year (In java):
If you can't use a queue, then I'd just use an array of ten numbers. Start at position 0 and keep track of where you are in the array. So the first number would go in position 0, the second in position 1, ... the 10th in position 9, and the 11th would loop back to the start and go in position 0. The get the numbers back, you would just have to print them out in reverse order from where you last put one in.
#include <iostream>
bool getNumber ( double numbers[], int & position );
void displayNumbers ( double numbers[], int position );
int main ( )
{
double lastTenNumbers[10];
int position = 0;
do {
getNumber ( lastTenNumbers, position );
} while ( position < 10 );
--position;
displayNumbers ( lastTenNumbers, position );
}
bool getNumber ( double numbers[], int & position )
{
std::cout << "Number: ";
std::cin >> numbers[position];
++position;
if ( std::cin ) returntrue;
returnfalse;
}
void displayNumbers ( double numbers[], int position )
{
int i = position;
std::cout << numbers[i--] << ' ';
while ( i != position )
{
std::cout << numbers[i] << ' ';
if ( i == 0 ) i = 9;
else --i;
}
std::cout << '\n';
}
better than pseudocode, but not good code so don't use it.