Page 1 of 1

Compiler Error

Posted: Sun Feb 06, 2005 3:04 am
by twhite5
When i submit the following code i get a compiler error.
It compiles fine using g++ one a True Unix 64 machine and on Visual C++. Any help you can give me would be appreciated, thanks

Code: Select all

#include<iostream>
#include<iomanip>
#include<string>
#include<math.h>

using namespace std;

int main()
{
  cout << showpoint << setprecision(4);
  pair<double,double> point[3],midpoint[2],center;
  double slope[2];
  double radius;
  double a,b,c;
  int index = 0, index2 = 0;
  string input;
  getline( cin, input, '\n' );
  do
  {
	index = 0; index2 = 0;
	for( int i = 0; i < 3; i++ )
	{
		while( input[index2] != ' ' )
			index2++;
		point[i].first = atof((input.substr(index,index2)).c_str());
		index = index2 + 1;
		index2++;
		while( input[index2] != ' ' && input.length() != index2 )
			index2++;
		point[i].second = atof((input.substr(index,index2)).c_str());
		index = index2 + 1;
		index2++;
	}
	if( (point[0].second - point[1].second) == 0 )
	{
		slope[0] = ((point[0].first - point[2].first)/(point[2].second - point[0].second));
		midpoint[0].first = (point[0].first + point[2].first)/2;
		midpoint[0].first = (point[0].second + point[2].second)/2;
		slope[1] = ((point[1].first - point[2].first)/(point[2].second - point[1].second));
		midpoint[1].first = (point[1].first + point[2].first)/2;
		midpoint[1].second = (point[1].second + point[2].second)/2;
	}
	else
	{
		slope[0] = ((point[0].first - point[1].first)/(point[1].second - point[0].second));
		midpoint[0].first = (point[0].first + point[1].first)/2;
		midpoint[0].second = (point[0].second + point[1].second)/2;
		if( (point[0].second - point[2].second) == 0 )
		{
		slope[1] = ((point[1].first - point[2].first)/(point[2].second - point[1].second));
		midpoint[1].first = (point[1].first + point[2].first)/2;
		midpoint[1].second = (point[1].second + point[2].second)/2;
		}
		else
		{
		slope[1] = ((point[0].first - point[2].first)/(point[2].second - point[0].second));
		midpoint[1].first = (point[0].first + point[2].first)/2;
		midpoint[1].second = (point[0].second + point[2].second)/2;
		}
	}
	center.first = (slope[0]*midpoint[0].first - midpoint[0].second - slope[1]*midpoint[1].first + midpoint[1].second)/(slope[0] - slope[1]);
	center.second = slope[0]*(center.first - midpoint[0].first) + midpoint[0].second;
	radius = sqrt( pow(center.first - point[0].first, 2 ) + pow(center.second - point[0].second, 2 ) );
	if( center.first < 0 )
	{
		if( center.second < 0 )
		{
			cout << "(x + " << -1 * center.first << ")^2 + (y + " << -1 * center.second << ")^2 = " << radius << "^2" << endl;
		}
		else
		{
			cout << "(x + " << -1 * center.first << ")^2 + (y - " << center.second << ")^2 = " << radius << "^2" << endl;
		}
	}
	else
	{
		if( center.second < 0 )
		{
			cout << "(x - " << center.first << ")^2 + (y + " << -1 * center.second << ")^2 = " << radius << "^2" << endl;
		}
		else
		{
			cout << "(x - " << center.first << ")^2 + (y - " << center.second << ")^2 = " << radius << "^2" << endl;
		}
	}
	a = 2*center.first;
	b = 2*center.second;
	c = pow(center.first, 2) + pow(center.second, 2) - pow( radius, 2 );
	cout << "x^2 + y^2 ";
	if( a < 0 )
	{
		if( b < 0 )
		{
			if( c < 0 )
				cout << "+ " << -1 * a << "x + " << -1 * b << "y - " << -1 * c << " = 0";
			else
				cout << "+ " << -1 * a << "x + " << -1 * b << "y + " << c << " = 0";
		}
		else
		{
			if( c < 0 )
				cout << "+ " << -1 * a << "x - " << b << "y - " << -1 * c << " = 0";
			else
				cout << "+ " << -1 * a << "x - " << b << "y + " << c << " = 0";
		}
	}
	else
	{
		if( b < 0 )
		{
			if( c < 0 )
				cout << "- " << a << "x + " << -1 * b << "y - " << -1 * c << " = 0";
			else
				cout << "- " << a << "x + " << -1 * b << "y + " << c << " = 0";
		}
		else
		{
			if( c < 0 )
				cout << "- " << a << "x - " << b << "y - " << -1 * c << " = 0";
			else
				cout << "- " << a << "x - " << b << "y + " << c << " = 0";
		}
	}
    cout << endl << endl;
  }while(getline(cin, input, '\n' ));
  return 0;
}


Posted: Sun Feb 06, 2005 3:30 am
by misof
Sadly, this is a problem with the (old? crippled? both?) compiler used at UVa. Ocassionally I have the same problem, I usually solve it using a "binary search" to find the problematic statement(s) -- as an example consider my number of submits on task A in the last Waterloo contest. When I found the problem with compiling, I was already too disgusted to fix the precision problems in my solution and thus didn't solve the problem :cry:

This time my problem was the following statement:
int x = round(60.0 * y);
On my system this only gives a warning, apparently this didn't even compile on UVa.

A more legitimate cause may be not #including a library many compilers link automatically, I had this problem once.

In your case, the atof() function is declared in stdlib.h
also, adding a #include<stdlib.h>
or even better #include<cstdlib>
should probably fix it.

Posted: Sun Feb 06, 2005 3:40 am
by twhite5
Thanks for you reply, i went ahead and included those libraries but unfortunately i am still receiving a compiler error. If you or anyonce else has anymore suggestions i am more than open to them. Thanks.

Posted: Sun Feb 06, 2005 3:49 am
by misof
One more thing that occured to me: Try replacing the 2 when you call pow() by 2.0, the header is pow(double,double) and if they enforce a very-very-strict type checking this may also cause problems.

If this doesn't help, you'll have to resort to the "binary search" method.

Either way, it's damn irritating that the judge doesn't return a proper compile error message, it would be MUCH simpler to fix these problems :cry: :cry: :cry: :cry: :cry:

Posted: Sun Feb 06, 2005 5:15 am
by Krzysztof Duleba
To use pair you should include utility header. You should also replace

Code: Select all

cout << showpoint << setprecision(4); 
with

Code: Select all

cout.setf(ios::showpoint);
cout.precision(4);
Funny enough, all the things that misof mentioned are not the issue here.

Posted: Sun Feb 06, 2005 5:22 am
by twhite5
Great, that fixed the problems thanks.