populating an array

im trying to get this code to work

what i need to do is to ask the user to input a persons first name and last name and store it into stringName[i];

so stringName[i]; should contain one name(first and last)
Assume that the names will be separated with a space. so first1 last1 first 2 last2

i can get it to work with getline(cin, stringName[i]);
but i need it to be like: fisrt1 last1 = stringNames[0], first2 last2 = stringNames[1]


here is the original question:
Given an array declared as

string studentNames[5];

write the C++ code to populate the array from the keyboard with the first and
last name of students. Assume that the names will be separated with a space. Include prompts for input in your code. Mention any additional header files that you need besides string for your code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

using namespace std;



int main()
{
	string studentNames[5];
	
	cout << "enter 5 student name: "; 
	for (int i = 0; i < 5; i++)
	{
		cin >> studentNames[i];
	}

	for (int i =0; i < 5; i++)
	{
		cout << studentNames[i] << " ";
	}
	 cout << endl;
	 system("pause");
	 return 0;
}
how does getline not do that for you?
Use getline function instead of cin, so you can do it first last, otherwise if you use cin, even if you type first last, it would only take first.
So: getline(cin,studentName[i]);

Aceix.
Last edited on
well getline does work fine

but i was wondering if there is a way to do it while having it on all one line

getline asks for first input but then i have to hit enter to input the next one
he said he used getline already and it worked but then he needs it to look like it is which for some reason it is not I'm a bit confused with
ninthred wrote:
i can get it to work with getline(cin, stringName[i]);
but i need it to be like: fisrt1 last1 = stringNames[0], first2 last2 = stringNames[1]

works fine for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main( int argc , char** argv )
{
    std::string names[ 5 ] = { std::string() };
    for( unsigned short i = 0; i < 5; i++ )
    {
        std::getline( std::cin , names[ i ] );
    }
    for( unsigned short i = 0; i < 5; i++ )
    {
        std::cout << names[ i ] << std::endl;
    }
}
Gib Lit
Lit Gib
Til Big
Ilt Igb
Tli Bgi
Gib Lit
Lit Gib
Til Big
Ilt Igb
Tli Bgi
That would be very complicated..You would have to make it so after ever second space ( hoping the user doesn't accidently hit it twice ) that it puts the next two words into a different position in the array. You are best off with using getline. Otherwise use getline and get the whole line then seperate the string into multiple strings and put those into an array.
Last edited on
yea i was thinking it would be a lot easier just to use getline and the question doesn't really specify how it it wants it done.

ill just go with getline

Thanks everyone for helping out.
OP wrote:
but i was wondering if there is a way to do it while having it on all one line

getline asks for first input but then i have to hit enter to input the next one

Then you might want to specify a unique delimiter for the getline function. eg: a semi-colon.
1
2
3
getline(cin,studentNames,';');
//so you enter names like this: Joel Smart;Julian Smart;...
//use a semicolon after every entry 


For more info: http://www.cplusplus.com/reference/string/basic_string/getline/

Aceix.
Topic archived. No new replies allowed.