compile error (compiled successfully using visual c++)

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 (compiled successfully using visual c++)

Post by marcadian »

I have compiled it using visual c++ but it return compile error and I don't know where's the error, please help me

Code: Select all

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>

using namespace std;
map<char,int> val;


int tobase10(string x,int base)
{
	int i=0,j=0,sum=0;

	for (i=x.length() ,j=1; i>=1 ; i--,j*=base)
	{
		sum += j * val[x[i-1]];
	
	}
	return sum;
}

bool valid(string x,int base)
{
	for (int i=0;i<x.length();i++)
	{
		//cout <<val[x[i]]<<" " <<base <<endl;
		if (val[x[i]] >= base) return false;
	}
	return true;
}

int main()
{
	string x,y;
	bool found;
	int angka;
	for (int i=0;i<=9;i++)
		val[i+48] = i;
	for (i=1;i<=26;i++)
		val[i+64] = i+9;

	while (cin >> x >> y)
	{
		found=false;
		for (i=1;i<=36;i++)
		{
			
			for (int k=i;k<=36;k++)
			{
				if (valid(x,i) && valid(y,k))
				{
					if (tobase10(x,i)==tobase10(y,k)) 
					{
						cout << x << " (base "<<i<<") = "<<y<<" (base "<<k<<")"<<endl;
						found=true;
						break;
					}
				}
				if (valid(x,k) && valid(y,i))
				{
					if (tobase10(x,k)==tobase10(y,i)) 
					{
						cout << x << " (base "<<k<<") = "<<y<<" (base "<<i<<")"<<endl;
						found=true;
						break;
					}
				}
			}
			if (found) break;
		}
		if  (!found) cout << x << " is not equal to " << y << " in any base 2..36" <<endl;
	}
	
	return 0;
}
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

Difference in variable scoping. VC++ is broken.

Code: Select all

...
int main()
{
   string x,y;
   bool found;
   int angka;
   for (int i=0;i<=9;i++)
      val[i+48] = i;
 /* i undeclared here */
   for (i=1;i<=26;i++)
      val[i+64] = i+9;

   while (cin >> x >> y)
   {
      found=false;
 /* i undeclared here */
      for (i=1;i<=36;i++) 
...
marcadian
New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

Post by marcadian »

yes I already find it, thank you. I have read the variable scoping like you tell me but when I compile with vc++ it compile error, I wonder why compiler like vc++ use language like that, it's different from many book
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Which version of visual C++ compiler are you using?

Older versions of Visual C++ will have this problem. However Visual C++ .NET will not have this problem.
Post Reply

Return to “C++”