我写的代码是工作的,但我觉得可能有一个更好的,标准的方式来结束一个数组的输入,我可能不知道。所讨论的代码位于getInputForArray()中,如果在整数输入中检测到cin.fail(),则通过停止循环来实现。有没有更好的办法?谢谢,所有。
#include <iostream>
using namespace std;
int sumOfArray(int arr[], int arrlen);
void getInputForArray(int arr[], int arrlen);
int main() {
const int LIST_SZ = 256;
int mylist[LIST_SZ];
// Zero out the array
for (int i = 0; i < LIST_SZ; i++)
mylist[i] = 0;
// Get user input for array
getInputForArray(mylist, LIST_SZ);
// Print the sum of all the numbers in the array
cout << "nThe sum is: " << sumOfArray(mylist, LIST_SZ) << endl;
return 0;
}
int sumOfArray(int arr[], int arrlen) {
int total = 0;
for (int i = 0; i < arrlen; i++)
total = arr[i] + total;
return total;
}
void getInputForArray(int arr[], int arrlen) {
cout << "Input any non-numerical value to denote the end of your inputn";
for (int i = 0; i < arrlen; i++) {
if (!cin.fail()) {
cout << "Please input a value for the [" << i
<< "] position of the array: ";
cin >> arr[i];
}
}
}
其他回答解决了EOF(或输入结束)选项。
OP可以修改用户输入检查,当非数字输入两次时停止循环。我还认为更好的做法是使用std::vector
而不是原始数组。
这是一个可能的实现:
std::vector<int> getDataFromInput( size_t max_data )
{
using std::cout;
std::vector<int> v;
std::string value;
bool stop_reading = false;
while ( v.size() < max_data )
{
cout << "Please input a value for position [" << v.size() << "] of the vector: ";
// input a string, if End of Input, stop reading
if ( !(std::cin >> value) ) break;
try
{ // if the user entered a valid number, add it to the vector
int num = stoi(value);
v.push_back(num);
stop_reading = false;
}
catch ( const std::invalid_argument &e )
{
if ( stop_reading ) break;
cout << "You entered a non-numerical value.nPlease, enter another "
<< "non-numerical value to stop input or a number to continuen";
stop_reading = true;
}
catch ( const std::out_of_range &e )
{
cout << "The value entered can't be represented by an int.n";
}
}
return v;
}
编辑
随着Lightness在轨道上的赛跑指出:
通过发送EOF来停止输入更符合"行业标准"。你可以通常在终端使用^D (Linux/Mac)或^Z (Windows)。
所以你可以把前面的代码段简化成这样:
std::vector<int> getDataFromInput( size_t max_data )
{
using std::cout;
std::vector<int> v;
std::string value;
while ( v.size() < max_data )
{
cout << "Please input a value for position [" << v.size()
<< "] of the vector: ";
if ( !(std::cin >> value) ) break;
try
{
int num = stoi(value);
v.push_back(num);
}
catch ( const std::invalid_argument &e )
{
cout << "You entered a non-numerical value.n";
}
catch ( const std::out_of_range &e )
{
cout << "The value entered can't be represented by an int.n";
}
}
return v;
}
你可以试试这样做。
void getInputForArray(int arr[], int arrlen) {
char c = 0;
size_t size = 0;
cout << "Press escape (ESC) to stop inputting values" << endl;
while(size < arrlen) {
cout << "Enter a number for position " << size << " of the array: ";
cin >> arr[size++];
fflush(stdin);
c = getchar();
if(c == 27) { // check for the escape key
break; // jump out of the loop
}
// Check non-numeric values.
if(cin.fail()) {
cout << "Error: numerical values only!" << endl;
cin.clear(); // clear the input
size--; // go back to previous position
}
}
}
或者,您可以通过将检查转义键的if(c == 27)
更改为if(c == EOF)
来检查EOF(文件结束)。EOF是一个宏,因为它的值依赖于系统。但是,我不推荐这种替代方法,因为每个系统上的键序列是不同的。Ctrl+Z用于基于dos或CP/M的系统,Ctrl+D用于类unix系统,Ctrl+用于AmigaOS,以及可能的其他系统。