-
Storing Users Input
Hello, I am quite new to C++ and I was wondering how to store a users input. I am thinking that I would use a string. I don't think I would use a string or a structure, unless their is a away to store input from a user into a structure. I am making a text based game right now, and I was wondering how to store users data. Ex. If I ask the user "how many cavalry units do you want?", I want to know how to store that data. THANKS.
-
Edit: I meant to say that I don't think that I would use a structure or an array. But I'm sure I would use a string. Help. :confused:
-
Umm..
Code:
string cavalryUnits;
cout << "how many cavalry units do you want? ";
cin >> cavalryUnits;
The entered value or whatever (value only should use an int, string if its text or something) is stored in cavalryUnits if you need to call it later.
-
A structure is a good idea.
With a structure, you can keep all of your data organized. This is especially true if...
- Some of the variables in the structure can be integers, some floats, and some strings.
- There are multiple users, and you need to store the same set of data for each user. With a structure, you can allow for any number of users, without knowing (when you write the program) how many users there will be.
You will generally get one variable from the user at a time, and fill-in the structure as you go.
Later when you learn about classes and objects you can "upgrade" the structure to a class (which is something like a smart structure).