【发布时间】:2019-05-04 17:59:52
【问题描述】:
我正在学习 C++ 课程,但在这个项目中我碰壁了。 我应该以 MM/DD/YYYY 的形式接受用户的输入,然后使用结构以几种方式对其进行操作。我尝试了几种不同的方法来分配结构中的变量,但我永远无法让它们代表任何值
我试过 getline(cin, data); 以及gets();
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct date
{
string day;
string month;
string year;
} pinfo;
string RunAgain;
int main()
{
struct date user;
while (1)
{
string user_entry;
cout << "Please enter date in the form of MM/DD/YYYY ";
getline(cin, user_entry);
string str = user_entry;
// month is from position 0, to until first / found
pinfo.month = str.substr(0, str.find("/", 0));
str = str.substr(str.find("/", 0) + 1, str.length()); // new string
user.month = pinfo.month;
// dsy is from position 0 to until first / found
pinfo.day = str.substr(0, str.find("/", 0));
str = str.substr(str.find("/", 0) + 1, str.length()); // new string
user.day = pinfo.day;
// Year is from position 0, to until "\n" found
pinfo.year = str.substr(0, str.find("\n", 0));
str = str.substr(str.find("\n", 0) + 1, str.length()); // new str
user.year = pinfo.year;
string month_alpha;
// error check user entry
if (user.month > "12" || user.month < "1")
{
cout << " Invalid entry months must be 1-12";
}
// convert numeric month to alpha
if (pinfo.month == "01")
{
month_alpha = "January";
}
else if (pinfo.month == "02")
{
month_alpha = "febuary";
}
else if (pinfo.month == "03")
{
month_alpha = "March";
}
else if (pinfo.month == "04")
{
month_alpha = "April";
}
else if (pinfo.month == "05")
{
month_alpha = "May";
}
else if (pinfo.month == "06")
{
month_alpha = "June";
}
else if (pinfo.month == "07")
{
month_alpha = "July";
}
else if (pinfo.month == "08")
{
month_alpha = "August";
}
else if (pinfo.month == "09")
{
month_alpha = "September";
}
else if (pinfo.month == "10")
{
month_alpha = "October";
}
else if (pinfo.month == "11")
{
month_alpha = "November";
}
else if (pinfo.month == "12")
{
month_alpha = "December";
}
else
{
cout << "Invalid Entry";
}
// check day range
if (pinfo.month == "1" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of January";
}
if (pinfo.month == "2" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of Febuary";
int number = atoi(pinfo.year.c_str());
if (pinfo.month == "2" && number % 4 == 0)
{
cout << pinfo.year << " -> Leap Year!";
}
if (pinfo.month == "3" && (pinfo.day > "31" || pinfo.day < "1"))
cout << pinfo.day << "is not a valid day of March";
}
if (pinfo.month == "3" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of March";
}
if (pinfo.month == "4" && (pinfo.day > "30" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of April";
}
if (pinfo.month == "5" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of May";
}
if (pinfo.month == "6" && (pinfo.day > "30" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of June";
}
if (pinfo.month == "7" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of July";
}
if (pinfo.month == "8" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of August";
}
if (pinfo.month == "" && (pinfo.day > "30" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of September";
}
if (pinfo.month == "1" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of October";
}
if (pinfo.month == "1" && (pinfo.day > "30" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of November";
}
if (pinfo.month == "12" && (pinfo.day > "31" || pinfo.day < "1"))
{
cout << pinfo.day << "is not a valid day of December";
}
// prints month, day, year
cout << pinfo.month << ", " << pinfo.day << ", " << pinfo.year << month_alpha << "(US) \n";
cout << month_alpha << " " << pinfo.day << ", " << pinfo.year << "(US Expanded) \n";
cout << pinfo.day << " " << month_alpha << " " << pinfo.year << "(US Military) \n";
cout << pinfo.year << "-" << pinfo.day << "month_alpha" << "-" << "(International) \n";
RunAgain = "x";
while (RunAgain != "y" && RunAgain != "n")
{
cout << "Run Again (y/n)? \n" << endl;
getline(cin, RunAgain);
}
// Program Exit
if (RunAgain == "n")
{
cout << "Programmer: Christopher Dresser \n\nGoodbye! Press <Enter> key to end the program... ";
getline(cin, RunAgain);
if (RunAgain.empty())
break;
}
}
}
【问题讨论】: