| | |
why i have to press "enter" twice before getline can read the string...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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;
} Works for me..you just might want to put
so that the name appears on its own line..makes it easier to read.
C++ Syntax (Toggle Plain Text)
cout << name[i]; cout << endl;
so that the name appears on its own line..makes it easier to read.
big146
![]() |
Similar Threads
- Capturing "Enter" key event in C# windows application (C#)
- Scanner - How to read "Enter" input (Java)
- c++ read string from text file (C++)
- Trojan Horse - HELP PLEASE (Viruses, Spyware and other Nasties)
- reading a char from keyboard without pressing "Enter"??? (C)
- vc++ mfc-i can't make getline work with a string for file input (C++)
Other Threads in the C++ Forum
- Previous Thread: Bitwise AND
- Next Thread: array of objects memory allocation
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






