简体   繁体   中英

rand() function in JettBrain Clion not working

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    int correct_number , guessed_number;
    correct_number = rand() ;
    cout << correct_number << endl;
    cout <<  "Enter your guess (0-9) : ";
    cin >> guessed_number;
    while (guessed_number == correct_number){
        cout << "Guess is Wrong , Tre Again \n"<< "Enter your guess";
        cin >> guessed_number;
    }
    cout << "Well guessed!";
    return 0;
}

This is a Code i build as a guessing games where user input a number as guess until it match with correct_number and then it breaks the loop. But somehow it is not working on Clion though it works on a random online c++ compiler.

There's a lot to unpack. Here's a mini code review:

#include <iostream>
#include <cstdlib>  // Prefer <random> for C++

using namespace std;  // Bad practice

int main() {
    int correct_number , guessed_number;  // Prefer each variable on its own line
    correct_number = rand() ;  // Will be the same every time; not seeded
    cout << correct_number << endl;  // Prefer '\n' over std::endl
    cout <<  "Enter your guess (0-9) : ";  // rand() was not constrained
    cin >> guessed_number;
    while (guessed_number == correct_number){  // While the guess is correct?
        cout << "Guess is Wrong , Tre Again \n"<< "Enter your guess";
        cin >> guessed_number;
    }
    cout << "Well guessed!";
    return 0;
}

I can appreciate someone coming from a different language, but do so only with your knowledge of principles. Use documentation to learn how to use the language. rand() has a set range, and it's not 0-9.

#include <iostream>
#include <cstdlib>

int main() {
  std::cout << "Range is: 0 - " << RAND_MAX << '\n';
}

Output:

Range is: 0 - 2147483647

Furthermore, rand() must be seeded, and seeded only once in your entire program. Doing so at the beginning of main() is usually sufficient there. Not seeding will result in the same output every time the program is run. That kind of determinism can be good for testing, but not for actual use.

But, what's better is to throw rand() out completely and use the classes provided in <random> . It's a bit more code, but the PRNGs are better, and separation of the PRNG generation from your distribution is desirable.

Here's your program with the issues fixed and rand() replaced with functionality from <random> .

#include <iostream>
#include <random>

int main() {
  int correct_number;
  int guessed_number;
  std::mt19937 prng{std::random_device{}()};
  std::uniform_int_distribution<int> range(0, 9);

  correct_number = range(prng);
  std::cout << correct_number << '\n';

  std::cout << "Enter your guess (0-9) : ";
  std::cin >> guessed_number;
  while (guessed_number != correct_number) {
    std::cout << "Guess is Wrong , Try Again \n"
              << "Enter your guess : ";
    std::cin >> guessed_number;
  }
  std::cout << "Well guessed!\n";

  return 0;
}

Output:

❯ ./a.out 
7
Enter your guess (0-9) : 5
Guess is Wrong , Try Again 
Enter your guess : 7
Well guessed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM