I'm trying to read in two instances of date and time (from user input) into a string value (MM/DD/YY hh:mm).
I need to be able to calculate the difference between the two in order to return how much time has elapsed from the first date/time to the second date/time.
What would be the most efficient way to go about obtaining said results?
I was thinking I need to convert each year, month, day, hour, and minutes into its own integer variable, after researching immensely online I'm still not sure how to convert from a string of characters to an integer.
I just learned starting learning C++ couple months ago, I would appreciate any advice. I hope to learn.
thank you for replying but that's not what I would hope to do.
This is what I have so far:
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstring>
//void calculateParkingHours(string, string);
//void calculateFee(float, float);
using namespace std;
int main ()
{
int enterY, enterM, enterD, enterH, enterMi, exitY, exitM, exitD, exitH, exitMi;
string enteringDate, enteringTime;
cout << "Please enter the date and time the car is entering the parking garage" << '\n'
<< "in the following format: MM/DD/YY hh:mm" << endl;
cin >> enteringDate >> enteringTime;
enterY = atoi ( enteringDate.substr( 6, 2 ));
enterM = atoi ( enteringDate.substr( 0, 2 ));
enterD = atoi ( enteringDate.substr( 3, 2 ));
enterH = atoi ( enteringTime.substr( 9, 2 ));
enterMi = atoi ( enteringTime.substr( 12, 2 ));
//cout << "Please enter the date and time the car is exiting the parking garage in the following format:" << '\n'
// << "MM/DD/YY hh:mm" << endl;
}
I'm having problems with using "atoi" is there a more efficient way to convert different parts of a string to integers?
so if I extrapolated separate integers from a stream MM/DD/YY hh:mm what would be the exact syntax to use to convert that into a time format C++ can recognize and contrasts between two different times?
for example: if the user entered 03/26/12 20:08 I would have:
03 stored in a integer variable enterM
26 stored in a integer variable enterD
12 stored in a integer variable enterY
20 stored in a integer variable enterH
08 stored in a integer variable enterMin
so if I extrapolated separate integers from a stream MM/DD/YY hh:mm what would be the exact syntax to use to convert that into a time format C++ can recognize and contrasts between two different times?
Please write small test functions to familiarize yourself with how time functions work. That is how any programmer goes about learning how to use functions that are new to them. That is much easier than to have one of us spend time giving you "exact" syntax.
Bookmarks