C++ word arrays

Discussion in 'Programmer's Corner' started by VVS, Jul 30, 2007.

  1. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    Does anybody know how to create "word" arrays?
    I am programming a cricket scorer and would like to create an array for all the player names. I don't know how to define the array: char, string, etc???
    Please help me
     
  2. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    string myString[10];
     
  3. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    gr8t! thanq u very much!
    I have one more question: How can change the font color of output and input lines.
    I don't mind if I have to type a small code before each line.
     
  4. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    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";
     
  5. MWalden

    Member

    Apr 26, 2007
    22
    0

    The number in between the brackets is the number of characters you want to set memory aside for. In other words the number needs to be as long or longer than the longest input you're going to save in that variable. Like if i declare a string:

    char TeamAP1[100];
    char TeamAP2[100];
    char TeamBP1[100];
    char TeamBP2[100];


    that allocates enough memory to store 100 characters for four different variables each. So declare 4 strings instead of 2. Also I think you gotta use a pointer when storing into an array so I would use:
    cin.getline(TeamAP1, 100, '/n'); , etc... instead of "cin>>".. The name of the string is actually a pointer to the first character in the string, or something like that.. (http://www.cprogramming.com for more info). But I'm no expert.

    Also to display that string leave the brackets off it will display the whole string. So your code should look like this..

    #include <cstdlib>
    #include <iostream>

    using namespace std;
    char teamap1[100];
    char teamap2[100];
    char teambp1[100];
    char teambp2[100];
    int main(int argc, char *argv[])
    {
    cout<<"type name of player 1\n";
    cin.getline(teamap1, 100, '\n');
    cout<<"type name of player 2\n";
    cin.getline(teamap2, 100, '\n');
    cout<<"type name of player 3\n";
    cin.getline(teambp1, 100, '\n');
    cout<<"type name of player 4\n";
    cin.getline(teambp2, 100, '\n');

    cout<< teamap1 << " and " << teamap2 << " vs " << teambp1 << " and " << teambp2 << "\n";

    system("PAUSE");
    return EXIT_SUCCESS;
    }


    Try that.
     
  6. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    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];?
     
  7. MWalden

    Member

    Apr 26, 2007
    22
    0
    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.

    Too keep from writing repetitive code for every player use a structure.. Like this:

    struct team {
    char name[100];
    }
    ^^^ Define this outside of the main function.
    Then inside of the main function declare:

    main(){
    team A[10]; //Number of players goes inside of brackets
    team B[10]; // Number of players goes inside of brackets
    team C // etc..

    //To store and access the info do this:
    cout<< "Enter name of player 1 for Team A: " <<endl; // 1 for player 1
    cin >> A[1].name; // 1 for player 1 2 for player 2 and so on.
    cout<< "Player 1's name is " << A[1].name;

    }

    That should work.
     
  8. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    Thank you very much it's worked perfectly well! :)
     
  9. Pootworm

    Member

    May 18, 2007
    29
    0
    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].

    The zero-based counting is counter-intuitive until you know what's going on behind the scenes. The identifier "teama" is a pointer to a string. teama[n] is equivalent to *(teama+sizeof(string)*n). So teama[0] is equal to teama, and points to the same string-sized chunk of memory allocated for your first string. teama[1] is the string-sized chunk of memory after the string-sized chunk of memory at teama[0], which is your second string.
     
  10. VVS

    Thread Starter Active Member

    Jul 22, 2007
    66
    0
    okay pootworm! thanq for ur advice!
     
Loading...