Random Number Guessing Game

Hey so for school I have create a random number guessing. I'm currently have problems getting it to loop back to the beginning of the program if they press y or n.

Here is my source code


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    do
    {
    bool play;
    srand(time(0));
    int number=rand()%100;;
    int guess;
    int tries;
    char answer;
    
    cout<< "This is a random number guessing game"<<endl;
    cout<< "Please choose a number between 1-100"<<endl;
    cout<<number<<endl;
    tries=1;
    cin>>guess;
    
    while(guess!=number)                    
    {
                       if(guess<number)
    {
                       cout<< "Too low. Enter another number"<<endl;
                       tries=tries+1;
                       cin>>guess;
    }
                       if(guess>number)
    {                   
                       cout<< "Too high. Enter another number"<<endl;
                       tries=tries+1;
                       cin>>guess;
    }    
                       if(guess=number)
    {
                       cout<< "Correct!"<<endl;
                       cout<< "It took you "<<tries<<endl;
                       cout<< "1   try  = Epic"<<endl;
                       cout<< "2-4 tries= Expert"<<endl;
                       cout<< "5-7 tries= Good"<<endl;
                       cout<< "7-9 tries= Decent"<<endl;
                       cout<< "10+ tries= Total Fail"<<endl;
                       cout<< "Play again? Y=Yes N=No"<<endl;
                       cin>>answer;
    }
    }
    while(answer='y' or 'n')
    {
    if(answer='y')
    {
                play=true;
    }
    if(answer='n')
    {
                play=false;
    }
    }
    }   
while(play);

    system("PAUSE");
    return 0;
}



Thank you for your assistance.
Last edited on
this post was similar to yours.
http://www.cplusplus.com/forum/general/51398/
@tmaynard:

1. Use code tags

2. What on earth is while(answer='y' or 'n') meant to do.

3. If you actually DO want that line, it should be
while (answer == 'y' || answer == 'n')

4. You REALLY should try not to use system("PAUSE");, it's generally frowned upon.
thanks for your replies.
@jalfor

Fixed my code. Its in code tags now and I was
trying to do number 3 and failed as you saw.
As of now I'm not aware of any other pausing code.
I'm looking for it now as I type,
but I appreciate your helping a complete novice.

@Azagaros

Didnt notice that post.
It helped me make my code shorter and easier to understand,
but still have the same problems.
I appreciate you pointing that out to me a complete novice.
Last edited on
From the BloodShed Dev-C++ guide:
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
include <stdio.h>

main()
 {
   int target;
   int guess;
   int again;

   printf("\n Do you want to guess a number 1 =Yes, 0=No ");
   scanf("%d",&again);

   while (again)

    {
      target = rand() % 100;
      guess  = target + l;

      while(target!=guess)
       {
         printf("\n What is your guess ? ");
         scanf("%d",&guess);

         if (target>guess) printf("Too low");
         else printf("Too high");
       }

      printf("\n Well done you got it! \n");
      printf("\nDo you want to guess a number 1=Yes, 0=No");
      scanf("%d",&again);
    }
 }
return 1;
Last edited on
closed account (10oTURfi)
I laughed so hard when I saw while(answer='y' or 'n')
You just made my day.
@Krofna
lol yeah c++ gets me annoyed at times so ill type whatever.
Topic archived. No new replies allowed.