#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main() {
    srand(static_cast<int>(time(0)));
    int secret = rand() % 10 + 1;
    int guess;

    cout << "Guess a number between 1 and 10." << endl;
    cin >> guess

    while (guess != secret) {
        if (guess < secret) {
            cout << "Too low, try again." << endl;
        } else {
            cout << "Too high, try again." << endl;
        }
    }
    cout << "Correct!" << endl;

    return 0;
}

`

It looks to be a logic error. Try this and compare to your code:

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main() {
    srand(static_cast<int>(time(0)));
    int secret = rand() % 10 + 1;
    int guess;

    cout << "Guess a number between 1 and 10." << endl;
    guess = 0;

    while (guess != secret) {
        cin >> guess;
        if (guess < secret) {
            cout << "Too low, try again." << endl;
        } else {
            cout << "Too high, try again." << endl;
        }

    }
    cout << "Correct!" << endl;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.