You say you want the numbers to be stored in a vector, but you don't have any code to do that, and neither do you have the #include for vector defined either.
It pretty much seems to be doing exactly what you're telling it to; you just need to add in the extra bits that you need.
Most of those #include headers you don't need, but to use vectors you need to use
#include <vector>
.
Also, with the
rand()
function, you'd often seed it at the beginning of main(), using
time(), for which you also need
#include <ctime>
.
No doubt there are far more perfect ways to accomplish what you want, but the code below may give you some ideas in the right direction:
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
|
#include<iostream>
#include<Windows.h>
#include <vector>
#include <ctime>
int main()
{
srand(time(0)); // seeds the pseudo random number generator
int maxnum=0, finito=0; // meaningful variable names
std::vector<int> mynumbers; // vector to store the numbers
do {
std::cout << "Enter max number: ";
std::cin >> maxnum;
// print out a bunch of random numbers
for (int i = 0; i < maxnum; i++)
{
int random = rand()+1;
std::cout << random << std::endl;
// store each number in vector
mynumbers.push_back(random);
}
std::cout << std::endl;
if (GetAsyncKeyState(VK_ESCAPE)) finito=1;
if (finito)
for(std::size_t i=0; i<mynumbers.size(); ++i)
std::cout << mynumbers[i] << " - ";
} while (finito == 0);
return 0;
}
|
The
GetAsyncKeyState(VK_ESCAPE) bit does work, but only after you've pressed ESCAPE, and then entered another input. You'll need to play around with that some more.
These links may also help you:
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/vector/vector/
https://www.geeksforgeeks.org/vector-in-cpp-stl/
Questions about the "GetAsyncKeyState" function
(a)
http://www.cplusplus.com/forum/windows/6632/
(b)
https://stackoverflow.com/questions/45497190/c-getasynckeystate-does-not-register-key-press-immediately
Also, you still have an open question at:
http://www.cplusplus.com/forum/beginner/250367/ - Maybe you could update it to mark as solved, or provide additional info if you need more help.
Hope that helps.