Help 4 this code

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <windows.h>
#include <ctime>
using namespace std;

int main()
{
int num, numrand, score;
char yn;
srand(time(NULL));

cout << "Do you want to play a game?(y/n)" << endl;
cout << "Your answer: "; cin >> yn;
if(yn == 'n' || yn == 'N')
return 0;
if (yn == 'y' || yn == 'Y')
cout << "Program will imagine a from 0 to 5 and you will try to guess it." << endl;
while(true){
cout << "Your number(Write -1 to quit.): "; cin >> num;
numrand = rand()%6;
if (numrand == num)
cout << "You win!" << endl;
Sleep(400);

if (num == -1)
break;
else
cout << "You lost!" << endl;
Sleep(400);
}
else
return 0;
}


Im trying to make a number guess game and it doesnt work
Last edited on
I think it should be something like this :

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

#include <iostream>
#include <cmath> //Unused Header
#include <cstdlib>
#include <windows.h> //Unused Header
#include <ctime>
using namespace std;

int main()
{
    int num, numrand, score;
    char yn;
    srand(time(NULL));

    cout << "Do you want to play a game?(y/n)" << endl;
    cout << "Your answer: "; cin >> yn;
    if(yn == 'n' || yn == 'N')
        return 0;
    if(yn == 'y' || yn == 'Y')
    {    
        cout << "Program will imagine a from 0 to 5 and you will try to guess it." << endl;
        while(true){
            cout << "Your number(Write -1 to quit.): "; cin >> num;

            if(num == -1)
                return 0;
            numrand = rand() % 6;
            if (numrand == num)
                cout << "You win!" << endl;
            else
                cout << "You lost!" << endl;

            sleep(400);

        }
    }
    else
    {
        cout << "Error : invalid character." << endl;
        // Restart the input process
    }
}


Last edited on
Topic archived. No new replies allowed.