[英]How can I get an if-statement that takes multiple cin inputs to recognize an error if only the first cin input is incorrect?
我正在構建一個程序,旨在將來自星期幾/值對的值存儲到一個向量中,然后顯示和總結一周中每一天的值。 因此,我要求用戶為每個 cin 輸入輸入一個字符串(星期幾,可以在 4 個不同的 forms 中的每一天)和一個 integer (相應的值)輸入。 (到目前為止,該程序僅顯示星期一和退出條件;我稍后會將其擴展為包括星期二到星期日。)
該程序還旨在識別不正確的輸入(並允許用戶重試他們的輸入)。 但是,如果只有星期幾輸入不正確,我無法讓程序識別錯誤輸入。 例如,如果我輸入“test test”作為輸入,程序將成功宣布“檢測到錯誤的星期幾”。 如果我輸入“Monday x”,它也會宣布此消息(即使措辭需要調整)。 但是,如果我輸入“測試 5”,程序會接受它而不顯示“不正確的星期幾”消息。
如何更改我的程序,以便使用預先存在的 else 語句在我輸入“測試 5”之類的內容時顯示“不正確的星期幾”?
一種解決方案是創建一個很長的 if 語句,如果輸入的星期幾與 29 個有效的星期幾條目中的任何一個都不匹配(例如“星期一”、“星期一”、“星期一”、“星期一、“星期二”、“星期二”……)。 但是,我想找到一種更簡單的方法。
謝謝您的幫助!
#include "../std_lib_facilities.h"
#include <iostream>
//Program in progress. Completing as part of my independent study of Programming: Principles and Practice by Bjarne Stroustrup.
int main()
{
string dayofweek;
int value;
vector<int> mondayvalues;
vector<int> tuesdayvalues;
vector<int> wednesdayvalues;
vector<int> thursdayvalues;
vector<int> fridayvalues;
vector<int> saturdayvalues;
vector<int> sundayvalues;
int incorrectentrycounter = 0;
int mondaysum = 0;
cout << "Please enter days of the week followed by values in integer form. When you are finished, please enter Done 0.\n";
string incorrectnumdump;
while (cin) {
if (cin >> dayofweek >> value) {
if (dayofweek == "Monday" || dayofweek == "monday" || dayofweek == "Mon" || dayofweek == "mon") {
mondayvalues.push_back(value);
}
if ((dayofweek == "Done") && (value == 0)) {
break;
}
}
else {
cin.clear();
cin >> incorrectnumdump;
cout << "Incorrect day of week detected; please try again.\n";
incorrectentrycounter++;
continue;
}
}
cout << "Here are the values you entered for each day of the week, along with their sums:\n";
cout << "Monday: ";
for (int x : mondayvalues)
cout << x << " ";
for (int i = 0; i < mondayvalues.size(); i++) {
mondaysum += mondayvalues[i];
}
cout << "\nSum of Monday values: " << mondaysum;
cout << "\nThere were " << incorrectentrycounter << "entries that displayed non-valid days of the week.";
}
嗯..也許您可以使用 ASCII 技術在內部將所有輸入翻轉為全部大寫或全部小寫字母,這樣您就不必在 if 語句中查找這兩種情況,只要大寫或小寫字母無關緊要. 制作一個 function 接收輸入並將它們轉換為全部大寫或全部小寫。 在將輸入分配給變量之前,請使用此 function。 這將使檢查它們更容易。 之后,您可以創建一個包含所有日期的 std::string 的 const 數組 [7]。 您可以通過排除法檢查您的輸入。 您將在以后比較輸入和數組天數,並消除每次不匹配的天數。 如果所有天都被消除,那么輸入錯誤。 如果還有 2 天或更長時間 - 輸入不足。 如果還有 1 天,那就是正確的輸入! 讓我知道您是否需要幫助
在玩弄了代碼之后,我選擇了這個解決方案,我認為它可以滿足我的需求。 代碼現在有兩個無效的入口語句塊而不是一個。 第一個(“檢測到不正確的星期幾”)在if (cin >> dayofweek >> value)
條件內,並將識別與星期一字符串之一或 Done 不匹配的 dayofweek 條目。
第二個(“無效條目”)在if (cin >> dayofweek >> value)
條件之外但在while (cin)
條件內。 我添加了第二個,以便如果有人輸入“測試測試”之類的內容,他們將收到錯誤通知,但 while 循環仍將繼續。
因此,似乎當用戶需要通過 cin >> 輸入多個值時,輸入驗證的一種方法可以是在代碼的不同級別輸入多個錯誤消息。
這個解決方案仍然有點笨拙,但允許我只使用“else”{}而不是“else”后跟一長串條件來捕獲無效條目。
#include "../std_lib_facilities.h"
#include <iostream>
//Program in progress. Completing as part of my independent study of Programming: Principles and Practice by Bjarne Stroustrup.
int main()
{
string dayofweek;
int value;
vector<int> mondayvalues;
vector<int> tuesdayvalues;
vector<int> wednesdayvalues;
vector<int> thursdayvalues;
vector<int> fridayvalues;
vector<int> saturdayvalues;
vector<int> sundayvalues;
int incorrectentrycounter = 0;
int mondaysum = 0;
cout << "Please enter days of the week followed by values in integer form. When you are finished, please enter Done 0.\n";
string incorrectnumdump;
while (cin)
{
if (cin >> dayofweek >> value)
{
if (dayofweek == "Monday" || dayofweek == "monday" || dayofweek == "Mon" || dayofweek == "mon")
{
mondayvalues.push_back(value);
}
else if ((dayofweek == "Done") && (value == 0))
{
break;
}
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "Incorrect day of week detected; please try again.\n";
incorrectentrycounter++;
continue;
}
}
else
{
cin.clear();
cin.ignore(10000, '\n');
cout << "Invalid entry; please try again.\n";
incorrectentrycounter++;
continue;
}
}
cout << "Here are the values you entered for each day of the week, along with their sums:\n";
cout << "Monday: ";
for (int x : mondayvalues)
cout << x << " ";
for (int i = 0; i < mondayvalues.size(); i++)
{
mondaysum += mondayvalues[i];
}
cout << "\nSum of Monday values: " << mondaysum << "\n";
cout << "\nThere were " << incorrectentrycounter << " invalid entries.";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.