getline的问题
本人菜鸟,发现程序运行并未按照编写代码的意图,即:输入quotation,回车一次,窗口提示输入speaker,然后键入speaker,回车,窗口显示输入的全部内容。 程序执行时,输入quotation之后,要回车两次才出现下一行提示;且程序直接执行到最后。。。。。。。
我那行代码cin.get();没有作用了 反复改动过发现是getline的问题 请各位大神明示~~
#include <iostream>
#include <string>
using namespace std;
int main(){
//declaring the necessary variables
string quote, speaker;
//prompt user for quotation
cout << " please enter the quotation:\n";
getline(cin, quote);
//promt user for the quote's author
cout << "enter the person to whom this quote is atrributed:\n";
getline(cin, speaker);
cout << "\n\n";
cout << "press enter or return to continue.\n";
cin.get();
return 0;
}
我那行代码cin.get();没有作用了 反复改动过发现是getline的问题 请各位大神明示~~
#include <iostream>
#include <string>
using namespace std;
int main(){
//declaring the necessary variables
string quote, speaker;
//prompt user for quotation
cout << " please enter the quotation:\n";
getline(cin, quote);
//promt user for the quote's author
cout << "enter the person to whom this quote is atrributed:\n";
getline(cin, speaker);
cout << "\n\n";
cout << "press enter or return to continue.\n";
cin.get();
return 0;
}
...全文
相关推荐
getline 模板函数读取遇到分隔符后的多余的字符.pdf
关于getline模板函数读取遇到分隔符后的多余字符的问题,首先需要了解getline函数的用法以及它在读取输入时的行为。
C++中getline()的用法详解
根据你的程序需求选择合适的版本,并注意避免可能出现的问题,如缓冲区溢出和输入流状态管理。
cin.get()、cin.getline()、getline()、gets()、getchar()
- 使用cin.getline()时,要确认输入不超过缓冲区大小,且不用处理换行符问题。- 使用getline()时,可以接收包括空格在内的整个字符串,同时要注意设置合理的结束符。
使用ifstream和getline读取文件内容[c++]
如果没有成功,它将打印错误信息并退出程序,这有助于在运行时发现和报告问题。在`main`函数中,我们依次调用了上述四个函数,演示了不同的文件读取策略。
浅谈C++中字符串输入get与getline的区别
为了避免这个问题,可以使用cin.get()函数,读取下一个字符(即使是换行符),将换行符舍弃。


