Need help!

Hello,

I am relatively new to the whole C++ thing. However, I have picked up some skills to make a simple text based game. Now I am planning on making an actual C++ game, containing chapters and parts, I'm not going to do random numbers and all, just text and user input. Basically something for me to practice my coding skills on and hopefully add on to it as I learn more things.

Now, why I am here is mainly due to some frustrating problems I have run into. I have tested strings and pointers and by the looks of it, I don't think it would be of any benefit to me. What I am trying to do is lets say:

void chapter1(){
//User enters information like, name, age, sex, etc.

}

void chapter2(){
//User doesn't have to enter information as it has already been stored above.

}

Basically storing user input and using it in another function later on. I am quite new to C++ and I am not used to using pointers just yet. However, if there is a way to create a string pointer and do exactly as I explained above, please reply a way to do it, I would like to see it and maybe practice writing the code and memorising how the syntax would be written out and all. Thanks! Also If you need more specific explanation, reply to me, I would be happy to do so.
Instead of this, where the function doesn't accept any parameters, and doesn't return any value
1
2
3
4
void chapter1(){
//User enters information like, name, age, sex, etc.

}
you could do something like
1
2
3
4
void chapter1(string& name, int& age, char & sex){
//User enters information like, name, age, sex, etc.

}
above, the function accepts parameters by reference. That means the function can modify the contents of those variables. Then you can pass those same variables as parameters to other functions as required.
See function tutorial:
http://www.cplusplus.com/doc/tutorial/functions/


Or, where you have a group of related variables, such as those giving details about the user, you could define a structure or class to contain them.
1
2
3
4
5
6
7
8
9
10
11
12
struct User {
    string name;
    int age;
    char sex;    
};

User chapter1() {
//User enters information like, name, age, sex, etc.
    User user;    // define an object
    // ... get the values for that object
    return user;   // return the object
}
Above, the function returns an object of type User which contains all of the details. Then you can just pass that single object as a parameter to other functions.
See data structures
http://www.cplusplus.com/doc/tutorial/structures/
Last edited on
ok i kind of understand but not fully. Are you able to give me an example of instead of using struct, use a class?
There are some examples of using a struct in the tutorial pages I linked to. Try those out to begin with, to gain understanding, then see where it leads you.

In C++ a class and a struct are very similar (and can be made interchangeable), though people tend to use a struct to contain data only, while a class may be used where member functions or constructors etc are required. But the dividing line can become blurred, the only rule is that members of a struct by default are public, while those in a class are private - but these settings can easily be changed.

My suggestion is to not try to move too far, too fast. Keep things simple until you gain familiarity with using a struct before considering what a fully-fledged class can offer. In the context of your original question, the struct should suffice - and if you need added capabilities as the project progresses, it isn't a big deal to modify the existing code.

http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/templates/
Ok, so, for the structure thing, are you able to help me give me an example of how it will look like? I know how structures work now but for my case, I need an example to guide me, thanks for the help so far!
(Edited)

Ok so I have came up with this, I think I just figured it out, this is my first time using a structure, tell me how I did:
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
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <exception>
#include <string>

using namespace std;

struct Test{
string name;
int age;


}user;

int main(){

cout << "How would you like to be addressed?\n> ";
cin >> user.name;

cout << "Ok " << user.name << ", mind telling me your age?\n> ";
cin >> user.age;

cout << "User: " << user.name << "\nAge: " << user.age << endl;


}


Ignore the includes, I'll be using them later on in my game. :) I feel so relieved, I have been stressed out because of this pesky problem, now I have it settled and I feel sooooo happy and excited to start on my game.
Last edited on
Well, we seem to be starting from a blank page. I don't think I can offer much more until you give more detail about what it is you'd like to do. You did say, "Now I am planning on making an actual C++ game, containing chapters and parts" - so what is the plan - do you have any sort of outline of what you want to do? If you're just intending to make it up as you go along, well, that's possible too, but if you'd like me to start, then I'd be the one who'd be making it up as I go along ... which may not be the direction that you're planning on going.

Here's a complete example which you can compile and run
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
#include <iostream>
#include <string>

struct Person {
    std::string name;
    int age;
    char sex;    
};

Person chapter1();
void chapter2(Person & user); 

int main() 
{
    Person u = chapter1();
    
    chapter2(u);
}

Person chapter1() 
{
    Person user;
    
    std::cout << "Name?\n";
    std::cin  >> user.name;
        
    return user; 
}

void chapter2(Person & user) 
{
    std::cout << "Hello " << user.name <<  '\n';
}

Last edited on
I noticed you didn't include namespace std, why is that?

That's because use of identifiers in the std namespace are qualified by std::
See lines 24,25,32.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

I dont understand some of the syntax

Lines 10 and 11 are function prototypes. Because main comes before the definition of these functions, the compiler needs to know the calling sequence of these functions. Note that the signature of lines 1-11 match the signature of lines 20 and 30 exactly.

Are you able to walk me through what each line or function of code does?

I'm not going to explain every line. I will explain some of the lines that may not be clear.

Line 15: Creates a local instance of Person called u. u is initialized to the Person instance returned by Chapter1().

Line 17: Calls Chapter2() passing the Person instance u.

Line 22: Declares a local Person instance called user.
Line 25: Inputs a name and stores it in the name member of user.
Line 27: Passes the Person instance named user back to the caller.

Line 32: Displays the "Hello" and the user's name from the Person instance that was passed as an argument.





Ok so I have written some code of how I am planning this out. So basically, I intend on using the chapters like a book.

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
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <exception>
#include <string>

using namespace std;

struct Data{
string name;
int age;
int input;


}user;

void characterCrea(), prologue(), chapter1(), chapter2();

void characterCrea(){
    cout << "Name: ";
    cin >> user.name;

    cout << "Age: ";
    cin >> user.age;

    cout << "Your name is " << user.name << " and your age is " << user.age << endl;
    cout << "===================================================\n"
    << "Type 1 for Yes, 2 for No\n> ";
    cin >> user.input;
        switch(user.input){
    case 1:
        system("cls"); // Yes, clears screen, wait 1 second and go to prologue.
        Sleep(1000);
        prologue();

    case 2: //No, clears screen and prints out character creation again.
        system("cls");
        characterCrea();

        }
}

void prologue(){
    cout << "test"; //print out to confirm program is working
    exit(0); // to prevent loop.(If there is any other way to prevent a loop, pls reply.
}

void chapter1(){
    // Big block of code to bring charcter through chapter 1.
}

void chapter2(){
    //transition into chapter 2. More like a book.
}

int main(){
    characterCrea();  //Calling functions.
    prologue();
    chapter1();
    chapter2();

}


Tell me what you think about this. (Constructive criticism, comments for improvement.)
I honestly think I'm good to go. But I feel that I could still improve, however, about 98% of me says, alright, we're good to go.

My story would be based of this:

" At the middle of the ocean stands a tower as tall as the sky and as deep as the sea itself. Surrounding it are rapids so strong no boat can get close. All that is known is that it is an unnatural structure, but nobody can remember who built it. Every year a piece of the tower falls off into the surrounding seas, making the oceans around the whole planet boil for seven days and seven nights. When the waters finally calm down, the rapids around the tower have spread a little farther out. Parents tell their children that unless they behave nicely, the tower rapids will swallow the world; it's an old tale that has been told for generations now. And every child remembers this tale while growing up; looking at that tower on the horizon, counting their age by its crumbling structure, and fearing that the story they remember so well - a story they may one day tell a child of their own - might just be true."



Also,

HAPPY NEW YEAR!!!!, I was watching my computer's date turn from 31/12/16 to 1/1/17. XD It was pretty lit.
Last edited on
Comments:
Using global variables is generally not good practice. As a rule of thumb try to constrain a variable to as narrow a scope as possible - that is use variables defined inside functions, or within individual blocks of a function, rather than make everything global.

Variable int input; is purely used to control the internal logic of part of the program and should definitely be local to the function where it is used. A case might be made for sharing some information from one chapter to the next, but think (plan) carefully what that information will be, and what purpose it will serve.

In particular, using a single global Data object tends to undermine the purpose of using a struct, because you won't be passing it from one function to another, or creating multiple instances.

Are you able to give me an example of what I should be doing? I'm not too sure about what you mean.
Last edited on
For the time being, I think you should just go ahead. It really isn't possible to give examples without a specific context, I don't think there's enough detail so far to say more.
Alright, thanks for the help! Ill come back if I need anymore help.
Line 13: input really doesn;t belong as a member of your Data structure. It should be a local variable.

Line 16: As Chervil said, avoid the use of globals. Declare user within main().
57
58
59
int main ()
{  Data user;
...

You'll need to pass user to each of your functions.

Line 34: I detest the use of Sleep(). 1) It's OS dependent. 2) Is it really necessary to artificially delay the user to give the impression the computer is "thinking"?

Line 35: You're calling prologue twice. Once here and again when you exit the function back to main(). Simply return.

Line 39: You're calling characterCrea recursively. Yes it works, but not the best practice. Using a loop is better.

Line 46: Why are you calling exit()? That will terminate the program. Simply return. See comment regarding line 35 as to why prologue() is getting called multiple times.







Topic archived. No new replies allowed.