By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Very strange problem with a operator "-"

P: n/a
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[i].h2*60+agenda[i].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.

Jun 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gaijinco wrote:

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).

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 18 '06 #2

P: n/a
[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
Jun 18 '06 #3

P: n/a

"Gaijinco" <ga******@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
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[i].h2*60+agenda[i].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

It prints the correct negative number!

What's happening, here?!

Thanks.

Jun 19 '06 #4

P: n/a
Jim Langston <ta*******@rocketmail.com> wrote:
"Gaijinco" <ga******@gmail.com> wrote in message

cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;


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
Jun 19 '06 #5

P: n/a
"Steve Pope" <sp*****@speedymail.org> wrote in message
news:e7**********@blue.rahul.net...
Jim Langston <ta*******@rocketmail.com> wrote:
"Gaijinco" <ga******@gmail.com> wrote in message

cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;


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


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

P: n/a
Jim Langston wrote:

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.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.