Help with time.

Dec 13, 2011 at 6:42pm
I need to know what I am doing wrong? I understand that theoretically it should not take you days to type in your name. But this is the simplest least amount of code I could use from mine with out changing a lot.

Please dont respond with anything to complex, I will not understand.

What I am trying to do is make a video store program, and right now I am trying to make a way to calculate last rental using time in the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;

int main ()
{
     string name;
     int which = 1;
     int lastRentTime[100];
     lastRentTime[which] = time(0);
     cout << "Please enter your name : ";
     cin >> name; 
     int difTime, daysEllapsed;
     difTime == (lastRentTime[1] - time(0));
     daysEllapsed == difTime % ( 86400 );
     cout << "That took you " << daysEllapsed << " days " << endl;
     system("pause");
     return 1;
}
Dec 13, 2011 at 6:49pm
Why are you using == rather than = on lines 17 and 18?
Dec 13, 2011 at 11:44pm
Because it was the way my teacher taught me.
Dec 14, 2011 at 12:07am
uhm...
Assignment( = )
Relational and equality( == )

You might want to read through this:
http://www.cplusplus.com/doc/tutorial/operators/

EDIT:
Here's a quote from that link:
Be careful! The operator = (one equal sign) is not the same as the operator == (two equal signs), the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality operator that compares whether both expressions in the two sides of it are equal to each other.
Last edited on Dec 14, 2011 at 12:08am
Dec 14, 2011 at 1:38am
Oh thanks. That would help a lot. But that still does not help my problem with time.
Dec 14, 2011 at 7:41pm
But it should help. In your code, the uninitialized variable diftime is being compared to the difference between the last rental time and now.Whereas with =, it would be set to the difference.

Etc.
Dec 14, 2011 at 8:15pm
Is your problem that you are always getting 0 or a negative number?

You are taking lastRentTime[1] - time(0). Time will always be higher than or equal to lastRentTime (depending on how fast you entered your name).

Another thought can be found after looking up the time function:
http://www.cplusplus.com/reference/clibrary/ctime/time/

Try:
1
2
3
4
5
6
7
8
9
     time_t lastRentTime[100] = { time(NULL) };

     cout << "Please enter your name : ";
     cin >> name; 

     int diffTime = time(NULL) - lastRentTime[1];
     int daysEllapsed = diffTime % ( 86400 );

    cout << "That took you " << daysEllapsed << " days " << endl;
Topic archived. No new replies allowed.