Very strange problem with a operator "-"

G

Gaijinco

I was trying to solve: acm.uva.es/p/v101/10191.html

I did this code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

bool digit(char c)
{
return isdigit(c);
}

struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};

// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}

int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah

appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda.h2*60+agenda.m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
}
return 0;
}

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;

It prints the correct negative number!

What's happening, here?!

Thanks.
 
P

Pete Becker

Gaijinco said:
My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

The value of 1080 - 17*60 + 45 is not the same as the value of 1080 -
(17*60 + 45).
 
B

benben

[snip]
My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

Well, if you are looking for the difference between the two times, why
don't you just subtract and take the absolute value?

diff(a, b) = |a-b| = |b-a| //math, not C++
What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;

It prints the correct negative number!

This time you did the subtraction right, but you forgot to take the
absolute value.
What's happening, here?!

Thanks.

Ben
 
J

Jim Langston

Gaijinco said:
I was trying to solve: acm.uva.es/p/v101/10191.html

I did this code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

bool digit(char c)
{
return isdigit(c);
}

struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};

// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}

int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah

appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':')+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda.h2*60+agenda.m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;


This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )

Is that what you wanted? If not, put parenthesis around your math to make
it evaluate in the correct order.
}
return 0;
}

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;

Now this is:

((agenda.back().h2 * 60 ) + agenda.back().m2 ) - 1080
 
S

Steve Pope

Jim Langston said:
This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )

Really? I thought + and - had the same precedence and
group left to right.

I could be confused though.

Steve
 
J

Jim Langston

Steve Pope said:
Really? I thought + and - had the same precedence and
group left to right.

I could be confused though.

Steve

Hmm.. true, I probably have that wrong. Which goes to prove the point, if
you want to be sure, use ()'s
 
P

Pete Becker

Jim said:
Hmm.. true, I probably have that wrong. Which goes to prove the point, if
you want to be sure, use ()'s

If you want to be sure, pay attention to the details. Stuffing in
parentheses is a sign of laziness.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top