I used getline to an array which it's size is determine by using pointer.. can't~ y?

This is a discussion on I used getline to an array which it's size is determine by using pointer.. can't~ y? within the C++ Programming forums, part of the General Programming Boards category; I am a newbie here. Trying to do my assignment.. Here is my code. Code: # include <iostream> # include ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    I used getline to an array which it's size is determine by using pointer.. can't~ y?

    I am a newbie here. Trying to do my assignment.. Here is my code.

    Code:
    # include <iostream>
    # include <ctime>
    # include <string>
    
    using namespace std;
    
    int main ()
    {
        
        int num_player;
        string name[100];
            
        cout << endl << "\tWelcome to game centre. I am Joe, your game instructor.\n"
            << "\t\tSnake and Ladder" << endl << endl
            << " Please enter the number of player [maximum 4 players]:" ;
        cin >> num_player;
        cout<< endl;
    
    
    
        cout << " Enter the " << num_player << " player's name: " << endl;
        for ( int j= 1; j <= num_player ; j++ )
        {
            cout << j << ". " ;
            getline(cin,name[j]);
        }
        
    
        cout << endl;
        system("PAUSE");
        return 0;
    }
    The output I get have a problem.
    System don't allow me to input for name[1];
    I don't understand why~
    Is it the getline problem?
    can someone fix it for me? T^T

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    You need to remove the newline left behind by inputting the number of players.
    Code:
        cin >> num_player;
        cin.ignore();  //!! add this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    oh i see~~
    thx~~

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    for ( int j= 1; j <= num_player ; j++ )
    Loop is wrong. It must go from 0 to num_player - 1 becuase C++ arrays are 0-indexed.
    Because you're using a pre-allocated array, you must be extra careful not to let num_player exceed 100.
    Or you could use a vector to dynamically allocate the space for the array:

    std::vector name(num_player);

    If you insist on using a fixed-array, then, get rid of C-arrays and use a C++ array:
    std::array<string, 100> name;
    Don't forget to include <array>.
    And don't forget to check that your number of players is in range!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Don't forget that using array templates requires C++11 compliance, which varies by compiler.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    You mean std::array? That has existed since TR1 (in the std::tr1 namespace). So yes, you will need a compiler that supports C++11 or TR1 to use it. Most compilers today ought to support it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I feel like you take it for granted though. My compiler is pretty new and still the only way I get C++11 is by using switches. Other people are possibly using something even older than I am.

    mingw32-gcc.exe (TDM-2 mingw32) 4.4.1

    I'm saying that you should mention C++11 that's all, because it's not the default.
    Last edited by whiteflags; 11-24-2012 at 03:32 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by whiteflags View Post
    I'm saying that you should mention C++11 that's all, because it's not the default.
    Yes, you are right. I do tend to forget and take it for granted
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letting the user determine size of array?
    By Frankie15 in forum C Programming
    Replies: 9
    Last Post: 10-09-2011, 08:58 PM
  2. Replies: 17
    Last Post: 10-31-2007, 08:43 AM
  3. Replies: 42
    Last Post: 12-19-2004, 07:59 AM
  4. Determine array size
    By eth0 in forum C++ Programming
    Replies: 16
    Last Post: 01-08-2004, 09:26 AM
  5. Determine the size of a character array...
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-10-2002, 09:22 AM

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