C ++编写一个程序,提示用户以数字形式(投掷/捕获)输入某人的生日

时间:2019-02-22 01:08:57

标签: c++ arrays if-statement struct try-catch

我已经编写了代码,这有点棘手。我正在尝试将其保存到用户输入出生日期(例如:1990年5月12日)的位置,并输出其出生日期为(“ 1990年12月5日”)。我已经为无效月份写了else / ifs和无效的天。出于某种原因,我无法显示日期和年份。例如,它只能正确显示月份“ December”,而Day和Year却得到了大量数字这完全是错误的。此外,我有任何想法要使该程序每四年能够处理一次“飞跃年”。感谢您的帮助。

#include <iostream>
#include <string>

using namespace std;

struct invalidDay : public exception 
{
const char * what() const throw () 
{
    return "Invalid date, try again.";
}
};

struct invalidMonth : public exception 
{
const char * what() const throw () 
{
    return "Invalid month, try again.";
}
};

int main()
{
int day;
int month;
int year;
string monthName;

try 
{
    cout << "Enter birthday: mm/dd/yyyy : ";
    cin >>month>>day>>year;

    if (day > 31)
        throw invalidDay();

    if (month == 1) {
        monthName = "January";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 2) {
        monthName = "February";
        if (day > 28)
            throw invalidDay();
    }
    else if (month == 3) {
        monthName = "March";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 4) {
        monthName = "April";
        if (day > 30)
            throw invalidDay();
    }
    else if (month == 5) {
        monthName = "May";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 6) {
        monthName = "June";
        if (day > 30)
            throw invalidDay();
    }
    else if (month == 7) {
        monthName = "July";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 8) {
        monthName = "August";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 9) {
        monthName = "September";
        if (day > 30)
            throw invalidDay();
    }
    else if (month == 10) {
        monthName = "October";
        if (day > 31)
            throw invalidDay();
    }
    else if (month == 11) {
        monthName = "Noveber";
        if (day > 30)
            throw invalidDay();
    }
    else if (month == 12) {
        monthName = "December";
        if (day > 31)
            throw invalidDay();
    }
    else if (month > 12)
        throw invalidMonth();

    cout << monthName << " " << day << "," << year << "." << endl;


}

catch (invalidDay& e)
{
    cout << e.what() << endl;
}
catch (invalidMonth& e)
{
    cout << e.what() << endl;
}

system("pause");
return 0;



}

2 个答案:

答案 0 :(得分:0)

在这种情况下,>>运算符仅在遇到空白时才停止正常读取。当您尝试按月阅读时,运算符会击中/字符并在流中引发错误标志,因为该字符既不是整数也不是空格的一部分。引发错误标志后,随后的两个读取操作无效,dayyear未初始化。

使用std::getline()分隔输入字符串的各个部分,然后用std::stoi()对其进行解析。

by年可以通过在if语句中添加检查年份是否为a年并在检查日期是否有效时使用单独的条件来轻松解决。

答案 1 :(得分:0)

如前所述,您可以使用std::getline。之后,您可以使用std::substr分割日期并将其解析为一个整数。

示例:

string temp;

cout << "Enter birthday: mm/dd/yyyy : ";
getline(cin, temp);

month = stoi(temp.substr(0, 2));
day = stoi(temp.substr(3,5));
year = stoi(temp.substr(6,10));

输入:

Enter birthday: mm/dd/yyyy : 05/24/2000

输出:

May 24,2000.
相关问题