簡體   English   中英

如何制作不接受浮點值的整數驗證函數?

[英]How to make an integer validation function that does not accept floating point values?

getRegionTotal()是我現在用於驗證的函數。 它的工作原理非常好,因為如果用戶輸入“二十”或“ -7”之類的東西,它將不會接受,並且會一直要求新值,直到獲得有效的值為止。 但是,如果用戶為北部地區的事故數量輸入60.7,它將接受60並刪除.7部分。 然后,當詢問南部地區的事故數量時,它將提供常規說明和更具體的說明。

//These will hold the number of accidents in each region last year
int northTotal = 0;
int southTotal = 0;
int eastTotal = 0;
int westTotal = 0;
int centralTotal = 0;

//passing 0 for northTotal, southTotal etc. because main doesn't know
//values of them until the function returns a value. When it returns a value
//it will go into the variables on the left. getRegionTotal will get the number
//of accidents for a region from the user and prompt the user using the string that
//is in the first argument.
northTotal = getRegionTotal("North", northTotal);
southTotal = getRegionTotal("South", southTotal);
eastTotal = getRegionTotal("East", eastTotal);
westTotal = getRegionTotal("West", westTotal);
centralTotal = getRegionTotal("Central", centralTotal);


int getRegionTotal(string regionName, int regionTotal)
{
    //instructs user to enter number of accidents reported in a particular region
    cout << "\nNumber of automobile accidents reported in " << regionName << " " << cityName << ": ";
    //while regionTotal is not an integer or regionTotal is negative
    while (!(cin >> regionTotal) || (regionTotal < 0) )
    {
        //give user more specific instructions
        cout << "\nPlease enter a positive whole number for the number of\n";
        cout << "automobile accidents in " << regionName << " " << cityName << ": ";
        cin.clear(); //clear out cin object
        cin.ignore(100, '\n'); //ignore whatever is in the cin object
                                //up to 100 characters or until
                                // a new line character
    }
    //returns a valid value for the number of accidents for the region
    return regionTotal;
}

解析整行,並確保您已消耗整行。

使用iostream:

#include <iostream>
#include <sstream>
#include <string>

for (std::string line; std::getline(std::cin, line); )
{
    std::istringstream iss(line);
    int result;

    if (!(iss >> result >> std::ws && iss.get() == EOF))
    {
        // error, die. For example:

        std::cout << "Unparsable input: '" << line << "'\n";
        continue;
    }

    // else use "result"
}

使用stdlib:

#include <errno>
#include <cstdlib>

char const * input = line.c_str();   // from above, say
char * e;
errno = 0;

long int result = std::strtol(input, &e, 10);

if (e == input || *e != '\0' || errno != 0)
{
    // error
}

兩種方法在根本上是相同的,但前一種可能更像是“慣用的C ++”。 就是說,如果您已經有一個字符串,則strtol -approach是一個很好的選擇,因為它可以為您提供精確的錯誤處理:是否消耗了整個字符串(如果沒有,則e指向下一個字符); 您是否消耗了任何字符串(如果沒有,則e指向開頭); 是否有上溢或下溢(檢查errno )。 另一方面,iostreams方法使您可以使用結尾的空格(由於>> std::ws ),而strtol -solution則不會。

還有std::stol ,它包裝了strtol (和類似的strtoull / strtod等),但是它在出錯時引發了異常,我相信異常不是構造正常行為(如讀取用戶輸入)的控制流的正確工具。 而且,您無法控制這些包裝程序的運行方式; 例如,即使他們不使用整個字符串(但不告訴您他們走了多遠),也可以成功,並且您無法指定數字基數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM