Learning C++, practice with for/while loops

Had an idea to learn.
Layout below.

// Program layout
// There is a room of 10 doors
// Computer chooses at random which are open and which are closed
// One door leaves the house
// The computer the checks if each door is open or closed
// If the door is closed, computer moves to the next
// If the door is open, computer asks if to close or not to close
// If response == close, computer closes the door and moves to the next
// If response == enter, computer goes through the door to the next room of 10 doors
// unless that door exits the house
// If you don't go through any door, computer outputs "You are trapped!\nTry again? (Y/N)\n"

I have made a for loop that counts a door number.
Code below.

1
2
3
4
5
6
7
8
int main()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "door number: " << i + 1 << endl;
        
    }
}


I recognize that I need to make a variable for every door that is between 0 and 1. I don't know how to separate int i as it's own variable for each door. As I type this, I think that I can make it choose if a door is open or closed at each coming to a door. How then would I make the computer choose which door leaves the house at random before the for loop is initiated yet changes every time the for loop is initiated? (The for loop is the room of doors?)

EDIT:
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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

// Program layout
// There is a room of 10 doors
// Computer chooses at random which are open and which are closed
// One door leaves the house
// The computer the checks if each door is open or closed
// If the door is closed, computer moves to the next
// If the door is open, computer asks if to close or not to close
// If response == close, computer closes the door and moves to the next
// If response == enter, computer goes through the door to the next room of 10 doors
// unless that door exits the house
// If you don't go through any door, computer outputs "You are trapped! Try again? (Y/N)"


void Room()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "door number: " << i + 1 << endl;
        Door();
    }
}

int Door()
{
    
}

int main()
{
    Room();    
}


EDIT 2:
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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

// Program layout
// There is a room of 10 doors
// Computer chooses at random which are open and which are closed
// One door leaves the house
// The computer the checks if each door is open or closed
// If the door is closed, computer moves to the next
// If the door is open, computer asks if to close or not to close
// If response == close, computer closes the door and moves to the next
// If response == enter, computer goes through the door to the next room of 10 doors
// unless that door exits the house
// If you don't go through any door, computer outputs "You are trapped! Try again? (Y/N)"

int Door()
{
    srand(time(NULL));
    int DoorPos = rand() %2;
    cout << DoorPos << endl;
}

void Room()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "door number: " << i + 1 << endl;
        Door();
    }
}

int main()
{
    Room();
}


I am now stuck because DoorPos outputs as the same each time. How to fix this, I wonder.

EDIT 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Room()
{
    for (int i = 0; i < 10; i++)
    {
        cout << "door number: " << i + 1 << endl;
        Door();
        if (DoorPos == 1)
        {
            cout << "This door is open! Enter? (Y/N)" << endl;
            cin >> response;
            if (response == Y || response == y)
            {
                
            }
        }
    }
}

I need to rewrite this to include a few while loops.

EDIT 4:
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
67
68
69
70
71
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

// Program layout
// There is a room of 10 doors
// Computer chooses at random which are open and which are closed
// One door leaves the house
// The computer the checks if each door is open or closed
// If the door is closed, computer moves to the next
// If the door is open, computer asks if to close or not to close
// If response == close, computer closes the door and moves to the next
// If response == enter, computer goes through the door to the next room of 10 doors
// unless that door exits the house
// If you don't go through any door, computer outputs "You are trapped! Try again? (Y/N)"

char response;
int EntDoor;

int Door()
{
    srand(time(NULL));
    int DoorPos = rand() %2;
    cout << DoorPos << endl;
    return DoorPos;
}

void Room()
{
    while(true)
    {
        for (int i = 0; i < 10; i++)
        {
            cout << "Door number: " << i + 1 << endl;
            Door();
            if (DoorPos == 0)
            {
                cout << "This door is closed. Moving on." << endl;
            }
            else
            {
                cout << "This door is opened! Enter? (Y/N)" << endl;
                while(true)
                {
                    cin response;
                    if (response == Y || response == y)
                    {
                        EntDoor = 1;
                        break;
                    }
                    if (response == N || response == n)
                    {
                        EntDoor = 0;
                        break;
                    }
                    else
                    {
                        cout << "That was an invalid response. Enter? (Y/N)" << endl;
                    }
                }
            }
        }
    }
}

int main()
{
    Room();
}
Last edited on
For your if statement using the condition of response, it has to be in apostrophes.
Topic archived. No new replies allowed.