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

int findNum ()
{
    int num;
    cout<<"You have 10 chances to guess the number that is generated by this program."<<endl;
    cout<<"All numbers generated are of positive integers"<<endl<<"Good Luck!!";
    cout<<"Enter you guess ";
    cin>>num;
    return num;
}

string start (int guess)
{
    int count = 1;
    int num;
    string result;
    num = findNum ();

        if (num == guess)
        result = "You Win!!";
        
        else   {while (num != guess && count < 10)
                       {count = count + 1;
                              if (num == guess)
                                 {result = "You Win!!";
                                 return result;}
              
                              else if (num < guess)
                                      {cout<<"Higher"<<endl;
                                      num = findNum ();}
               
                              else if (num > guess)
                                      {cout<<"Lower"<<endl;
                                      num = findNum ();}
               }
               
          }
          if (count == 10)
          result = "You Lose!!";
          
          else
          result = "You Win!!";
          return result;
        
}

int main ()
{
    string result;
    result = start (+rand());
    cout<<result<<endl;
    system ("pause");
    return 0;
}

Recommended Answers

All 6 Replies

Look up srand().

Look up srand().

i tried srand but it keeps printing the first 3 couts

Then you did it wrong. Post your code how you tried it. strand( time(0) ); should appear only once in your program, preferably near the top of main() before any loops or other executable code.

And you may need to add the ctime header file to the list of includes. At least I think that's where the time() function is declared.

And you may need to add the ctime header file to the list of includes. At least I think that's where the time() function is declared.

I googled it and found a c++ website with the time thing you were talking bout thanks, it works perfectly now.

Hey, I remeber a way which can generate changing random numebers but i donot know that if it works properly coz i pretty much forgot that code a long while ago..

The Only Backdrop is that it needs a user input to stop changing random numbers..

#include <iostream>
using namespace std;

int main()
{
cout<<" Guess The Number";
int x;
cin >>x;

int y;

while(!cin.get())y=rand();

if (y==x)
{
cout<<"Win";
}
else
{
cout<<"LOSS";
}

}

This is not my average code. But i have just written it right now to show an example.

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.