如何实现可识别非整数输入并退出的C++数组填充函数?
问题:fillArray函数无法在输入非整数时正确退出循环
我是本学期C++入门课程的新生,几乎没有编程经验。我编写了fillArray()函数用于将用户输入存入numArray数组,该函数在仅输入数值且不提前退出时运行正常,但无法在输入非整数时正确退出循环。我尝试在fillArray()中编写if/else语句实现该功能,但未达到预期效果。作业要求见代码注释,目前还未处理reverseArray()部分,先逐个解决问题。现有代码如下:
// arrays.cpp /* Write a program that uses the following functions: Fill_array() takes as arguments the name of an array of int values and an array size. It prompts the user to enter int values to be entered in the array. It ceases taking input when the array is full or when the user enters non-numeric input, and it returns the actual number of entries. Show_array() takes as arguments the name of an array of int values and an array size and displays the contents of the array. Reverse_array() takes as arguments the name of an array of int values and an array size and reverses the order of the values stored in the array. The program should use these functions to do the following: fill an array, display the array, reverse the array, display the array, reverse all but the first and last elements of the array, and finally, display the array. Name your program arrays.cpp */ #include <iostream> using namespace std; void fillArray(int array[], int size) { int inputNum; for(int index = 0; index < size; index++) { cout << "Enter an integer: "; cin >> inputNum; if((inputNum > -1000) && (inputNum < 1000)) array[index] = inputNum; else break; } } void showArray(int array[], int size) { for(int index = 0; index < size; index++) { cout << array[index] << endl; } } int main() { int numArray[10]; fillArray(numArray, 10); showArray(numArray, 10); return 0; }
我尝试了网上找到的多种if/else语句写法,仍无法让函数在输入非整数时正确退出循环。
问题分析
你的代码核心问题在于:当输入非整数时,cin >> inputNum会直接失败,输入流进入错误状态,inputNum的值不会被更新,这时你判断inputNum范围的逻辑完全起不到检测非整数的作用。另外,作业要求fillArray返回实际输入的条目数,但你的函数是void类型,也不符合要求。
修改方案
以下是调整后的代码,解决输入非整数退出的问题,同时满足作业对返回值的要求:
#include <iostream> #include <limits> // 用于处理输入流的ignore操作 using namespace std; // 修改返回值为int,返回实际输入的元素个数 int fillArray(int array[], int size) { int inputNum; int count = 0; // 记录实际输入的元素数量 for(int index = 0; index < size; index++) { cout << "Enter an integer: "; // 检测输入是否成功:如果输入非整数,cin提取操作会失败,条件成立 if(!(cin >> inputNum)) { // 清除输入流的错误状态,避免后续操作失效 cin.clear(); // 忽略缓冲区中剩余的错误输入,直到换行符 cin.ignore(numeric_limits<streamsize>::max(), '\n'); break; // 退出循环 } // 如果需要保留原来的数值范围判断,取消下面的注释即可 // if(inputNum >= -1000 && inputNum <= 1000) // { array[index] = inputNum; count++; // } // else // { // break; // } } return count; // 返回实际输入的元素个数 } void showArray(int array[], int size) { for(int index = 0; index < size; index++) { cout << array[index] << endl; } } int main() { int numArray[10]; // 接收fillArray返回的实际输入数量,传给showArray避免输出未初始化的垃圾值 int actualCount = fillArray(numArray, 10); showArray(numArray, actualCount); return 0; }
关键修改点说明
- 输入状态检测:用
!(cin >> inputNum)判断输入是否成功,这是检测非整数输入的正确方式——当输入无法转换为int时,cin的提取操作会失败,触发退出逻辑。 - 流状态处理:输入失败后必须用
cin.clear()清除错误状态,否则后续所有cin操作都会直接失败;cin.ignore()则是清空缓冲区里的错误输入内容,避免影响后续操作。 - 返回实际条目数:将fillArray改为int返回类型,用count记录实际输入的元素数量,符合作业要求;同时main函数中用这个数值调用showArray,避免输出数组中未初始化的垃圾值。
内容的提问来源于stack exchange,提问作者JoshF
相关产品推荐
相关产品推荐

