我应该如何在C++中使用结构体解决输入失败的问题?

How should I fix this problem with input failure using Structs in C++?

本文关键字:输入 解决 失败 问题 结构体 C++ 我应该      更新时间:2023-10-16

我在计算机科学课上有这个结构作业。问题是在函数上

void input (Time & time);

当我尝试调用该函数时,错误

FATAL ERROR: Input failure

被打印出来。以下是该函数应该执行的操作:

无效输入(时间和时间(; 输入的工作是输入一个看起来像 星期三 19:05 并相应地设置 Time arg(在本例中为 {3, true, 7, 5}(。如果输入失败或无效,请投诉并死亡。(您的 ok 函数可以执行部分(不是全部(此检查。您将(尝试(输入 5 件事:天、小时、冒号、分钟、安培。如果任何输入使 cin 处于错误状态,请抱怨并死亡。如果日期与 const 全局日期名称数组中的某一天不完全匹配,请抱怨并死亡。(让我们通过说日期名称必须完全符合我们期望的大小写来使生活更轻松一点,对用户来说更难一点:第一个字母大写,其余字母小写。这样我们就不必为大小写转换而烦恼。如果小时不在法定范围内,请抱怨并死亡。如果我们在一小时后输入的字符不是冒号,请抱怨并死亡。如果分钟不在法定范围内,请抱怨并死亡。如果 AM/PM 指定不完全匹配"AM"或"PM"(区分大小写(,请抱怨并死亡。如果我们活下来,我们的工作就是设定时间。 例如,星期三晚上 7:05 抱怨并死亡(错误的一天名称(案例错误(( 例如,星期三 x:00 PM 抱怨并死亡(糟糕的时间( 例如,星期三下午 0:00 抱怨并死亡(糟糕的时间( 例如,星期三 7;05 PM 抱怨并死亡(坏结肠( 例如,星期三晚上 7 点抱怨并死亡(坏结肠( 例如,星期三晚上 7:x 抱怨并死亡(糟糕的分钟( 例如,星期三晚上 7:60 抱怨并死亡(糟糕的一分钟( 例如,星期三晚上 7:05 抱怨并死亡(错误的 AMPM(案例错误(( 您不必为所有这些可能性生成不同的错误消息,您可以说类似 致命错误:虚假时间输入 如果输入有任何问题。

如果有人能告诉我我做错了什么,我将不胜感激。我删除了与该问题无关的所有部分。

#include<iostream>
#include<string>
#include<sstream>
#include<cctype>
using namespace std;
//Structs
struct Address {
unsigned number;
string street;
string suffix;
string city;
string state;
unsigned zip;
};
struct Time {
unsigned day;
bool pm;
unsigned hour;
unsigned minute;
};
//Prototypes
void show(const Address & address);
void show(const Address address[], unsigned elements, unsigned desiredZip);
void show(const Address address[], unsigned addressElements, const unsigned desiredZip[], unsigned desiredZipElements);
void show(const Time & time);
bool ok(const Time & time);
int compare(const Time & time0, const Time & time1);
bool die(const string & msg);
int nameAsDay(const string &day); //My own function
void input(Time & time);
const string days[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int main() {
Address addList[7] = { a1, a2, a3, a4, a5, a6, a7 };
unsigned zipList[2] = { 91324, 91325 };
Time t1 = { 0, false, 12, 1 };
Time t2 = { 0, 0, 12, 0 };
Time t3 = { 4, true, 5, 30 };
Time t4 = { 1, false, 11, 60 };
Time t5 = { 7, 1, 9, 1 };
Time t6 = { 0, 0, 12, 0 };
Time t7;
show(a1);
show(addList, 7, 91329);
show(addList, 7, zipList, 2);
show(t2);
cout << ok(t2) << endl;
cout << compare(t1, t2) << endl;
input(t3);
show(t3);

system("pause");
return 0;
}
//Displays time and day
void show(const Time & time) {
cout << days[time.day] << " " << time.hour << ":";
if (time.minute == 0)
cout << "00";
else if (time.minute < 10)
cout << "0" << time.minute;
else
cout << time.minute;
if (time.pm == 0)
cout << " AM" << endl;
else
cout << " PM" << endl;
}
//Determines if the data for struct Time is valid
bool ok(const Time & time) {
if ((time.day < 0) || (time.day > 6))
return false;
if ((time.hour < 1) || (time.hour > 12))
return false;
if ((time.minute < 0) || (time.minute > 59))
return false;
return true;
}
//Compares two times to see which comes first
int compare(const Time & time0, const Time & time1) {
//Check that time is correctly entered
if ((!ok(time0)) || (!ok(time1)))
die("Unable to interpret input");
if (time0.day < time1.day)
return -1;
else if ((time1.day == time0.day) && (time1.pm == true) && (time0.pm == false))
return -1;
else if ((time1.day == time0.day) && (time1.pm == time0.pm) && (time0.hour < time1.hour))
return -1;
else if ((time1.day == time0.day) && (time1.pm == time0.pm) && (time1.hour == time0.hour) && (time0.minute < time1.minute))
return -1;
else if ((time1.day == time0.day) && (time1.pm == time0.pm) && (time1.hour == time0.hour) && (time1.minute == time0.minute))
return 0;
else
return 1;
}
//Die function, exits program
bool die(const string & msg) {
cout << endl << "FATAL ERROR: " << msg << endl;
return -1;
}
string convertDayToString(const Time &time) {
const string day[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
return days[time.day];
}
//My own function - changes day to an element #
int nameAsDay(const string &day) {
if (day == "Sunday")
return 0;
else if (day == "Monday")
return 1;
else if (day == "Tuesday")
return 2;
else if (day == "Wednesday")
return 3;
else if (day == "Thursday")
return 4;
else if (day == "Friday")
return 5;
else if (day == "Saturday")
return 6;
else 
return 100; //Input failure
}
//Set the time argument and discern errors
void input(Time & time) {
string userInput;
unsigned found, hour, minute;
if (nameAsDay(userInput) == 100) 
die("Input failure.");
else 
time.day = nameAsDay(userInput);
cin >> userInput;
found = userInput.find(":");
if (found == 2) {
if (isdigit(userInput[0]) && isdigit(userInput[1])) {
stringstream(userInput.substr(0, 2)) >> hour;
if ((hour > 0) && (hour < 13))
time.hour = hour;
else
die("Hour out of range.");
}
else
die("Invalid hour input.");
if (isdigit(userInput[3]) && isdigit(userInput[4])) {
stringstream(userInput.substr(3, 1)) >> minute;
if ((minute >= 0) && (minute <= 59))
time.minute = minute;
else
die("Minute out of range.");
}
else
die("Invalid minute input.");
}
else
die("Invalid time input.");
cin >> userInput;
if (userInput == "AM")
time.pm = false;
else if (userInput == "PM")
time.pm = true;
else
die("Invalid AM/PM input.");
}

在开始测试之前,您应该阅读输入。这

cin >> userInput;
if (nameAsDay(userInput) == 100) 
die("Input failure.");
else 
time.day = nameAsDay(userInput);

而不是这个

if (nameAsDay(userInput) == 100) 
die("Input failure.");
else 
time.day = nameAsDay(userInput);
cin >> userInput;

虽然仔细观察,我想知道代码是否不是这个

cin >> userInput;
if (nameAsDay(userInput) == 100) 
die("Input failure.");
else 
time.day = nameAsDay(userInput);
cin >> userInput;

无论哪种方式,您都需要在调用nameAsDay之前读取一些输入。