Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

please give a C++ simple project using only loops,if else,swithes

+5 votes
asked Jun 20, 2025 by Umair Haider (170 points)

1 Answer

0 votes
answered Jun 22, 2025 by Proxmega Game (200 points)

Here you go,

#include <iostream>
using namespace std;

int main() {
    int secretNumber = 42;  // Hardcoded secret number
    int guess;
    char choice;

    do {
        cout << "Guess the secret number (1 to 100): ";
        cin >> guess;

        if (guess == secretNumber) {
            cout << "Congratulations! You guessed it right.\n";
        } else if (guess < secretNumber) {
            cout << "Too low! Try again.\n";
        } else {
            cout << "Too high! Try again.\n";
        }

        cout << "Do you want to try again? (y/n): ";
        cin >> choice;

        switch (choice) {
            case 'y':
            case 'Y':
                // loop continues
                break;
            case 'n':
            case 'N':
                cout << "Thanks for playing!\n";
                break;
            default:
                cout << "Invalid input. Exiting.\n";
                choice = 'n'; // force exit
                break;
        }

    } while (choice == 'y' || choice == 'Y');

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...