Page 1 of 1

compile error, why?

Posted: Sat Aug 05, 2006 4:45 pm
by marcadian

Code: Select all

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	long double r;
	
	while (cin >> r)
	{

		cout <<setprecision(3)<<fixed<<showpoint;
		cout <<r*r*(43/21.0 - sqrt(3))<<" ";
		cout <<4*r*r*( (sqrt(3)/2) - (31/42.0))<<" ";
		cout <<4*r*r*(10/21.0 - sqrt(3)/4) <<endl;

	}
	return 0;
}

and also this one

Code: Select all

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector <int> ans;
bool used[10]={false};

bool cek(int x)
{
        int i,j,k;
	int tab[10]={0},index[10]={0};
	char angka[10]=("");
	itoa(x,angka,10);
	for (i=0,j=0;i<strlen(angka);i++)
	{
		if (index[j]==1) return false;
			else index[j]=1;
		j=(j+angka[j] -48 )%strlen(angka);
	}

    for (i=0;i<strlen(angka);i++) if (index[i]==0) return false;
                

        if (j==0) return true;
	return false;
}

void generate(int level,int angka)
{
	if (cek(angka)) ans.insert (ans.end(),angka);	
	if (angka>987654321);
	else
	{
		for (int i=1;i<=9;i++)
		{
			if (used[i]) continue;
			used[i]=true;
			generate(level+1,angka*10+i);
			used[i]=false;
		}
	}
}

int main()
{
	int start,cas=0;

	for (int i=1;i<=9;i++)
	{
		used[i]=true;
		generate(1,i);
		used[i]=false;
	}
	vector <int>::const_iterator j;

	cin >> start;
	sort(ans.begin(),ans.end());
	while (start!=0)
	{
		cas++;
		for (j=ans.begin();j!=ans.end();j++)
		{
			if ((*j)>=start)
			{
				cout << "Case "<<cas <<": "<<*j <<endl;
				break;
			}
		}
		cin  >> start;
	}
	return 0;
}
please help me. I have compiled both of it on my machine using djgpp

Posted: Sat Aug 05, 2006 6:23 pm
by sacer_carabus
to first code - there is not sqrt(int). There is sqrt(double). I remember have some problems like this.
to second - there is not "itoa" function in standard c++. i use instead:
char damn_buf[30];
int temp = 666;
sprintf(buf, "%d", damn_buf);

Posted: Sun Aug 06, 2006 6:16 am
by chunyi81
For the first code, other than the problem spotted by the previous poster, this line of code also has a problem:

Code: Select all

cout <<setprecision(3)<<fixed<<showpoint; 
iomanip library is not well supported by the g++ compiler in the OJ.

Change to this instead:

Code: Select all

cout << setprecision(3);
cout.setf(ios::fixed | ios::showpoint);
By the way for the sqrt function there should be no problems with older versions of g++ compiler but will have problems with newer versions of g++ compiler

Posted: Sun Aug 06, 2006 4:30 pm
by marcadian
thx 4 ur reply. I also can use sqrt(3.0) right? is this

Code: Select all

cout << setprecision(3);
cout.setf(ios::fixed | ios::showpoint);
code permanently change until I change it again?

Posted: Sun Aug 06, 2006 5:08 pm
by chunyi81
You can use sqrt(3.0).

For your second question, I don't get your question. Could you rephrase? Thanks.

Posted: Tue Aug 08, 2006 4:35 pm
by marcadian
is that code change the cout format permanently (like cout <<setprecision(3)<<fixed<<showpoint; ) ??or I must change everytime I use cout (like setwidth) ? sorry for my bad english

Posted: Wed Aug 09, 2006 12:52 am
by Krzysztof Duleba
setf and precision methods do change cout behaviour for all subsequent writes (I don't know about setprecision, though, I don't use it).

Posted: Wed Aug 09, 2006 3:07 pm
by Moha
If you use cout.setf() or cout.precision() the effects are permanent.

Posted: Fri Aug 11, 2006 5:18 pm
by marcadian
Krzysztof Duleba wrote:setf and precision methods do change cout behaviour for all subsequent writes (I don't know about setprecision, though, I don't use it).

so how you output real if you need precision 3 digits for example in c++ with cout??

Posted: Sat Aug 12, 2006 2:07 am
by Krzysztof Duleba
I would go with cout.setf(ios::fixed) and cout.precision(3) if IO speed wasn't an issue (otherwise it would be printf("%.3lf", aDouble) or something from my asm library).