gpt4 book ai didi

c++ 无法清除 inputStream

转载 作者:行者123 更新时间:2023-11-28 05:28:33 24 4
gpt4 key购买 nike

<分区>

我是 C++ 的新手,正在尝试创建一个基本程序。请求两个值并将结果存储在单独的变量中,如下所示:

#include <iostream>

using namespace std;

int main () {

int sizeOfArray = -1, bufferSize = -1;

while (true){
cout << "Enter the size of the array: " << endl;
cin >> sizeOfArray;
if (cin.fail())
cin.clear();


cout << "Enter the size of the buffer (k): " << endl;
cin >> bufferSize;
if (cin.fail())
cin.clear();


if (sizeOfArray > 0 && bufferSize > 0){
break;
}

}

return 0;
}

但是,当输入一个不是 int 类型的值而不是清除并请求下一个输入时,我遇到了一个无限循环,如下所示:

Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): Enter the size of the array: Enter the size of the buffer (k): ^C

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com