您好,我正在为学校做作业,但遇到了一些问题,我是 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.00 美元。编写一个计算并打印停车费的程序。程序的输入是汽车进入停车场的日期和时间,以及同一辆车离开停车场的日期和时间。两个输入的格式为
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;
}