程序流程中的 C++ 未知 error/bug
C++ unknown error/bug in program flow
我刚刚复习 C++,我不知道为什么 3 和 5 是唯一可行的选项。我已经将它转换为 if-else 语句,但仍然是同样的问题。下面是代码:
#include <iostream>
using namespace std;
int main()
{
char c, s[50] = {'[=11=]'};
int num;
cout << "Select cin method:" << endl;
cout << "1. cin.get(c);" << endl;
cout << "2. cin.get(s, 10);" << endl;
cout << "3. cin.get(s, 10, '*');" << endl;
cout << "4. cin.getline(s, 10);" << endl;
cout << "5. cin.read(s, 10);" << endl;
cout << "Select: " << flush;
cin >> num;
switch (num) {
case 1:
cin.get(c); // cin >> c;
break;
case 2:
cin.get(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 3:
cin.get(s, 10, '*'); // cin >> s; max length 10, '*' as string terminator
break;
case 4:
cin.getline(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 5:
cin.read(s, 10); // cin >> s; max length 10, records '\n'
break;
default:
break;
}
if (num == 1)
cout.put(c); // cout << s;
if (num >= 2 && num <= 5)
cout.write(s, 15); // cout << s; max length 15
}
每当我为 num 输入 1/2/4 时,它都会绕过 switch 和 else-if 语句。我已经尝试检查 "cout << num" 得到了什么 num 并且它得到的值是正确的。我也没有收到任何错误消息。以下是我得到的示例:
Select cin method:
1. cin.get(c);
2. cin.get(s, 10);
3. cin.get(s, 10, '*');
4. cin.getline(s, 10);
5. cin.read(s, 10);
Select: 1
--------------------------------
Process exited after 1.676 seconds with return value 0 Press any key to continue
它不会绕过 switch
,它会正确执行。问题是,在读取数字后,输入流中有一个额外的字符(您在数字后按的 \n
),这会导致您在情况 1、2 和 4 中看到的行为。在这些情况下,您正在执行的下一次读取操作会发现 \n
并停止读取。
要修复它,您可以在 cin >> num
:
之后添加类似的内容
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
(并包括 <limits>
我刚刚复习 C++,我不知道为什么 3 和 5 是唯一可行的选项。我已经将它转换为 if-else 语句,但仍然是同样的问题。下面是代码:
#include <iostream>
using namespace std;
int main()
{
char c, s[50] = {'[=11=]'};
int num;
cout << "Select cin method:" << endl;
cout << "1. cin.get(c);" << endl;
cout << "2. cin.get(s, 10);" << endl;
cout << "3. cin.get(s, 10, '*');" << endl;
cout << "4. cin.getline(s, 10);" << endl;
cout << "5. cin.read(s, 10);" << endl;
cout << "Select: " << flush;
cin >> num;
switch (num) {
case 1:
cin.get(c); // cin >> c;
break;
case 2:
cin.get(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 3:
cin.get(s, 10, '*'); // cin >> s; max length 10, '*' as string terminator
break;
case 4:
cin.getline(s, 10); // cin >> s; max length 10, '\n' as string terminator
break;
case 5:
cin.read(s, 10); // cin >> s; max length 10, records '\n'
break;
default:
break;
}
if (num == 1)
cout.put(c); // cout << s;
if (num >= 2 && num <= 5)
cout.write(s, 15); // cout << s; max length 15
}
每当我为 num 输入 1/2/4 时,它都会绕过 switch 和 else-if 语句。我已经尝试检查 "cout << num" 得到了什么 num 并且它得到的值是正确的。我也没有收到任何错误消息。以下是我得到的示例:
Select cin method:
1. cin.get(c);
2. cin.get(s, 10);
3. cin.get(s, 10, '*');
4. cin.getline(s, 10);
5. cin.read(s, 10);
Select: 1
--------------------------------
Process exited after 1.676 seconds with return value 0 Press any key to continue
它不会绕过 switch
,它会正确执行。问题是,在读取数字后,输入流中有一个额外的字符(您在数字后按的 \n
),这会导致您在情况 1、2 和 4 中看到的行为。在这些情况下,您正在执行的下一次读取操作会发现 \n
并停止读取。
要修复它,您可以在 cin >> num
:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
(并包括 <limits>