结束数组输入(c++行业标准)
Ending array input (C++ industry-standards)
我写的代码是工作的,但我觉得可能有一个更好的,标准的方式来结束一个数组的输入,我可能不知道。所讨论的代码位于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,以及可能的其他系统。
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 标准是否严格定义了该程序应该如何编译?
- 如何从Windows应用程序输出到标准?
- 安全到标准:移动会员?
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 标准::变体的赋值运算符
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 如何在 Mac 上使用 c++17 并行标准库算法?
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 结束数组输入(c++行业标准)