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!

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.

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.:)

Thank you all! It works now:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.