So I need to write a program where the user tries to guess a magic number. The only thing that I do not know how to do is set the max. amount of guesses to 5. This is what I have so far. Can someone please show me how to limit the user to 5 guesses? Thanks so much!

#include <iostream>
using namespace std;

int main()
{
    int magic=6, guess;

    do
    {
        cout <<"Guess the magic number. The number is anywhere between 0 and 9: \n";
        cin >>guess;
        if (magic == guess)
        {
            cout <<"You guessed right. \n";
        }
        else if (magic<guess)
        {
            cout << "You guessed too high.\n";
        }
        else
        {
            cout << "You guessed too low.\n";
        }
    }while (magic!= guess);
        return 0;
    }
#include <iostream>

using namespace std;
int magic = 7;
int guess;
int trys =0;
int main(){
    do{
        cin>>guess;

        if(guess!=magic){
        ++trys;
        }

        if(guess<magic){
        cout<<"TOO low!"<<endl;
        }

        if(guess>magic){
        cout<<"Too high!"<<endl;
        }

        if(guess==magic){
        cout<<"Woohoo"<<endl;
        break;
        }

    }while(trys!=5);
    if(trys==5){
    cout<<"Boo! you lost!"<<endl;
    }
}

This is the code.

commented: We are not a homework service. Do NOT write the code for people. HELP them write their own code. -3

Perhaps, to make your code more efficient, you might use srand() so that, you will get random magic numbers.

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.