必须有class / struct / union

时间:2012-04-07 04:58:32

标签: visual-c++

你好我正在为学校做作业,但是我遇到了一些问题,我是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美元的停车费可停放长达3小时。车库每小时额外收费0.50美元,或每小时超过3小时。最高收费 任何给定的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;
}

1 个答案:

答案 0 :(得分:2)

您将dateStr声明为int。它应该是一个字符串吗?您也应该初始化它。