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?

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.

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. :(

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

commented: A good question +7

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.

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

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?

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.

commented: This makes perfect sense :) +2

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.

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.

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.