c++ - 将解析的数据输入结构时遇到问题
问题描述
我正在学习 C++ 课程,在这个项目中我遇到了障碍。我应该以 MM/DD/YYYY 的形式接受用户的输入,然后使用结构以几种方式对其进行操作。我尝试了几种不同的方法来分配结构中的变量,但我永远无法让它们代表任何值
我试过 getline(cin, data); 以及获取();
#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;
}
}
}
解决方案
您的程序非常复杂,因为您将字符串用于数值,并且因为没有其他任何东西,有很多测试什么都没有(如果您知道您在给定月份,为什么要检查您是否在其他月份之后?)。你还要检查pinfo.day < "1"
所有的月份,只做一次就足够了。
还有一些错误,例如
if (pinfo.month == "2" && (pinfo.day > "31" || pinfo.day < "1"))
2 月份从来没有 31 天,如果上面的测试是真的,表明 2 月份有一个错误,无论如何你都会这样做
int number = atoi(pinfo.year.c_str()); if (pinfo.month == "2" && number % 4 == 0) { cout << pinfo.year << " -> Leap Year!" << endl; } if (pinfo.month == "3" && (pinfo.day > "31" || pinfo.day < "1")) cout << pinfo.day << "is not a valid day of March" << endl;
所以你测试跳跃案例太晚了,你还检查了火星的案例(可能是复制/粘贴错误)。
我鼓励您将日期和月份转换为数字,并在与月份相对应的索引处包含一个包含最大可能天数/月份名称的数组,如果您只想要一个近似值,请使用 2 月的特殊管理,否则使用可用的库来检查。
当您打印错误消息时,用换行符刷新它,否则下一个打印将折叠到它。
您是否再次运行的方式也很复杂,如果通过getline进入的最后一行不为空,为什么要中止退出?
推荐阅读
- lisp - Lisp 中用于 minimax-alpha-beta 的 REVERSE 函数
- css - 跨项目共享css和ts
- uwp - UWP XAML Prism 未正确绑定数据
- javascript - 向 Bootstrap 4 导航栏下拉菜单添加幻灯片效果(在菜单外部关闭)的问题
- jquery - asp.net核心中的xml绑定
- javascript - 如果最小化,Cordova Windows 应用程序脚本不会执行
- html - 不从Angular 7中的本地路径加载图像
- c - Prime 与否 Prime 输出缓慢
- kubernetes - Kubernetes:了解资源请求/限制和调度
- java - Jersey 2 请求过滤器在此代码中不起作用