Please! Help Beginner

As many times as the user wants, play a guessing game with the user. The program randomly selects a secret number in the range [1,100], then lets the user guess until he gets the number. When the user is wrong, tell him or her whether he or she is too high or too low. When the user guesses the right answer, ask whether the user wants another game by inputting 'y' or 'n'. For each game, the program selects another random number as the answer. An example run of your program might go as
Last edited on
What do you need help with?
making a program c++ ^^^^ above

Guess the secret number:
7
No, too low
10
No, too low
20
No, too low
30
No, too low
42
That's right! Want to play again (y/n)? y
Guess the secret number:
84
That's right! Want to play again (y/n)? y
Guess the secret number:
2
No, too high
87
No, too high
1
That's right! Want to play again (y/n)? n
Ok, bye
Shows us what you have so far, and we'll be happy to help.
This is what I have and it is not working ..





#include <iostream>
#include <iomanip>
using namespace std;
char chr;

int main()
{
int number;
int guess;
int tries;
char answer;

srand(number>0);
do
{
number=rand()%100+1;;
cout<<"Enter a number between 1 and 100"<<endl;
cin>>guess;
if (number<guess)
cout<<"Too high try again"<<endl;
tries=1;
} while(number>guess);
cout<<"Too low try again"<<endl;
tries++;
if(number==guess)
cout<<"Congratualtions!! "<<endl;
cout<<"You got the right number in "<<tries<<" tries";
do
{
cout<<"Would you like to play again? Enter Y/N";
cin>>answer;
if ('N')
cout<<"Thanks for playing!";
} while(answer='Y');



cin>>chr;
return 0;

}
closed account (oGN8b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
  srand (time(0));

  char answer ('Y');

  while (answer == 'Y')
  {
    const int number (rand() % 100 + 1);

    int tries (0);

    int guess;

    std::cout << "Enter a number between 1 and 100" << std::endl;
    std::cin >> guess;

    ++tries;

    while (guess != number)
    {
       if (guess > number)
       {
         std::cout<<"Too high try again"<<std::endl;
       }
       else
       {
         std::cout<<"Too low try again"<< std::endl;
       }

       std::cin >> guess;
       ++tries;
    }

    std::cout << "Congratulations!! " << std::endl;
    std::cout << "You got the right number in " << tries << " tries" << std::endl << std::endl;

    answer = ' ';

    while ((answer != 'Y') && (answer != 'N')) // Only uppercase taken
    {
      std::cout << "Would you like to play again? Enter Y / N" << std::endl;
      std::cin >> answer;
    }
  }

  return 0;
}
Last edited on
You need to add
1
2
#include <cstdlib> //For rand and srand
#include <ctime> //For the time function 


You can delete #include <time>

Change this:
srand (time());

To:
1
2
3
4
5
    //Get the sytem time.
    unsigned seed = time(0);

    // Seed the random number 
    srand(seed);


Change:
std::cout<<"Too high try again"<<endl;
To:
std::cout<<"Too high try again"<<std::endl;

Change:
std::cout<<"Too low try again"<<endl;
To:
std::cout<<"Too low try again"<<std::endl;

Change:
answer = '';
To:
answer = ' ';

Last edited on
@ lechuga2000, You are correct. It is why I said to use those headers #include <cstdlib> //For rand and srand and #include <ctime> //For the time function and not the #include <stdlib.h> nor #include <time.h>

According to the ISO Standard for C++,
"The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible with C."

That being said, if the compiler is old and not in accordance with the ISO Standard for C++, then that it is another issue.
You are absolutely correct.
Topic archived. No new replies allowed.