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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 17
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

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

 
0
  #1
Sep 14th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

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

 
0
  #2
Sep 14th, 2004
Works for me..you just might want to put

  1. cout << name[i];
  2. cout << endl;

so that the name appears on its own line..makes it easier to read.
big146
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #3
Sep 14th, 2004
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC