strings and array

This is a discussion on strings and array within the C++ Programming forums, part of the General Programming Boards category; I'm sure that I'm going to sound dumb on that one but I can't figure out why this simple code ...

  1. #1
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    strings and array

    I'm sure that I'm going to sound dumb on that one but I can't figure out why this simple code isn't working correctly. I just ask the name of the players and put them in an array. The problem is when I print out the name, I'm missing the first character except for the first player...is there a way to fix that? thanks

    here is the part of the code posing problem:

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main()
    {
    int player;
    cout<<"number of players: ";
    cin>>player;
    
    char name[player][30]
    
    for (x=1; x<=player;x++)
    {
    cout<<"player "<<x<<" enter your name please\n";
    cin.ignore();
    cin.getline(name[x-1],30);
    }
    
    //this part is just to test
    
    for (int i=0; i<player;i++)
    {
    cout<<name[i]<<"\n";
    } 
    }
    oh and by the way can you tell me why I need to use cin.ignore() ?

    thanks a lot
    bart

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    That code shouldn't even compile. Besides the missing semicolon, C++ doesn't allow variable length arrays in that way, I believe.

    However, I think I can answer both your questions at the same time. You don't need the cin.ignore() there, and that is why you are missing the first character of the other players. You are ignoring the first character for each.

    You need to put the cin.ignore() before the loop, because you only want to use it once. It is used to ignore the newline (from when you hit enter) after you type in the number of players. Using cin >> players leaves that newline in the input stream, so that when you use getline it reads that in as an entire line. By using the ignore() once after you use cin >>, you get rid of that extra newline so that the calls to getline will work properly.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int player;
    cout<<"number of players: ";
    cin>>player;

    char name[player][30]

    This sequence is undefined/illegal as you can't declare an array using static memory with a non constant variable like player. If you want user to be able to control size of the array called name, then you will need to use dynamic memory to declare space for name.

    getline() uses three parameters. The third defaults to newline char. That means that input into the C string used as the first parameter for getline() will halt when the first new line char is found, or if the number of char alloted by the second parameter of getline() has been read in before the newline char is found.

    >> doesn't remove the newline char put into the input buffer when user hit enter to enter the value for player. Therefore, without ignore() to ignore the newline char, there would be nothing put into the first call to getline() inside the loop.

  4. #4
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    thanks

    thanks a lot..it's working perfectly now... and I understood the use of cin.ignore()

    bart

    PS: it was compiling correcty before (beside the semi colon that I forgot to type when I posted them thread ) just the first letter of the players but the first one were missing

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Out of curiosity, what compiler are you using, bart?

  6. #6
    bartinla
    Join Date
    Oct 2004
    Posts
    10
    I use Dev-C++...isn't it working on your compiler? I used that kind of 2d array in all my code and the game is working perfectly... can u let me know if it's working on your compiler or not? so that at least I will know if what I'm doing is wrong

    thanks bart

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It fails on MSVC++ 6.0 for me, but works on Dev-C++. Technically, I believe the C++ standard forbids declaring arrays like you did, but the latest C standard allows it. It is possible that Dev-C++ allows it because of that. I wouldn't recommend using it though, because for now at least, it is non-standard in C++. You might want to consider learning an alternative that compiles on all C++ compilers.

    Using vectors and strings (my recommended solution):
    Code:
    #include<iostream>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        int player;
        cout<<"number of players: ";
        cin>>player;
        cin.ignore();
    
        vector<string> name(player);
    
        for (int x=1; x<=player;x++)
        {
            cout<<"player "<<x<<" enter your name please\n";
            getline(cin, name[x-1]);
        }
    
    //this part is just to test
    
        for (int i=0; i<player;i++)
        {
            cout<<name[i]<<"\n";
        }
        cin.get();
    }
    Or using new and delete:
    Code:
    // ??? I'm tired and going to bed, maybe somebody else can give
    // you an example using C style strings and new and delete. ;)

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'm pretty sure this will compile, though I don't have compiler at work to test it.
    Code:
    //declare an array of C strings using dynamic memory
    const int MAX = 30;
    int players;
    cout << "enter number of players" << endl;
    cin >> players;
    
    char ** name;
    int i;
    
    name = new char*[players];
    for(i = 0; i < players; ++i)
      name[i] = new char[MAX];
    
    //do something with name here
    
    //delete memory for name now that done with it.
    for(i = 0; i < players; ++i)
      delete[] name[i];
    
    delete[] name;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 11:15 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21