0

Hi all,

I am trying to learn the basic concept about array, however when I tried to change the type into string, it shows error .. I am using IDE visual studio 2005

#include <iostream>
#include <string>
using namespace std;

int main()
{
	
	string stName[20];
	cout << "what is your name? " << endl;
	cin >> stName;
	cout << stName << endl;
}

Thanks for the assistance..

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by reza.adinata
Featured Replies
  • 1
    jonsca 1,059   6 Years Ago

    In order to place the string into the array of strings you must specify an index. E.g., [code] cin >> stName[0]; //to write into the first string in the array cout<<stName[0]<<endl; [/code] Read More

  • [QUOTE=reza.adinata;1164404]But the question is that, how to show output of all inputs within the array? not just each element.. Example, I want to type " MY NAME" and i will get the result of " MY NAME ". I tried to use char, and it ends up showing only "MY" … Read More

1

In order to place the string into the array of strings you must specify an index. E.g.,

cin >> stName[0]; //to write into the first string in the array
cout<<stName[0]<<endl;
0

In order to place the string into the array of strings you must specify an index. E.g.,

cin >> stName[0]; //to write into the first string in the array
cout<<stName[0]<<endl;

But the question is that, how to show output of all inputs within the array? not just each element.. Example, I want to type " MY NAME" and i will get the result of " MY NAME ". I tried to use char, and it ends up showing only "MY"

thank you..

1

But the question is that, how to show output of all inputs within the array? not just each element.. Example, I want to type " MY NAME" and i will get the result of " MY NAME ". I tried to use char, and it ends up showing only "MY"

thank you..

The >> operator treats a space as a delimiter, so it'll stop after the first name. Use getline to get a whole line(first and last name.

See the example on this link. It does exactly what you are trying to do.

http://www.cplusplus.com/reference/string/getline/

As far as I can tell, you only need one string, not 20. You're only storing one name.

Edited by VernonDozier: n/a

0

The >> operator treats a space as a delimiter, so it'll stop after the first name. Use getline to get a whole line(first and last name.

See the example on this link. It does exactly what you are trying to do.

http://www.cplusplus.com/reference/string/getline/

As far as I can tell, you only need one string, not 20. You're only storing one name.

I see.. yes, it is all clear. Thank you

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.