This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to getline when you have used cin >> char?


Hi,

There is a strange behavior about g++ compiler under
unix system. When I first cin >> char and then
cin.getline(char*, int), it don't wait for me to input
a line, but directly execute the next line. How to
solve this problem? Please help me!!

Regards!
Weihui


leo:~/adv_str/hw1 > cat try.cxx
#include <iostream>

using namespace std;

int main () {
    char c;
    cout << "Input a char!!";
    cin >> c;
    cout << c << endl;
    char* line = new char[1000];
    cout << "Please input sentence: " << endl;
    cin.getline(line, 1000);
    cout << line << endl;
    cout << "Input a char!!";
    cin >> c;
    cout << c << endl;
    return 0;
}



leo:~/adv_str/hw1 >
leo:~/adv_str/hw1 > g++ try.cxx
leo:~/adv_str/hw1 > a.out
Input a char!!c
c
Please input sentence:
                       //Here don't wait for input!!! 
Input a char!!a
a
leo:~/adv_str/hw1 >

__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]