我正在上C ++类,在这个项目上我碰壁了。 我应该接受来自用户的MM / DD / YYYY形式的输入,然后使用结构以几种方式对其进行操作。我尝试了几种不同的方法来在struct中分配变量,但是我永远无法使它们代表任何值
我尝试过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;
}
}
}
答案 0 :(得分:0)
您的程序非常复杂,因为您使用字符串作为数字值,并且进行了大量的测试,没有别的东西(如果您知道自己在给定月份中,为什么还要检查自己是否在其他月份之后)?您还要检查Core
整个月,只做一次就足够了。
也有类似的错误
Base
2月份永远不会有31天,并且如果以上测试正确,则表明您2月份仍然有错误
pip3 install selenium Collecting selenium Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/selenium/ Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/selenium/ Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/selenium/ Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/selenium/ Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/selenium/ Could not fetch URL https://pypi.org/simple/selenium/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/selenium/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping Could not find a version that satisfies the requirement selenium (from versions: ) No matching distribution found for selenium Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping
所以您测试飞越情况太晚了,还检查了火星的情况(可能是复制/粘贴错误)。
我建议您将日期和月份转换为数字,并在包含与月份相对应的索引处使用一个包含最大可能天/月名称的数组,如果只想近似,则应使用2月的特殊管理方式,否则使用可用的lib进行检查。
当您打印错误消息时,请用换行符刷新它,否则将下一张打印折叠到该消息上。
您是否要再次运行的方法也很复杂,并且如果通过 getline 输入的最后一行不为空,为什么要中止退出?