Jenny Marks
Jenny Marks

Reputation: 13

Vector in C++, my code works but not how I want it to?

So I am trying to make a basic program, I want to make a vector called numbers, and fill it with 10 random numbers between one and a hundred. This is what I have so far;

#include <iostream>
#include <vector>

using namespace std;

int main()
{
   vector<int> numbers;

   int in = rand() % 100 + 1;

   for (int i = 0; i < in; i++)
   {

      int n = rand() % 100 + 1;

      numbers.push_back(n);

      cout << "Number is: " << numbers[i];

      system("pause");
      return 0;
   }
}

It only outputs one number and I am looking to output ten random ones.

Upvotes: 0

Views: 93

Answers (6)

ruirodrigues1971__
ruirodrigues1971__

Reputation: 342

You should put the system ("pause") and return outside the loop, not inside:

... 
for () {...}

system ("pause");
return 0;

Upvotes: 0

2785528
2785528

Reputation: 5576

The following idea has been useful for me...

//Initialize in ctor
std::vector<int> vecI;
for (int i=0; i<100; ++i)  vecI.push_back(i); 

time_t seed = std::chrono::system_clock::now().time_since_epoch().count();

std::shuffle(vecI.begin(), vecI.end(), std::default_random_engine(seed));

// in ctor, ok to display for debug, or disable for release
if(debugEnabled) {
   for (int i=0; i<100; ++i)
      std::cout << vecI[i] << " ";  // 
std::cout << "\n" << std::endl;  
}

In application, you get constant time access to random values.

// easy to use in application
for (int i=0; i<10; ++i)
   testWith (vecI[i]);  // ten selections from values in range 0..99


// easy to test with all in-range values, in shuffled order
for (int i=0; i<100; ++i)
   testWith (vecI[i]);  // all values in shuffled order


// test with all in-range values, in order
for (int i=0; i<100; ++i)
   testWith (i);  // all values in order

Upvotes: 0

The19thFighter
The19thFighter

Reputation: 103

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // Declare and define a vector holding ints
    vector<int> numbers;

    // Run loop 10 times, increase i by one every time it's finished
    for (int i = 0; i < 10; i++)
    {
       // Generate a random number, get the last 3 digits, add one
       int n = rand() % 100 + 1;

       // Push that number back to the the vector
       // Like adding the number to a list
       numbers.push_back(n);

       // Print the current number from inside the vector
       // by using the counter/index i using an array-like syntax
       cout << "Number is: " << numbers[i] << std::endl;

    }
    // This executes the command pause, after it has been terminated,
    // stop the execution, with "success" by returning 0
    system("pause");
    return 0;
}

This is most likely what you're looking for.

However be aware, that using a vector in this case creates a little overhead. I would simply print n instead of pushing the number onto the vector just to retrieve it afterwards. But as this is probably just a preparation for a more advanced use of vectors afterwards this should be fine.

Output:

Number is: 84
Number is: 87
Number is: 78                                                                                                                                                                                    
Number is: 16                                                                                                                                                                                    
Number is: 94                                                                                                                                                                                    
Number is: 36                                                                                                                                                                                    
Number is: 87                                                                                                                                                                                    
Number is: 93                                                                                                                                                                                    
Number is: 50                                                                                                                                                                                    
Number is: 22       

Upvotes: 1

PRDeving
PRDeving

Reputation: 669

In C and C++, random uses a seed to generate the output, indeed, if you don't seed the random, the random will generate the same number once and over again.

You can -and should- seed it with timestamp

#include"time.h"
srand(time(0));

after this, the random will generate "true" random numbers.

ALSO, you have to remove this two lines:

 system("pause");
  return 0;

It breaks your loop in first iteration

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 118021

Remove the following line

system("pause");

This will stop the continuation of your program. Also move your return 0; to the last statement in main, otherwise it will stop after only the first iteration.

Also if you only want to generate 10 numbers, then set that as your loop's stop condition

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;
    const int in = 10;    // Generate 10 random numbers

    for (int i = 0; i < in; i++)
    {
        int n = rand() % 100 + 1;

        numbers.push_back(n);

        std::cout << "Number is: " << numbers[i] << std::endl;
    }

    return 0; // now you may return since the loop is complete
}

Working demo

Upvotes: 2

Havenard
Havenard

Reputation: 27934

return 0 inside the loop will cause it to terminate after the first iteration. return is not meaningful to any loop in C, this statement is used to terminate the function.

You would probably see this right away if the code was properly indented. Indenting matters.

Also you are not telling it to loop 10 times, you are telling it to loop in times, and in is rand() % 100 + 1.

Upvotes: 0

Related Questions