whats wrong with this code?

Im doing a lottoline that writes out 7 random numbers in 10 lines and i need to use a vector to save the element to write out later but i got no clue what i have been missing.
Cuz when i run the code i just get all the 10 lines out but its the same numbers.


#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
char jn;
vector <int> v1;
bool flag = true;
while (flag == true){
cout << "Vill du starta slumpningen (j/n)" << endl;
cin >> jn;
if (jn == 'j' || jn == 'J')
{
int rad = 1;
int tal;
cout << endl;
while (rad < 11)
{
for (int v = 0; v < 7; v++ )
{

tal = rand () % 35 +1;
v1.push_back(tal);

cout << v1.at(v) << " ";
}
cout << setw(10) << " Lottorad : " << rad++ << endl;
}
cout << endl;
}
else if (jn =='n', 'N')
{
cout << "Tack " << endl;
flag = false;
}
}
}
Last edited on
Look at this loop

for (int v = 0; v < 7; v++ )
{

tal = rand () % 35 +1;
v1.push_back(tal);

cout << v1.at(v) << " ";
}

You always output v1[0], v1[1],..., v1[6] because you use as the index variable v that has the range of values [0, 7).

Change cout << v1.at(v) << " "; to cout << v1.back() << " ";
Last edited on
Oh that explains why it only wrote out the same line 10 times.
thanks alot for the fast respond.
I got a other question about this code.

If I want to make every random number to come out in order for every line from 1 to 35 what would the code be to do that?
If I have correctly understood your question then you can sort values of each line.
Topic archived. No new replies allowed.