• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1551
  • Last Modified:

string array getline


I'm puzzled by a statement(s) regard getline and after perusing the standard I'm in need of clarification.  Consider.

int main()
{
  string myName[20];
  cout << "Please enter your name: ";
  getline(cin, myName);
  cout << "Hello " << myName << endl;
}

The program has an obvious errror so the programmer was told:

"getline doesn't accept a string array for its second parameter. Just get rid of the [20] from myName."

Now I question the validity of said statement in part because 'getline' can and DOES accept a string array as its second parameter.  So now:

int main()
{
  std::string Array[2];

  std::cout << "Enter some words...\n";
  std::getline(std::cin, Array[0]);

  std::cout << "Enter something else...\n";
  std::getline(std::cin, Array[1]);

  std::cout << "You entered:\n";
  for(int i(0); i< 2; ++i)
      std::cout << Array[i] << std::endl;

    return 0;
}
0
forums_mp
Asked:
forums_mp
1 Solution
 
mrwad99Commented:
>> string myName[20];

That is an array of std::strings.

The string array you need to use is an array of characters,

i.e

char myName[20];

HTH
0
 
mrwad99Commented:
>> std::getline(std::cin, Array[0]);

That works because Array[0] is a single std::string, which is the same as saying

string myName;
getline(cin, myName);

0
 
Hamed ZaghaghiProgrammerCommented:
hi,
if you want to get array of string you must get them like this:

string myname[20];
int i;
for(i=0; i<20; i++)
    getline(cin, mayname[i]);

and if you want to declare one string that means array od characters not array of strings, so you must declare:

char myname[20];
getline(cin, myname);

have good programming day;
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
forums_mpAuthor Commented:

So you're saying

string myStr;
and
string myOtherStr[20];

are the same?

Furthermore, you havent answered my question which is.  Is this

"getline doesn't accept a string array for its second parameter. Just get rid of the [20] from myName."

a true statement?
0
 
mrwad99Commented:
? I am not saying that at all.

string myStr;

This represents *one* std::string

string myOtherStr[20];

This represents 20 std::strings, in an array called myOtherStr.  Each of these strings is accessible via myOtherStr[i] where 0<= i <= 19.  (i is between 0 and 19)

>> "getline doesn't accept a string array for its second parameter. Just get rid of the [20] from myName."

Yes that is correct.  It accepts either a char[] array or a std::string.

When you pass, say, myOtherStr[19] you pass the 20th element of the array myOtherStr, which is one single std::string.
0
 
allmerCommented:
int main()
{
  string myName[20];
  cout << "Please enter your name: ";
  getline(cin, myName);
  cout << "Hello " << myName << endl;
}
This definetly won't compile. As suggested by yourself it works using the myName[x].
Look at what you did there more closely:
myName[x] doesn't represent a string-array anymore it is a mere std::string as far as getline, or any other function, is concerned.
For you as the programmer it is of course part of an array of std::string.

Maybe this is what the confusion is all about.
So using the [] operator on an array represents one of the basic element that the array contains in your case a std::string.
Hope this clarifys.
Jens
0
 
Hamed ZaghaghiProgrammerCommented:
hi,

getline function does not accept a string array in the second parameter, so you can nt use it with aray of strings.

good luck;

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now