Hi, I am using Visual Studio 2008 Express Edition, and I wrote my code as follow:
====================================code starts

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

int main() {
	string *p;
	int n;
	cout << "Please input the number of students"<<endl;
	cin>>n;
	p = new string [n];
	cout <<"Please input the name"<<endl;
	for (int i=0; i<n;i++)
	getline(cin,p[i]);
	for (int i=0; i<n;i++)
	cout << p[i]<<endl;
	return 0;
	
}
==

=====================================code ends


When execute the code, if I input n=4; I can just type 3 names in.
=============================Execution results
Please input the number of students
4
Please input the name
Cristofor
Ivar
Anna

Cristofor
Ivar
Anna
Press any key to continue...
==================================ends


How ever, if the code is
====================================code starts

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

int main() {
	string *p;
	int n=4;
	p = new string [n];
	cout <<"Please input the name"<<endl;
	for (int i=0; i<n;i++)
	getline(cin,p[i]);
	for (int i=0; i<n;i++)
	cout << p[i]<<endl;
	return 0;
	
}

=======================================code ends

Then I got the right results:
=============================Execution results
Please input the name
Cristofor
Ivar
Anna
Jenny

Cristofor
Ivar
Anna
Jenny
Press any key to continue...
==================================ends

Could anyone tell me why? Thanks a lot!

Edited 6 Years Ago by Nick Evan: Added code-tags

Right after your cin >> n line, put a cin.ignore() . When you are entering the number there's the newline that came after it when you pressed enter. It gets stuck in the stream. The getline (the first time through the loop) is simply taking this '\n' in and thinking that it reached the end of a line already. The next three inputs go in okay. The ignore will pick up the excess character and the stream will be empty in time for your getline.

Edited 6 Years Ago by jonsca: n/a

Hi, I am using Visual Studio 2008 Express Edition, and I wrote my code as follow:

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

int main() {
    string *p;
    int n;
    cout << "Please input the number of students"<<endl;
    cin>>n;
    p = new string [n];
    cout <<"Please input the name"<<endl;
    for (int i=0; i<n;i++)
    getline(cin,p[i]);
    for (int i=0; i<n;i++)
    cout << p[i]<<endl;
    return 0;

}

When execute the code, if I input n=4; I can just type 3 names in.
Execution results

Please input the number of students
4
Please input the name
Cristofor
Ivar
Anna

Cristofor
Ivar
Anna
Press any key to continue...

How ever, if the code is

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

int main() {
    string *p;
    int n=4;
    p = new string [n];
    cout <<"Please input the name"<<endl;
    for (int i=0; i<n;i++)
    getline(cin,p[i]);
    for (int i=0; i<n;i++)
    cout << p[i]<<endl;
    return 0;

}

Then I got the right results:

Please input the name
Cristofor
Ivar
Anna
Jenny

Cristofor
Ivar
Anna
Jenny
Press any key to continue...

Could anyone tell me why? Thanks a lot!

When you enter using the cin >> n you type a number followed by a newline character. The extry using cin only read the number and leaves the return char in the input stream. The subsequent getline consumes this newline char the first time through the loop consequently only giving three more iterations through the loop, i.e. entering three more names. When you hardcode the value 4 into you program the newline char is not in the input stream to be read so you loop works the desired amount of times.

You need to detemine how to handle this extra return correctly before you can read the names. See previous post for how to do this.:)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.