Text Adventure Like Torn.

I have been trying to make a text adventure game based on torn, but with only two choices. Employe and Hacker. Or, if your feeling genourus, maybe add in "Criminal". I have searched and tested but nothing i seem to try works. Most of the problem was the login and register part, but i got that down.


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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <Windows.h>
#include <String>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include<fstream>
#include <time.h>

using namespace std;

int employe = 10;
int hacker = 20;

double iSecretMoneyAWeek = ceil(rand() * 2);
double iSecretSickly = ceil(rand() * 2);
double iSecretFired = ceil(rand() * 2);
double iSecretJailed = ceil(rand() * 2);

int goToWork = 11;
int goHome = 21;
int goToStore = 31;

int reply1;
int reply2;
int reply3;
int reply4;
int reply5;

string goToJail = "You were caught and sent to jail.";

string myCareer;

int main()
{
    string
        command, //to choose what to do each time
        name, password, //the ones that are found in the file
        inName, inPassword, //the ones you are going to input from keyboard
        registerName, registerPassword; //also what you're going to input
        //and if you know C strings, just replace that with something like
        /*
        char
        command[9],
        name[31], password[31], //it could be any size, but like this you have got 30 characters at your
        //disposal, if you consider it to be enough
        inName[31], inPassword[31],
        registerName[31], registerPassword[31];
        */
    while (1)
    {
        cout << "(register/exit/login)\n"
            << "Command: ";
        getline(cin, command);
        //(for C strings)
        //cin.get(command, 9);
        //cin.get();
        if (command == "exit") //(for C strings) if (!strcmp(command, "exit"))
        {
            return 1;
        }
        if (command == "register") //(for C strings) if (!strcmp(command, "register"))
        {
            //open file for registration
            ofstream g("registration.txt"); //ofstream is the one for getting data from the file, 
            //and the file does not even have to exist. If it's ofstream, it'll take care of it for you.
            //but be warned that if there is a file called "registration.txt" in the name folder as the
            //.exe file, the contents will be deleted
            if (!g.is_open()) //if it's not open, then there is no such file with the given name inside
            //the folder (that is, in the folder where the .exe file is going to be)
            {
                cout << "could not open file\n"; //just so that you know why it won't work if it doesn't
                return 0;
            }
            cout << "\n\n\n" //3 newlines
                << "New Username: ";
            getline(cin, registerName); //input from keyboard will go into registerName
            cout << "New Password: ";
            getline(cin, registerPassword); //input from keyboard will go into registerPassword
            g << registerName; //this basically says "put whatever's to the right (registerName) into
            //g ("registration.txt")".
            g << '\n'; //and now there will be a new line
            g << registerPassword; //and now the password
            //all placed safely in the file that g opened
            g.close(); //always make sure you close the file, or else you might end up with some nasty
            //stuff in the memory
        }
        if (command == "login") //(for C strings) if (!strcmp(command, "login"))
        {
            //open file, and then put the name and password into the strings
            ifstream f("registration.txt"); //ifstream is the one for getting data from the file, and
            //let us assume you've already created a file called "registration.txt"
            if (!f.is_open()) //if it's not open, then there is no such file with the given name inside
            //the folder (that is, in the folder where the .exe file is going to be)
            {
                cout << "could not open file\n"; //just so that you know why it won't work if it doesn't
                return 0;
            }
            getline(f, name, '\n'); //reads the user name from file f (which is using "registration.txt")
            getline(f, password, '\n'); //reads the password from file f (which is using "registration.txt")
            //also, if you tell the file to get you that text up until '\n', that's when you know it reads
            //the whole line at most, and won't go any further
            //and that is done by the 3rd parameter
            f.close(); //you don't need it open now, since you have the name and password from the file

            //login
            while (1)
            {
                //you are going to input the name and password here
                cout << "\n\n\n"
                    << "Enter Username: ";
                getline(cin, inName);
                cout << "Enter Password: ";
                getline(cin, inPassword);
                //or this, if you are working with C strings (second version of declaration)
                //cin.get(inName, 31);
                //cin.get();
                //cin.get(inPassword, 31);
                //cin.get();
                //and the "cin.get()" after each input line is necessary, or else[...]
                //no idea what's happening inside istream, but it's mandatory if you don't want your
                //input to get stuck or worse
                if (inName == name && inPassword == password)
                {
                    cout << "Login Successful\n" //the '\n' is a character, so that's why I can add it 
                    //and it will automatically output a newline in console, alongside the string
                        << "Welcome, "
                        << inName << "\n\n\n\n\n";

                    if (inName == name && inPassword == password) {

                        cout << "Your adventure begins...""\n\n\n\n\n";
                        cout << "You were a kid with no cares. Now you are a 21 year old who has to face a decision. Do you want to be an Employee(10).Hacker(20)""\n";
                        cin >> reply1;

                        
                        if (reply1 = employe) {      //problem here 
                            myCareer = "Employe";
                        }
                        if (reply1 = hacker) {
                            myCareer = "Hacker";
                        }
                        cout << myCareer;          // to here

                       
                        

                        

                       
                           
                            

                        break;
                    }


                }
                cout << "incorrect name or password\n"; //if you haven't entered the valid account,
                //then the while loop is not done yet. So that's why this output is without condition
            }
            //now do something about the account
        }
        cout << "\n\n\n\n\n"; //give it 5 newlines
    }
}[code][code][code]
Last edited on
Lines 137 and 140: You are using = (assignment) when you should be using == (equality test).
Last edited on
than kyou so much that was bothering me for so long! :)
Note that you could also be using else if chains to prevent redundant checks.

1
2
3
4
5
6
7
8
9
10
11
12
if (thing == "this")
{

}
else if (thing == "that")
{

}
else if (thing == "another thing")
{

}


Also, my_file_stream.close() will be called automatically when the fstream variable goes out of scope. No need to explicitly call it unless you're trying to re-open the file within the same scope.

Also, your if statement on line 130 is redundant.
Last edited on
Topic archived. No new replies allowed.