在C++中,当调用getline()函数时,在vc++6.0下运行时,需转行两次。且在某些情况下,会发生错误。 例如: //C++ #include <iostream>#include <fstream>#include <string> using namespace std; int main(){ ofstream outFile; string state = ""; string capital = ""; outFile.open("date.txt"); if(!outFile.is_open()) { outFile.open("date.txt"); } cout << "The state: "; getline(cin, state); while(state != "x" && state != "X") { cout << "The capital: "; getline(cin, capital); outFile << state << '#' << capital << endl; cout << "The state: "; getline(cin, state); } return 0;} 当键入:Oregon, Salem, New Jersey, Trenton, x 结果为:Oregon# Salem#New Jersey Trenton#x 结果显然错误。 对于这种错误,说法不同,但可能是vc++6.0的bug. 因为在vs2005中运行,一切OK.

评论