【问题标题】:Input Validation to Accept Only Int Values输入验证以仅接受 Int 值
【发布时间】:2020-05-11 19:37:18
【问题描述】:

我正在设计一个程序来读取多个用户输入,并且规范包括它们始终是正整数。然后将修改这些值以仅保留数百个值并用于显示条形图。我遇到的问题是程序的输入验证。如果用户无视指令并输入十进制值而不是整数,程序输出的其余部分将立即显示,用户无法输入任何附加值。下面的 if 语句显示了我试图解决这个问题的方法。

int main() {
    int store1;
    int store2;


    cout << "Enter today's sales for store 1:" << endl;
    cin >> store1;
    if(store1 < 0 || ((store1 % 1))!=0)
        cout << "Value of sales must be an integer greater than zero" << endl;

    cout << "Enter today's sales for store 2:" << endl;
    cin >> store2;
    if(store2 < 0 || ((store2 % 1))!=0)
        cout << "Value of sales must be zero or greater" << endl;

如果用户输入小数,程序输出为:

Enter today's sales for store 1:
400.34
Enter today's sales for store 2:
Enter today's sales for store 3:
Value of sales must be zero or greater
Enter today's sales for store 4:
Enter today's sales for store 5:
SALES BAR CHART
(Each * = $100)
Store 1: ****
Store 2: 
Store 3: 
Store 4: 
Store 5: 

Process finished with exit code 0

如果我不得不猜测,小数点后的值(在本例中为 3 和 4)分别被传递给存储 2 和 3 的输入,并且由于它们小于 100,因此不会在相应的旁边显示 *店铺。为了便于阅读,我没有包含我的代码来获取数百个值和输出 *,而且因为我几乎肯定我的问题与 cin 有关。

总而言之,我需要验证用户输入,以便通过任何输入法只接受一个正整数。任何帮助是极大的赞赏!

【问题讨论】:

  • 对于输入验证,do-while 做得最好。

标签: c++ validation input type-conversion output


【解决方案1】:

您可以使用strtoll 来转换和验证编码为整数的输入字符串。

cin 读取的值是string 类型,然后将其传递给get_positive_integer(),它是strtoll 的包装器,转换并验证字符串,当字符串不在预期格式,否则返回解码后的正整数。请注意使用中间 long long 类型来轻松检查溢出,并使用 getline 读取整个输入行,而不会在空格处中断字符串,这会将第一行的输入传递到下一个读数。

#include <climits>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int get_positive_integer(const string& str)
{
    const char* s = str.c_str();
    char* end;
    long long val = strtoll(s, &end, 10);
    if (end == s) {
        // str was not encoding an integer.                                                                                                            
        return -1;
    }
    if (*end) {
        // Stopped reading before the end of the string                                                                                                
        // the whole string was not an integer.                                                                                                        
        return -1;
    }
    if (val > INT_MAX) {
        // Too big to fit in an int type                                                                                                                
        return -1;
    }
    if (val <= 0) {
        // Not a positive integer.                                                                                                                     
        return -1;
    }
    return val;
}

int main()
{
    string input;

    cout << "Enter a positive integer:" << endl;
    getline(cin, input);
    int value = get_positive_integer(input);
    if (value < 0) {
        cout << "Not a valid positive integer" << endl;
        return EXIT_FAILURE;
    }

    cout << "Value: " << value << endl;
    return EXIT_SUCCESS;
}

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2016-08-27
    • 1970-01-01
    • 2022-10-22
    相关资源
    最近更新 更多