6 Replies - 1287 Views - Last Post: 21 April 2008 - 11:02 AM Rate Topic: -----

#1 greatdragon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Silly undeclared variable problem

Posted 21 April 2008 - 05:49 AM

I'm getting back to C++ after my constant language jumping, and now I'm having a strange problem that I haven't been able to solve by searching.

#include <iostream>
#include <fstream>

using namespace std;

int a,b,c;
bool results[21];

int left = 0;

void pour(int depth, int last, int am,int bm, int cm){
	if(depth >= 15) return;
	if(am == 0) results[cm] = true;

	if(am > 0){
		if(bm < b && last != 3){
			int left = pourTwo(a,am,b,bm);
			pour(depth+1,1,left,am+bm-left,cm);
		}
		if(cm < c && last != 5){
			int left = pourTwo(a,am,c,cm);
			pour(depth+1,2,left,bm,am+cm-left);
		}
	}
	if(bm > 0){
		if(am < a && last != 1){
			int left = pourTwo(b,bm,a,am);
			pour(depth+1,3,am+bm-left,left,cm);
		}
		if(cm < c && last != 6){
			int left = pourTwo(b,bm,c,cm);
			pour(depth+1,4,am,left,bm+cm-left);
		}
	}
	if(cm > 0){
		if(am < a && last != 2){
			int left = pourTwo(c,cm,a,am);
			pour(depth+1,5,am+cm-left,bm,left);
		}

		if(bm < b && last != 4){
			int left = pourTwo(c,cm,b,bm);
			pour(depth+1,6,am,bm+cm-left,left);
		}
	}
}


int main()
{

	ifstream fin("milk3.in");
	fin >> a >> b >> c;
	fin.close();

	ofstream fout("milk3.out");



	bool anyprinted = false;
	for(int i = 0; i < 21;i++){
		if(results[i] == 1){
			if(anyprinted) fout << ' ';
			else anyprinted = true;
			fout << i;
		}
	}
	fout << endl;


	fout.close();

	return 0;
}


The relevant piece being:

int left = 0;

int pourTwo(int capOne,int filledOne,int capTwo,int filledTwo){//returns remaining in source bucket
return max(0,filledOne - (capTwo - filledTwo));
}

void pour(int depth, int last, int am,int bm, int cm){
if(depth >= 15) return;
if(am == 0) results[cm] = true;

if(am > 0){
if(bm < b && last != 3){
int left = pourTwo(a,am,b,bm);
pour(depth+1,1,left,am+bm-left,cm);
}
...

The problem is, where I've highlighted "int", I'm forced to redeclare "left", which I've already declared as a global variable. If I try to just set the value without declaring it as an int, I get "error: `left' undeclared (first use this function)". How stupid am I being here, and what's going on? Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Silly undeclared variable problem

#2 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Silly undeclared variable problem

Posted 21 April 2008 - 06:11 AM

hmmm... the only error I get is this:

Quote

error: `pourTwo' was not declared in this scope


However, the problem might be this:
After you have declared int left as global, you are creating int left within your pour() function. Try changing all lines like this:
        if(cm < c && last != 6){
            int left = pourTwo(b,bm,c,cm);
            pour(depth+1,4,am,left,bm+cm-left);
        }

to this:
        if(cm < c && last != 6){
            left = pourTwo(b,bm,c,cm);
            pour(depth+1,4,am,left,bm+cm-left);
        }

so, instead of redeclaring, you're redefining
Hope this helps :)

This post has been edited by gabehabe: 21 April 2008 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 greatdragon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Silly undeclared variable problem

Posted 21 April 2008 - 08:39 AM

View Postgabehabe, on 21 Apr, 2008 - 06:11 AM, said:

hmmm... the only error I get is this:

Quote

error: `pourTwo' was not declared in this scope


However, the problem might be this:
After you have declared int left as global, you are creating int left within your pour() function. Try changing all lines like this:
        if(cm < c && last != 6){
            int left = pourTwo(b,bm,c,cm);
            pour(depth+1,4,am,left,bm+cm-left);
        }

to this:
        if(cm < c && last != 6){
            left = pourTwo(b,bm,c,cm);
            pour(depth+1,4,am,left,bm+cm-left);
        }

so, instead of redeclaring, you're redefining
Hope this helps :)


The code I posted is actually the working version, what mystifies me is that redefining instead of redeclaring doesn't work. 0_o (I'm using GCC) Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1440
  • View blog
  • Posts: 11,025
  • Joined: 06-February 08

Re: Silly undeclared variable problem

Posted 21 April 2008 - 08:55 AM

It could just be that it can't change because it already has something assigned to it.

Try changing the global int left = 0; to just int left;

Does that do anything?
Was This Post Helpful? 0
  • +
  • -

#5 greatdragon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Silly undeclared variable problem

Posted 21 April 2008 - 09:49 AM

I've tried pretty much every variant of declaration/redefinition, and they don't seem to work. Would there be any case where a method wouldn't "see" variables with a global scope?

Edit: I'm starting to think "left" might be some sort of semi-reserved word; if I change the name of the variable, it works perfectly.

This post has been edited by greatdragon: 21 April 2008 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Whizzy*


Reputation:

Re: Silly undeclared variable problem

Posted 21 April 2008 - 10:19 AM

Just being a dummy an all...

Where is pourTwo declared?

This post has been edited by Whizzy: 21 April 2008 - 10:23 AM

Was This Post Helpful? 0

#7 greatdragon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Silly undeclared variable problem

Posted 21 April 2008 - 11:02 AM

Oh, oops, I forgot to mention that I removed it from the pasted code to save space, so it's not the problem. I think the actual issue is the choice of the word "left", but Google didn't turn anything up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1