compile error, why?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
marcadian
New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

compile error, why?

Post 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
sacer_carabus
New poster
Posts: 1
Joined: Sat Aug 05, 2006 6:18 pm
Contact:

Post 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);
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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
marcadian
New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

Post 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?
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

You can use sqrt(3.0).

For your second question, I don't get your question. Could you rephrase? Thanks.
marcadian
New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

Post 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
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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).
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...
Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post by Moha »

If you use cout.setf() or cout.precision() the effects are permanent.
marcadian
New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

Post 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??
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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).
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...
Post Reply

Return to “C++”