-2
#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.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Aaditya
  • 1
  • 2
  • The compiler that is used is way important than the IDE. CLion has nothing to do with this. Also you failed to seed the random number generator so expect the same number every time. – drescherjm Aug 24 '22 at 16:56
  • i am new to coding can you suggest a fix. – Aaditya Aug 24 '22 at 17:03
  • Look at this example: [https://en.cppreference.com/w/c/numeric/random/rand](https://en.cppreference.com/w/c/numeric/random/rand) – drescherjm Aug 24 '22 at 17:06
  • [https://stackoverflow.com/questions/3539398/how-does-rand-work-does-it-have-certain-tendencies-is-there-something-better](https://stackoverflow.com/questions/3539398/how-does-rand-work-does-it-have-certain-tendencies-is-there-something-better) – drescherjm Aug 24 '22 at 17:07
  • thanks coming from python i was using rand() as random so i didn't know you have to declare a seed . – Aaditya Aug 24 '22 at 17:19
  • *it is not working* is the kiss of death for many questions. Explain how it is not working. Where possible demonstrate the problem by showing the expected output and the actual output. – user4581301 Aug 24 '22 at 17:26

1 Answers1

1

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!
sweenish
  • 4,793
  • 3
  • 12
  • 23