Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,587 people online right now. Registration is fast and FREE... Join Now!




Silly undeclared variable problem

 
Reply to this topicStart new topic

Silly undeclared variable problem, I feel like an idiot

greatdragon
21 Apr, 2008 - 04:49 AM
Post #1

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 22


My Contributions
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.

CODE

#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.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 05:11 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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:
cpp
        if(cm < c && last != 6){
int left = pourTwo(b,bm,c,cm);
pour(depth+1,4,am,left,bm+cm-left);
}

to this:
cpp
        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 smile.gif

This post has been edited by gabehabe: 21 Apr, 2008 - 05:13 AM
User is offlineProfile CardPM
+Quote Post

greatdragon
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 07:39 AM
Post #3

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 22


My Contributions
QUOTE(gabehabe @ 21 Apr, 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:
cpp
        if(cm < c && last != 6){
int left = pourTwo(b,bm,c,cm);
pour(depth+1,4,am,left,bm+cm-left);
}

to this:
cpp
        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 smile.gif


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
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 07:55 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

greatdragon
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 08:49 AM
Post #5

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 22


My Contributions
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 Apr, 2008 - 08:50 AM
User is offlineProfile CardPM
+Quote Post

Whizzy
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 09:19 AM
Post #6

Unregistered





Just being a dummy an all...

Where is pourTwo declared?


This post has been edited by Whizzy: 21 Apr, 2008 - 09:23 AM
+Quote Post

greatdragon
RE: Silly Undeclared Variable Problem
21 Apr, 2008 - 10:02 AM
Post #7

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 22


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month