Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Array...

Status
Not open for further replies.

sumsumin

Programmer
Jun 13, 2007
7
0
0
CA
I'm very new to C++.

I have the following string (called 'names'): "Cameron;Keremy;Jyle;Tyson;".

How do I turn this into an Array?
names[0] would be Cameron, names[1] would be Keremy, etc.?




I really would appreciate some help with this please!
 
Show some code and we can give a better answer than just guessing.
 
Code:
// main3b.cpp
// This holds a number game
// Copyright (c) 1998
///////////////////////////

#include <iostream.h> // For input/output
#include <fstream.h> // For file input/output
#include <string.h>  // For strcpy
#include <time.h>  // For time
#include <stdlib.h>  // For toupper and tolower


// The main function of our number game
int main(void)
{

    using namespace std;
    int PlayersAmount;
    string Player("");
cout<<"Welcome to MultiPlayer Strategy by Cameron McKinnon. Enjoy!\n\n\nOkay, time to start the game! First, you're going to have to answer some \nquestions.\n\n\n***\nHow many people will be playing?\n***\n\nNumber of players: ";
cin>>PlayersAmount;
cout<<"\n";
int count = 0;

 while(count<PlayersAmount)
{
                         string Player; //note: the [] appears after the variable

//populate array:

                           string Currentplayer("");

                           int saycount=count+1;
                           cout<<"Player "<<saycount<<", please enter your desired username: \n\n";
                           cin>>Currentplayer;

                           Player[saycount] = Currentplayer;
                           count ++;
}






















cout<<"\n\n";
    system("PAUSE");
    return EXIT_SUCCESS; 
}
 
Try vector<string>
Then use the push_back() method of vector to add strings to it, size() to see how many strings are in it, and you can access each string just like an array (with the var[n] format).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top