0

I want to ask for help with this code. I'm trying to create a code where it will randomly generate a number and once it lands to a specific number, it will trigger a message saying "You won something" or if it lands to a number that isn't the specific one then it will trigger "You won nothing".

#include <ctime>
#include <iostream>

using namespace std;

int main() {
    jump:
    srand((unsigned) time(0));
    int randomNumber;
        for (int index = 0; index = 1; index++) {
            randomNumber = (rand() % 5) + 1;
            cout << randomNumber << endl;
            if (randomNumber == 5) {
                cout << "You won something!\n" << endl;
                break;
            } else {
                cout << "You won nothing!\n" << endl;
                goto jump;
            }
        }
}```
  • 1
    TL:DR: Move the `jump` label down one line. – user4581301 Mar 06 '22 at 15:52
  • 1
    That said, `goto` can be hard to get right, and even when you do get it right, it's almost impossible to convince people that you did get it right. On the rare occasion where `goto` is the right tool for the job, I find it takes more time to get `goto` through a code review and years of re-explaining why I used it than it would have taken to write the code without the `goto`. I don't think you need the `goto` at all here. It's causing another bug you'll probably see in a few minutes. – user4581301 Mar 06 '22 at 15:56
  • also, this is C++, as you rightly notice. `srand` and `rand` are part of libc, so C. the libc random number generator is **bad**. You shouldn't use it! Especially not because C++ has `std::random` built in, which has a good and easy to use random number generator. Plus, `goto` is a terrible idea in C++. Whatever you're reading to learn C++, drop it; it was written by someone who learned to write C from before ca 1990; really, really bad. Find something else to learn C++. What you're currently reading is actively harmful. – Marcus Müller Mar 06 '22 at 15:58
  • (`rand` is bad because the random number generation method is flawed – the numbers aren't very well-distributed in practical implementations. Even `random`, which already is better, is still terribly bad, compared to modern PRNGs. Also, it uses a global internal state, which is nothing but trouble and a really bad design pattern – you can't have multiple independent random number generators with `rand`, not even with `rand_r`.) – Marcus Müller Mar 06 '22 at 16:02
  • atop of that, seeding with the current time in seconds is another anti-pattern that by now should have been removed from all serious literature. It's seriously something from the 1970s, where it was unlikely that two programs could be started within the same second. On modern computers, we start thousands of programs in a second. This is all a pointer that the material with which you're learning is really old, and wasn't even good when it was new. – Marcus Müller Mar 06 '22 at 16:05
  • Here is an example of how your program would look if you used actual C++, instead of libc and `goto`: [`uniform_int_distribution` example](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) – Marcus Müller Mar 06 '22 at 16:07

0 Answers0