must have class/struct/union

I changed my code so it does not need getline, however I am now having errors with my functions lines 31-35 and I am not sure if it is okay to have line 11 where it is.
Here are the errors, each line has both errors.
error C2228: left of '.c_str' must have class/struct/union
error C2228: left of '.substr' must have class/struct/union

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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 );
Last edited on
You can do something like this:
1
2
3
4
5
6
7
8
9
10
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;
// The following should be in a function
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, '/');
...


I changed the code, but I do not know what to do next. I am still not sure how to even add the math to make the program run, or how to use stringstream with the math; I have searched everywhere and I cannot find anything similar. Is it possible to use an if else statement? If so how do I say which year, month, or day I am entering because I have the user input it two different times?
Last edited on
also stringstream is a function not a variable
I used a void function and it seems to be working so far..but I have no idea what to do next I am lost at line 44, not sure how to make it work, I want to take the hr from the exit time and subtract it from the hr from the enter time, but it is taking the same hr.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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;
  
Last edited on
changed the code above!!
error C2228: left of '.c_str' must have class/struct/union
error C2228: left of '.substr' must have class/struct/union
This is because on line 10 dateStr is int not string. You don't need line 10 if you place the code form line 29 on into the function parseDate.

you can do it like you did it above. It's another approach. If you like it more do it. On line 22/27 you need geline with std::string dateStr to get the whole for parsing.

for time calculation consider this http://www.cplusplus.com/reference/clibrary/ctime/
I do not really understand how to use getline so I am trying to do it with a string, I had line 29 cont. in the functin parseDate but I had an error for having a function within a function, is it possible to get around that somehow?
Last edited on
Topic archived. No new replies allowed.