We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media

# Old Style IO to New Style IO

Hi, I used to solve ACM problems, but I used old scanf & printf for input and output.But now i want to use C++ style IO. So, i picked a simple problem, and tried to convert its old style IO to new style IO.

The problem : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=996
(in summery, as input two numbers will be given, and you have to print the difference of that two numbers. :P )

Old Style: Online Judge Accepts this code.

``````#include<stdio.h>

int main()
{
long double a,b,d;

while(scanf("%Lf %Lf", &a,&b) == 2){

if (a > b) d = a - b;
else d = b - a;

printf("%.Lf\n", d);
};

return 0;
}``````

New Code:

``````#include<iostream>
using namespace std;

int main()
{
long double a,b,d;

while(!cin.eof()){
cin >> a >> b;

if (a > b) d = a - b;
else d = b - a;

cout << d << endl;
cout.flush();
};

return 0;
}``````

But, New C++ code is not being accepted, judge keeps on saying wrong answer.
What am i doing wrong?

3
Contributors
11
Replies
1 Year
Discussion Span
6 Months Ago
Last Updated
12
Views
Question
iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0

Due to how streams and the extraction operator work, you can use stream input directly in the condition of any conditional statement (such as an if or a while). Try eliminating the !cin.eof() (which tends to be a very buggy construct) as the condition and moving cin >> a >> b into the condition.

Also, the cout.flush() is not necessary. Use of std::endl takes care of it for you.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Skill Endorsements: 5

Due to how streams and the extraction operator work, you can use stream input directly in the condition of any conditional statement (such as an if or a while). Try eliminating the !cin.eof() (which tends to be a very buggy construct) as the condition and moving cin >> a >> b into the condition.

Also, the cout.flush() is not necessary. Use of std::endl takes care of it for you.

Did as you say,

``````#include<iostream>
using namespace std;

int main()
{
long double a,b,d;

while(cin >> a >> b){
if (a > b) d = a - b;
else d = b - a;

cout << d << endl;
};

return 0;
}``````

But still, wrong answer. :(

iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0

what did you enter for a and b, and what were the results?

Ancient Dragon
Achieved Level 70
Team Colleague
32,139 posts since Aug 2005
Reputation Points: 5,836
Skill Endorsements: 69

what did you enter for a and b, and what were the results?

You enter the 2 numbers and get the difference. Link to the problem.http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=996

Also in the first post, the old style IO used program is accepted by online judge, but I am trying to use new C++ style IO, but, after conversion, the problem is not being accepted, online judge is saying wrong answer.

iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0

I've been messing around with it as well. I've created (and submitted) several "New Style" versions that look like they behave identically to your "Old Style" version. I think it's just a quirk in the online-judge's evaluation algorithm(s).

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Skill Endorsements: 5

I've been messing around with it as well. I've created (and submitted) several "New Style" versions that look like they behave identically to your "Old Style" version. I think it's just a quirk in the online-judge's evaluation algorithm(s).

Where they accepted? Do I / should I need to stick with the old style?

Why Online Judges evaluation algorithm will mess with the internal things of a program? Isn't it there just to pump in input and testing output against the input?

iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0

None of the new versions were accepted, only the old version. They all ran well within the time limit but say they are "not able of solving the proposed problem".

It says to use Standard Input, and Standard Output, which cin and cout are supposed to map to; the program calculates and displays the correct difference(s); and the input terminates with an end of file.

I really don't know how it (the judge) works so I can't say what it does or why. It seems to me that there's something in the behavior of one of the streams that it doesn't like.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Skill Endorsements: 5

None of the new versions were accepted, only the old version. They all ran well within the time limit but say they are "not able of solving the proposed problem".

It says to use Standard Input, and Standard Output, which cin and cout are supposed to map to; the program calculates and displays the correct difference(s); and the input terminates with an end of file.

I really don't know how it (the judge) works so I can't say what it does or why. It seems to me that there's something in the behavior of one of the streams that it doesn't like.

:( then i really don't have much of a choice other then keep using the old C IO System.This feels really bad.

Do you have any suggesting other then ACM where i will be able to solve algorithm based problem? I can solve general adhoc problem fairly easily, but now i want to focus some advance topic.

iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0

I really don't do much of this type of thing, so I'm afraid I really don't have any suggestions for you. :(

I'm sure you would be able to find something on Google.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Skill Endorsements: 5

I really don't do much of this type of thing, so I'm afraid I really don't have any suggestions for you. :(

I'm sure you would be able to find something on Google.

Ok,I will.

Thanks for saving me from a lot pain. :D

iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10
Skill Endorsements: 0
Question Answered as of 2 Years Ago by Fbody and Ancient Dragon

Its old, but posting anyway so someone stumbling across might find this thread useful.

I overlooked some silly mistakes in the previous versions, but here is the code that works with new style io.

``````#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
long long int ally, enemy;

while(cin >> ally >> enemy)
cout << abs(ally-enemy) << endl;

return 0;
}
``````
iamcreasy
Junior Poster in Training
91 posts since Nov 2009
Reputation Points: 10