strange error
Not sure how I messed up, but I get an error that simply states "exited with non-zero status"
sigh
What did I do wrong?
PS If it helps, I use repl.it to compile and write my code
1 2 3 4 5 6
|
srand(time(NULL));
vector<int> listo;
for(int i = 0; i<20; i++){
listo[i] = rand() % 100 + 1;//Fills vector with 20 random integers
std::cout << listo[i] << " ";//Prints the vector
}
|
You haven't allocated memory for the vector yet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <vector>
#include <cstdlib>
#include <chrono>
int main()
{
srand(std::time(NULL));
std::vector<int> listo;
for(int i = 0; i<20; i++){
listo.push_back(rand() % 100 + 1);//Fills vector with 20 random integers
std::cout << listo[i] << " ";//Prints the vector
}
}
|
edit: another alternative is to allocate vector memory upon declaration:
1 2 3
|
std::vector<int> listo(20);
//then
listo[i] = rand()%100+1;
|
ps: rand() is not a good way to generate random #s, search this forum over the past week or so for better alternatives
Last edited on
Haha LOL.
Now I realise I used a vector rather than an array.
You didn't declare the size of the vector.
You can make it
1 2 3 4 5
|
vector<int>listo(20);
for(int i = 0; i<20; i++){
listo[i] = rand() % 100 + 1;//Fills vector with 20 random integers
std::cout << listo[i] << " ";//Prints the vector
}
|
But I would assume you wanted to use
|
listo.push_back(rand() % 100 + 1);
|
Topic archived. No new replies allowed.