Problem with void

I have just started to learn c++ so my code might not be the cleanest....Anyways, when I try to answer the "what do you want to do" question as soon as I press enter it ends the program.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream> //for using cout
#include <unistd.h> //lets the program wait
#include <string> //allows strings
using namespace std; //for using cout

void HOME(){
    //layout of the player home
    cout<<"Welcome to Your home\n";
    cout<<"There is a treasure chest by the window, type 'T' to see whats inside of it.\n";
    cout<<"There is a bag by the front door, to access it type 'B'.\n";
    cout<<"There is a closet door to your left, type 'C' to access it.\n";
    cout<<"The door that leads outside is to your right, type 'D' to go through it.\n";
    sleep(2);
    cout<<"What would you like to do?\n";
}

void CHESTLVL1(){
    
    cout<<"There is nothing in the chest"<<endl;
    
}

void BAGLVL1(){
    
    cout<<"There is a notebook in the bag.\n";
    sleep(1);
    cout<<"Would you like to look at it? Y or N.\n";
}

void CLOSETLVL1(){
    
    cout<<"There is a leather jacket and a motor cycle helmet.\n";
    sleep(1);
    cout<<"Do you want to take the leather jacket and helmet? Y or N.\n";
}

void NOTEBOOKLVL1(){
    
    cout<<"*You flip through the notebook*";
    cout<<"There are multiple drawings of mountains and planets.\n";
    cout<<"Maybe you can fill this notebook with more stuff?\n";
}

int main(){
    
    int hh1ans;
    int bh1ans;
    int ch1ans;
    
    cout<<"Welcome to Terraneous! This is a place like no other! You can be what ever you chose to be. Though, be weary your choices can be good or bad."<<endl; //introduction
    sleep(2); //waits for 2 senconds
    
    string name; //to get player name
    cout<<"What is your name? ";
    getline(cin, name);
    cout<<"Hello "<<name<<"!\n";
    sleep(0.5);
    
    HOME();
    cin>>hh1ans;
    if (hh1ans=='T'|| hh1ans=='t'){
        
        CHESTLVL1();
        sleep(3);
        HOME();
        
    }else if (hh1ans=='B'||hh1ans=='b'){
        
        BAGLVL1();
        cin>>bh1ans;
        if (bh1ans=='Y'||bh1ans=='y'){
            
            NOTEBOOKLVL1();
            sleep(9);
            HOME();
        }
        else (bh1ans=='N'||bh1ans=='n');{
            
            HOME();
        }
        
    }else if (hh1ans=='C'||hh1ans=='c'){
        
        CLOSETLVL1();
        cin>>ch1ans;e
        if (ch1ans=='Y'||ch1ans=='y'){
            
            cout<<"You take the leather jacket!";
            
        }else (ch1ans=='N'||ch1ans=='n');{
            
            HOME();
        }
    }
    
    
}



Problem with getting user input.

46
47
48
    int hh1ans;
    int bh1ans;
    int ch1ans;
would be better as
46
47
48
    char hh1ans;
    char bh1ans;
    char ch1ans;

because the user is expected to type a letter, not a number.

(or use string if the user may type a whole word - but then the rest of the code would need to change accordingly. For now, use char.)
Last edited on
Registered users can post here. Sign in or register to post.