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

使用 cin 的良好輸入驗(yàn)證循環(huán) - C++

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

問題描述

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

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:

這個(gè)循環(huán)是我構(gòu)建的一種很好的驗(yàn)證輸入的方法嗎?或者有更常見/可接受的方法嗎?

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 錯(cuò)誤還不夠特殊,因?yàn)殄e(cuò)誤通常很可能發(fā)生.我更喜歡只在不太頻繁的錯(cuò)誤情況下使用異常.

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.

代碼還不錯(cuò),但跳過 80 個(gè)字符有點(diǎn)隨意,如果你擺弄循環(huán),錯(cuò)誤變量不是必需的(如果你保留它應(yīng)該是 bool).您可以將 cin 的讀取內(nèi)容直接放入 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 個(gè)字符之外,這些只是小問題,更多的是喜歡的風(fēng)格.

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

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: a级网站 | 一区二区三区欧美 | 毛片毛片毛片毛片 | 国产精品毛片一区二区三区 | 精品久久久久一区二区国产 | 亚洲黄色av网站 | 日韩一级黄色毛片 | 亚洲一区二区精品视频 | 久久久久久免费毛片精品 | 久久99精品久久久久久 | 亚洲国产69 | 国产免费又黄又爽又刺激蜜月al | 九九热这里 | 国产精品成人品 | 国产亚洲一区二区在线观看 | 国产一区二区三区在线 | 久久久久久精 | 久久久国产一区 | 精品久久久久久久久久 | 亚洲精品天堂 | 狠狠的干狠狠的操 | 亚洲成人三级 | 日韩欧美大片 | 日本一区二区在线视频 | 操久久| 日韩免费视频 | 草草草影院 | 亚洲在线一区二区三区 | 亚洲精品久久嫩草网站秘色 | 亚洲+变态+欧美+另类+精品 | 国产一区二区三区视频免费观看 | 国产精品免费一区二区三区四区 | 精品久| julia中文字幕久久一区二区 | 台湾佬久久 | 亚洲美女视频 | 成人性生交a做片 | 欧美黑人一级爽快片淫片高清 | 久久免费观看一级毛片 | 国产激情小视频 | 亚洲国产激情 |