1

Working on an exercise and I think I'm pretty close to getting it right. The prompt asks for me to have the user say how many random numbers they want generated, and then to generate that many random numbers. For simplicity I'm having the random numbers be 0-1000. We're practicing using files too, so we want this written to a file.

#include <iostream>
#include <fstream>
#include <ctime>

int main()
    {
    int userinput,i, N, rdnum[N];
    std::ofstream ofs;

    std::cout << "How many random numbers do you want to generate?\n";
    std::cin >> N;

    srand(time(0));
    rdnum[N] = rand() % 1001;

    ofs.open("rand_numbers.txt");

    if (!ofs)
    {
        std::cout << "Open error\n";
        exit(0);
    }

    else if (ofs.good())
    {
        for (i = 0; i < N; i++)
        {
            std::cout << "Random numbers: " << rdnum[i] << std::endl;
            ofs << rdnum[i] << std::endl;
        }
    }
}

Thanks!

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
SalvGi
  • 59
  • 9
  • You're not currently using a `std::vector` in your code. Do you want to use a `std::vector`? – JohnFilleau Mar 10 '20 at 19:03
  • See [here](https://stackoverflow.com/questions/4926622/how-to-generate-different-random-numbers-in-a-loop-in-c) for generating random numbers in a loop, and [here](https://stackoverflow.com/questions/17984268/how-do-i-add-elements-to-an-empty-vector-in-a-loop) for how to add elements to a vector. You can also check the [std::rand](https://en.cppreference.com/w/cpp/numeric/random/rand) and [std::vector](https://en.cppreference.com/w/cpp/container/vector) reference pages. – JohnFilleau Mar 10 '20 at 19:06
  • @John I do not want to use vector if possible, although somebody has already suggested that it is probably better to use. In class we're using arrays right now, we haven't even touched vectors yet. It's a very elementary class. – SalvGi Mar 10 '20 at 19:13
  • @John I'll check those out, thanks! – SalvGi Mar 10 '20 at 19:13
  • It looks like you already know how to iterate through the array in your output loop, so I'll say you should do the same thing for generating your random values. Iterate through the array, generate a random value on each iteration, and assign that value to the current index of the array. – JohnFilleau Mar 10 '20 at 19:20
  • A note: after `if (!ofs)`, `else if (ofs.good())` can only be true. If the file stream is not bad, it must be good. There is a strong case for moving the is good check inside the for loop to ensure that the file is being written correctly. That check could be performed with something like `if (!(ofs << rdnum[i] << std::endl)) { // handle error }` – user4581301 Mar 10 '20 at 19:23
  • And look up the term "Variable Length Array" or "VLA". The C++ standard doesn't allow for VLAs, but many compilers *do*. To make code more portable, you should avoid them when possible. I think at your current level, it's fine to use a VLA in this program, but when you finish this assignment read up on the concept. VLA's are kind of an almost religious contention on this site, you can read [here](https://meta.stackoverflow.com/questions/376955/what-should-i-do-when-an-op-uses-variable-length-arrays-vlas-in-c) for information on the culture of VLAs on Stackoverflow and why they freak people out – JohnFilleau Mar 10 '20 at 19:25

1 Answers1

1
int userinput,i, N, rdnum[N];

Here, you declare an integer N leaving it with an indeterminate value.

You also declare an array of length N. The behaviour of using an indeterminate value like this is undefined.

Besides, N is not a runtime constant value, so using it as the size of an automatic array is ill-formed.

To create an array of runtime size, you have to use dynamic storage. The simplest way to create a dynamic array is to use std::vector. Note that for this simple program, no array is necessary since you could simply output the random number directly in the loop where it is generated.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Ah didn't know I couldn't do that, thanks I'll fix that up and see if it helps! I figured since the user was inputting the value of N that I could leave it blank – SalvGi Mar 10 '20 at 19:04
  • @Kyle in C++ order matters. You cannot lay out n expression like `c = a + b;` and expect `c` to be updated later as you change `a` and `b` later in the program. If the expression is in the code before `a` and `b` are supplied, `c` is computed before `a` and `b` are supplied. If you want `c` to be computed after `a` and `b` have been supplied (or recomputed after they've been changed) you have to do it yourself. The [As If Rule](https://en.cppreference.com/w/cpp/language/as_if) can kick in and change exactly when something happens (or even if) but it will not change the coded behaviour. – user4581301 Mar 10 '20 at 19:17