D
Devendra_Vidhale
Hello,
I and new to C++ and tried to do a simple program using getline
library. The code is as follows-
#include<iostream>
using std::endl;
using std::cout;
using std::cin;
#include<string>
using std::string;
using std::getline;
int main() {
int number;
string name;
cout<<"Please enter your roll number: ";
cin>>number;
cout<<endl<<"Please enter your name: \n";
getline(cin, name);
cout<<"\nYou are "<<number<<". "<<name;
return 0;
} //end main
The code above compiles with GCC 4.2.1 (prerelease) on linux. The
problem is that the prompt "Please enter your name: " does not work
during execution. The program jumps directly from first cout statement
to third one, and exits. The string value stays blank.
However, if I comment out the lines for variable number, and remove
references from everywhere, the program prompts for the string
properly.
How should I receive the string from user after taking the int ? Is
there some way to flush the cin stream, like cout<<endl does for cout
stream ? Or is there some thing else I am missing ?
Devendra
I and new to C++ and tried to do a simple program using getline
library. The code is as follows-
#include<iostream>
using std::endl;
using std::cout;
using std::cin;
#include<string>
using std::string;
using std::getline;
int main() {
int number;
string name;
cout<<"Please enter your roll number: ";
cin>>number;
cout<<endl<<"Please enter your name: \n";
getline(cin, name);
cout<<"\nYou are "<<number<<". "<<name;
return 0;
} //end main
The code above compiles with GCC 4.2.1 (prerelease) on linux. The
problem is that the prompt "Please enter your name: " does not work
during execution. The program jumps directly from first cout statement
to third one, and exits. The string value stays blank.
However, if I comment out the lines for variable number, and remove
references from everywhere, the program prompts for the string
properly.
How should I receive the string from user after taking the int ? Is
there some way to flush the cin stream, like cout<<endl does for cout
stream ? Or is there some thing else I am missing ?
Devendra