c++ - 为什么我的部分代码被跳过而不让我输入?

标签 c++ string c-strings

当我为第一个问题输入大量信息时,为什么我的代码会跳过最后一个问题?我做错了什么?

const int SIZEC =31;
char phrase[SIZEC];
cout << " Provide a phrase, up to 30 characters with spaces. > " << endl;
cin.getline(phrase, SIZEC);
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << " The phrase is: " << phrase << endl;
cout << endl;


cout << " Using sring Class Obects " << endl;
cout << "--------------------------" << endl;
cout << endl;

string leter;
cout << " Provide a single character > " << endl;
cin >> leter;
cout << " The single character is: " << leter << endl;
cout << endl;

如果需要之前的代码,请告诉我,我会添加它。

最佳答案

使用std::string::resize作为解决方法。

string phrase;
getline(cin, phrase);
phrase.resize(30);    // phrase will be reduced to 30 chars

string letter;    // better to use char letter
cin >> letter;
letter.resize(1);

关于c++ - 为什么我的部分代码被跳过而不让我输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994681/

相关文章:

c++ - ftell 非常大文件的错误

c++ - 编译牡丹1.10.1 : undefined namespaces

ios - 在 swift 中检查空值(不是 nil 或 NSnull)总是返回 nil?

windows - 批处理 - 无法打印字符串开头的空格

java - 访问 DLL 时 JNI 崩溃

c++ - 我如何在一段时间或 for 循环中使用 .find() 并且每次都没有给出相同的发现值

json - 在redis中将结构存储为字符串

c++ - 用 CString 替换 LPCTSTR 是否安全?

c - C 中的字符串解析 - 带格式说明符的字符串

c++ - 了解 C++ 中的 C 字符串和字符串文字