943,510 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4203
  • C++ RSS
Sep 14th, 2004
0

why i have to press "enter" twice before getline can read the string...

Expand Post »
why i have to press "enter" twice before getline can read the string... can anybody help me to solve this problem...

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

int main() {
	
	string *name, hold;
	int num, i, j;
	
	cout << "----------------------------------------------------\n"
		 << "This program creates a dynamic student record system\n"
		 << "----------------------------------------------------\n";

	cout << "\nPlease enter the numbers of student: ";
	cin >> num;

	name = new string [num];

	cout << "\n";
	
	for ( i = 0; i < num; i++ ) {
		
		cout << "Please enter the name of student [" << i + 1 << "]: ";
		fflush(stdin);
		getline( cin, name[i] ); 
		
		cout << name[i];

		for ( j = 0; j <= i; j++ )
			if ( name[i] < name[j] ) {
				hold = name[i];
				name[i] = name[j];
				name[j] = hold;
			}

	}

	cout << "\nList of student <sorted>:\n";

	for ( i = 0; i < num; i++ )
		cout << name[i] << endl;

	cout << "\nList of student name that contains the string Mo:\n";
	
	for ( i = 0; i < num; i++ )
		if ( name[i].find( "Mo" ) == 0 )
			cout << name[i] << endl;

	cout << "\n";

	delete [] name;
	
	return 0;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lara_ is offline Offline
17 posts
since Jul 2004
Sep 14th, 2004
0

Re: why i have to press "enter" twice before getline can read the string...

Works for me..you just might want to put

C++ Syntax (Toggle Plain Text)
  1. cout << name[i];
  2. cout << endl;

so that the name appears on its own line..makes it easier to read.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 14th, 2004
0

Re: why i have to press "enter" twice before getline can read the string...

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bitwise AND
Next Thread in C++ Forum Timeline: array of objects memory allocation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC