【发布时间】:2012-04-06 05:09:45
【问题描述】:
我只在 C++ 上工作了大约一个月。我不太了解它是如何工作的,但是我需要为学校编写一个程序。我使用了一个 void 函数,到目前为止它似乎正在工作,但我不知道下一步该做什么我在第 44 行迷路了,我不知道如何让它工作,有没有办法从某个字符串?如果值在两个字符串中,我将如何确定哪个值?这是我的任务:
停车库收取 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;
stringstream ss;
string enter_date;
string enter_time;
string exit_date;
string exit_time;
int calculatecharge;
int num;
int i;
int year;
int month;
int ddmmyyChar;
int dayStr;
string line;
int x;
void atk()
{
getline (cin,line); // This is the line entered by the user
stringstream ss1(line); // Use stringstream to interpret that line
ss >> enter_date >> enter_time;
stringstream ss2(enter_date); // Use stringstream to interpret date
string year, month, day;
getline (ss2, year, '/');
}
int main()
{
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;
atk();
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;
atk();
if (hr - hr < 3)
cout<<"Parking fee due: $2.00" << endl;
【问题讨论】:
-
您包含的某些头文件不是必需的。例如,如果您不执行文件 I/O,则不需要
#include <fstream>。
标签: c++ parsing stringstream getline