Thursday, July 30, 2009

In C++, how can I use the "cin >> " or "cin.get()" commands to create an array of a specific size?

I'm working on a game in C++. In the game, the user can enter their name and whatnot. I want to assign the name to an array or visa versa. What is in the array is not important at the moment. For what it's worth, the array will hold the stats from the game. So i want it to look something like





int NameOfPlayer;


cin %26gt;%26gt; NameOfPlayer;


NameOfPlayer[4] = {0,0,0,0} ;





So the next time the program is run with a different user, the different user can enter his or her DIFFERENT name. And the array be named for that persons name. The reason i want to do this is because i'm going to write the arrays out on a text file to save them for later use. I know the above code isn't right, but can anyone help me figure out how?

In C++, how can I use the "cin %26gt;%26gt; " or "cin.get()" commands to create an array of a specific size?
If your going to do what you said, in that the array is to hold the name, and different data such as scores, etc, you might want to consider a struct.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


#include %26lt;cassert%26gt;


using namespace std;





//to create a struct:


typedef struct { //creates a struct of parallel arrays


string NameOfPlayer[4];


int Score[4];


int Health[4];


} StructName;





int main() {


StructName caller;





//the following for loop is to enter data into one of the arrays


for(int i=0; i%26lt;=4; i++) {


cout %26lt;%26lt; "Enter name for player number: " %26lt;%26lt; 1 %26lt;%26lt; ": ";


cin %26gt;%26gt; caller.NameOfPlayer[i];


}





ofstream outfile; //creates variable of type ofstream


outfile.open("datastorage.dat"); //opens the file and assigns


assert(!outfile.fail()); //makes sure that outfile has not failed





//outputs data to the outfile.


for(int i=0; i%26lt;=4; i++){


outfile %26lt;%26lt; caller.NameOfPlayer[i] %26lt;%26lt; ";";


}


}











Those little snippets should help you gain a bit of ground on the whole concept. I too am in the process of writing a game, and I found it easier to use structs and vectors rather than arrays to make the game. Should you have any SERIOUS questions about the game making process or C++, you can email me at juan.sanchez@manlawonline.org





good luck.
Reply:well all i can think of( although im not that good with c++)


is to use a lot of cin%26gt;%26gt; 's to get the information, assig it to a variable, then organize the variables into an array
Reply:If it is being stored to a file and read from a file, use the ifstream and ofstream features of C++. Your array will need two parameters, one for the score and one for the name.
Reply:I agree with broken. The problem with using an array is that when you save it to a text file, it will save it on a single line. This means everything has to be formatted or your data will get mixed together when the program re-reads the data file to get the players info. I suggest using structs. You can still use arrays in the struct for the name, but it allows you to save that single struct into a data file instead of multiple arrays and whatnot. I've actually been working on an RPG of my own and here's basically how I set mine up:





struct character


{


char name[20];


int level = 1;


int Items[100], QuestFlags[100], MapFlags[100];


{





Basically, you can create a datatype of character and save THAT to a file. This means it saves the name, level, and the other arrays that hold my information.





So just do something like





character player1;





then you can save the data by simply doing a normal assignment.





player1.name = "bob"; // or by means of an input from the keyboard





just save the player1 to an outfile and then you have everything!

statice

No comments:

Post a Comment