Your problem likely lies here:
1 2
|
cin >> dynamic[i-1].name;
getline(cin, dynamic[i-1].name);
|
It's generally not advised to mix
getline
with
cin
cin
will stop reading at whitespace, so if your line is separated by spaces
cin
will only capture the input up to the first whitespace character, but this will leave the remainder of the input in the buffer, and your
getline
call on the next line will retrieve the remainder of that buffer up to the newline character, which is the second part of the name, and is assigning what is retrieved and overwriting what the line before it did.
Using just
getline
on the other hand will read up until the end of the line, which will include some whitespace characters (by default it will stop at the newline character, but you can set your own delimiter as well, see:
http://www.cplusplus.com/reference/string/string/getline/)
dynamic.name
. That includes both the first name and the last name, I am assuming? If so, just use
getline
and it will capture the entire line up to the newline character.
As far as being prompted twice, you can initialize
SIZE
to an invalid value (like -1 or something like that) and begin the loop there:
1 2 3 4 5 6 7
|
int SIZE = -1;
while (SIZE <= 0)
{
//prompt in here
//keep spinning until they give you valid input
//but if they enter valid input the first time then it won't loop again (condition will fail at beg of loop)
}
|
But that's just one way to do it. If that doesn't really make sense or seems odd to you, you can always prompt once, then enter a loop if the entered input was incorrect and loop in there until they provide valid input. Both should be fine as far as functionality is concerned, just as long as you're careful to not call
new
with an invalid parameter.