正文

c++中的getline()2009-03-29 14:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sun2009/41839.html

分享到:

在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.

阅读(2011) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册