but there's a problem:
just for trying it out I entered this code:
#include <cstdlib>
#include <iostream>
using namespace std;
string teama[2];
string teamb[2];
int main(int argc, char *argv[])
{
cout<<"type name of player 1\n";
cin>>teama[1];
cout<<"type name of player 2\n";
cin>>teama[2];
cout<<"type name of player 3\n";
cin>>teamb[1];
cout<<"type name of player 4\n";
cin>>teama[4];
cout<<""<<teama[1]<<" and "<<teama[2]<<" vs "<<teamb[1]<<" and "<<teamb[2]<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
The program crashes as soon as it is about to execute this line:
cout<<""<<teama[1]<<" and "<<teama[2]<<" vs "<<teamb[1]<<" and "<<teamb[2]<<"\n";
the brackets means it's an array. 100 means that it has set back 100 places for letters/numbers whatever and the one means you're only going to store an entry into the value of 1's position. So that way you're only going to be able to store one letter (the first) of the string you enter.kewl! thanq very much MWalden. But I actually don't want to use much space by using char for 22 players - wouldn't that use up a lot of space?
I fiddled around with the code and made this:
#include <cstdlib>
#include <iostream>
using namespace std;
string teama[100];
string teamb[100];
int main(int argc, char *argv[])
{
cout<<"type name of player 1\n";
cin>>teama[1];
cout<<"type name of player 2\n";
cin>>teama[2];
cout<<"type name of player 3\n";
cin>>teamb[1];
cout<<"type name of player 4\n";
cin>>teamb[2];
cout<<""<<teama[1]<<" and "<<teama[2]<<" vs "<<teamb[1]<<" and "<<teamb[2]<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
what does the 100 in string teama[100] stand for and the 1 in cin>>teama[1];?
Actually, the only problem with your original code is with the array indexing (and whatever's going on with player 4). C arrays start counting at 0. If you allocate space for 2 elements with "string teama[2]", you work with the strings teama[0] and teama[1].but there's a problem:
just for trying it out I entered this code:
#include <cstdlib>
#include <iostream>
using namespace std;
string teama[2];
string teamb[2];
int main(int argc, char *argv[])
{
cout<<"type name of player 1\n";
cin>>teama[1];
cout<<"type name of player 2\n";
cin>>teama[2];
cout<<"type name of player 3\n";
cin>>teamb[1];
cout<<"type name of player 4\n";
cin>>teama[4];
cout<<""<<teama[1]<<" and "<<teama[2]<<" vs "<<teamb[1]<<" and "<<teamb[2]<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
The program crashes as soon as it is about to execute this line:
cout<<""<<teama[1]<<" and "<<teama[2]<<" vs "<<teamb[1]<<" and "<<teamb[2]<<"\n";
by Aaron Carman
by Jake Hertz