必须有类/结构/联盟
问题描述:
你好我正在为学校的任务,但我遇到了一些问题,我是一个初学者在C + +中,并不真正熟悉它。我非常感谢迄今为止获得的帮助,但我仍然在第31-35行发现错误。必须有类/结构/联盟
error C2228: left of '.substr' must have class/struct/union
error C2228: left of '.c_str' must have class/struct/union
这是我的任务 一个停车库收费$ 2.00最低收费停车长达三个小时。车库每小时或每小时额外收费0.50美元,超过三小时。任何给定的24小时期限的最高收费为 为10.00美元。将车停放超过24小时的人每天将支付8美元。 编写计算并打印停车费用的程序。您的程序输入是汽车进入停车库的日期和时间,以及同一辆汽车离开停车场的日期和时间。两个输入是在 YY/MM/DD hh:mm
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;
int dateStr;
int parseDate(std::string dateStr);
int main()
{
int enter_date;
int enter_time;
int exit_date;
int exit_time;
cout << "Please enter the date and time the car is entering "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
cin >> enter_date >> enter_time;
cout<< "Please enter the date and time the car is exiting "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
cin >> exit_date >> exit_time;
{
// Format: YY/MM/DD hh:mm
int year = atoi(dateStr.substr(0, 2).c_str());
int month = atoi(dateStr.substr(3, 2).c_str());
int day = atoi(dateStr.substr(6, 2).c_str());
int hour = atoi(dateStr.substr(9, 2).c_str());
int min = atoi(dateStr.substr(12, 2).c_str());
// Now calculate no. of mins and return this
int totalMins = 0;
totalMins += (year * 365 * 24 * 60); // Warning: may not be accurate enough
totalMins += (month * 30 * 24 * 60); // in terms of leap years and the fact
totalMins += (day * 24 * 60); // that some months have 31 days
totalMins += (hour * 60);
totalMins += (min);
return totalMins;
}
return 0;
}
答
您声明dateStr
为int
格式。它应该是一个字符串?你也应该初始化它。
我真的不知道......我试着将它改为一个字符串,但它似乎没有帮助。也许我做错了?另外我以为我通过将int放在它前面来初始化它,这是否是错误的? – user1311854 2012-04-07 05:31:16
听起来像你需要回到基础。尝试制作一个更小的程序,然后建立你的任务。 – 2012-04-07 05:47:43
我一直在做这个不到一个月,完全认为这是“基础知识”....所以我改变它像你说的一个字符串,但是我现在得到这个错误...这个应用程序已经要求运行时以不寻常的方式终止它。请联系应用程序支持团队以获取更多信息。 – user1311854 2012-04-07 06:01:48