久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 cin 的良好輸入驗證循環 - C++

Good input validation loop using cin - C++(使用 cin 的良好輸入驗證循環 - C++)
本文介紹了使用 cin 的良好輸入驗證循環 - C++的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在上我的第二堂 OOP 課,我的第一堂課是用 C# 教授的,所以我是 C++ 的新手,目前我正在練習使用 cin 進行輸入驗證.所以這是我的問題:

I'm in my second OOP class, and my first class was taught in C#, so I'm new to C++ and currently I am practicing input validation using cin. So here's my question:

這個循環是我構建的一種很好的驗證輸入的方法嗎?或者有更常見/可接受的方法嗎?

Is this loop I constructed a pretty good way of validating input? Or is there a more common/accepted way of doing it?

謝謝!

代碼:

int taxableIncome;
int error;

// input validation loop
do
{
    error = 0;
    cout << "Please enter in your taxable income: ";
    cin >> taxableIncome;
    if (cin.fail())
    {
        cout << "Please enter a valid integer" << endl;
        error = 1;
        cin.clear();
        cin.ignore(80, '
');
    }
}while(error == 1);

推薦答案

我不太喜歡為 iostream 打開異常.I/O 錯誤還不夠特殊,因為錯誤通常很可能發生.我更喜歡只在不太頻繁的錯誤情況下使用異常.

I'm not a huge fan of turning on exceptions for iostreams. I/O errors aren't exceptional enough, in that errors are often very likely. I prefer only to use exceptions for less frequent error conditions.

代碼還不錯,但跳過 80 個字符有點隨意,如果你擺弄循環,錯誤變量不是必需的(如果你保留它應該是 bool).您可以將 cin 的讀取內容直接放入 if 中,這可能更像是 Perl 慣用語.

The code isn't bad, but skipping 80 characters is a bit arbitrary, and the error variable isn't necessary if you fiddle with the loop (and should be bool if you keep it). You can put the read from cin directly into an if, which is perhaps more of a Perl idiom.

這是我的看法:

int taxableIncome;

for (;;) {
    cout << "Please enter in your taxable income: ";
    if (cin >> taxableIncome) {
        break;
    } else {
        cout << "Please enter a valid integer" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '
');
    }
}

除了只跳過 80 個字符之外,這些只是小問題,更多的是喜歡的風格.

Apart from only skipping 80 characters, these are only minor quibbles, and are more a matter of preferred style.

這篇關于使用 cin 的良好輸入驗證循環 - C++的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产精品一区二区免费 | 天天射网站 | 精品国产不卡一区二区三区 | 亚洲第一av网站 | 国产伦一区二区三区四区 | 日韩aⅴ视频 | 天天看天天摸天天操 | 久久久久国产一区二区三区四区 | 蜜桃在线一区二区三区 | 日韩免费看片 | 亚洲综合日韩精品欧美综合区 | 日韩电影免费在线观看中文字幕 | 国产欧美一区二区三区免费 | 亚洲福利一区二区 | 欧美视频一区 | 色综合一区二区三区 | 中文字幕成人av | 久久精品成人 | 久久精品国产v日韩v亚洲 | 久久久久久久一区 | 成人免费精品 | 国产福利视频 | www.干| 亚洲欧洲视频 | 一区二区精品电影 | 久久久久久久久久影视 | 精品国产网 | 亚洲一区二区av在线 | 国产成人精品一区二三区在线观看 | 成人在线观看中文字幕 | 99爱在线观看 | 午夜精品久久久久久久久久久久久 | 色一阁| 日本特黄a级高清免费大片 国产精品久久性 | 久久一二区 | 黄色免费网址大全 | 97综合在线 | 亚洲欧美视频在线观看 | av网站在线播放 | 久久久天天 | 日韩av免费在线电影 |