I'm new to this site and am desperate for help. I'm learning C++ for university, and have just received my second project. it goes like this:

Write a program in which:
A random integer between 0 and 500 is generated at the beginning.
The user is asked to guess the number.
The user is told whether her/his guess was too high or too low, and asked to guess again, until the guess is correct.

I have made numerous attempts, and just cant get it to work!! This is what i did;

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int num, guess;

    do{

    num=rand()%500;

    cout<< "Guess the secret number: ";
    cin>> guess;}

    while (guess<num);
    cout<< "Sorry, that guess is too low.";

    while (guess>num);
    cout<< "Sorry, that guess is too high.";

    while (guess!=num);
    cout<< "Congratulations, you got it!";

    return 0;
}

I know this is really simply, but I just cant get my head round it! I don't want to be just given the answer by the way, just some pointers where i might have gone
wrong, because I really want to learn this. Any help would be appreciated! Thanks

you could wrap it in one while loop
you may use if...else and break to solve this question
a simple example

while(1)
{
  if(num < xx) .....
  ......
  else if(num == yy) break;
}

Really so for being so stupid with this, but I don't know where to put the break! This is what i tried( really sorry about this, but my lecturer goes so fast I cant keep up!)

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int num, guess;

    cout<<"Guess the secret number: ";
    cin>> guess;

while (num=(rand() %500));{

    if (guess<num){ cout<< "Sorry, that guess is too low.";}

    else if (guess>num){cout<< "Sorry, that guess is too high.";}

    else if (guess==num){cout<< "You got it!"; break;}



    }


return 0;
}

My build log reads:

C:\Users\Joe\My Programmes\hw2\main.cpp||In function 'int main()':|
C:\Users\Joe\My Programmes\hw2\main.cpp|12|warning: suggest parentheses around assignment used as truth value|
C:\Users\Joe\My Programmes\hw2\main.cpp|18|error: break statement not within loop or switch|
||=== Build finished: 1 errors, 1 warnings ===|

Also sorry, but dont know how to do the code tag thing, looking it up now!

the break would kick you out of the nearest loop

while (num=(rand() %500));

should not put ";"

you could just use "else" instead of "else if" if that is the last option

else (guess==num){cout<< "You got it!"; break;}
while (num=(rand() %500))

this line has a small problem since num may become 0
if num == 0, you would never go into the loop

you are almost there, good luck

I messed about a bit with it and ended up with this. it works but the 'random' number is always 41!

#include <iostream>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    int num, guess;
    num = rand() % 500 ;


    do{
    cout<< "Guess the random number: ";
    cin>> guess;

        if (guess<num){
        cout<< "Sorry, that is too low."<<endl;}

        else if (guess>num){
        cout<< "Sorry, that is too high."<<endl;}

    }
    while (guess !=num);
    cout<<"Congratulations, you've got it!"<<endl;

    return 0;
    }
srand

you need to provide a seed

I know i've said it a lot already, but really, thank you. My final code looks like this;

#include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int main()
    {
    srand(time(0));
    int num, guess;
    num = rand()%500;

    do{
    cout<< "Guess the random number: ";
    cin>> guess;

        if (guess<num){
        cout<< "Sorry, that is too low."<<endl;}

        else if (guess>num){
        cout<< "Sorry, that is too high."<<endl;}

    }
    while (guess !=num);
    cout<<"Congratulations, you've got it!"<<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.