Hello, I've created something, its quite large, however I decided to write it without using oop, now I believe I see why I should just use oop.

Okay, so the problem is with my bool values
specifically:

button_pickup2 = false;
button_pickup1 =  true;

For some reason when the ReflexAgent function returns the values back to the main function, it resets their values to false, false. Even if they were set to 0, 1.
Furthermore, I believe I have another similiar problem with the Fill_Item values, those are supposed to be keeping a some tally of everything, this always sums up to 0 however.
I'm really stuck on my code, its quite sloppy, I know how to program oop, I'll probably just use that in the future and avoid these problems. Thanks for any help, i've included the code below and tried to add comments of where the program is going, along with some test files to use.
thankyou.

I used g++ to compile the program, and the program also compiles in dev c++.

from unix command line i used: a.out belt1 belt2
...that should get it running.

///I just throw this into one big main function.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
//int Fill_Item1, Fill_Item2;
int job = 9;
bool Fill_Item1_Button, Fill_Item2_Button;

string ReflexAgentWithState(int belta_current, int belta_next, int beltb_current, int beltb_next, int power, bool button_pickup1, bool button_pickup2, int Fill_Item1, int Fill_Item2)
{
       
       string think;
       
       if (Fill_Item1 > Fill_Item2)
       {
        Fill_Item1_Button = true;   
        Fill_Item2_Button = false;         
       }
       else 
       {
        Fill_Item2_Button = true; 
        Fill_Item1_Button = false;    
       }
       cout << "belta_current: " << belta_current <<
	 " beltb_current: " << beltb_current<<endl;
if (belta_current != -1 || beltb_current != 1)
  { cout << "888888";
    /////////////////////////////////// belt a current bigger than belt b
    if (button_pickup1 == true)
    {
      think = "pickup belta";      
    
    }
    else if (button_pickup2 == true)
    {
      think = "pickup beltb";  
      
    }
    else if (belta_current > beltb_current)
    { 
        ////WILL JUMP ALL THE WAY DOWN HERE (3) 
        ///THESE BUTTONS WILL BE SET!
           //this sets belt1 to pick up the current item from.
           button_pickup2 = false;
           button_pickup1 =  true;
           
       if (!Fill_Item1_Button)
       {
///(4) SHOULD BE IN HERE AND RETURN "drop fill item1", however when we leave this function, the button_pickup2 and 1 are set back to false! I want them to stay as they were set in this function, help???
                 if (belta_current > Fill_Item1)
                 //think = dump/pickup/fill //FillItem1 = belta_current  
                 {think = "drop fill item1";  }   
       }
       else if (!Fill_Item2_Button)
       {
            if (belta_current > Fill_Item2)
               //think = dump/pickup/fill //FillItem2 = belta_current 
                {think = "drop fill item2";  }
       }
       else 
       {

            //belt a current isn't bigger than the fill items.
       }
    }//end else if belta_current
	/////////////////////////////////////
	
	//////////////////////////////////// belt b current bigger than belta
     else if (belta_current < beltb_current)
     {
       
         //this sets belt2 to pick up the current item from.
           button_pickup2 = true;
           button_pickup1 =  false;
       if (!Fill_Item1_Button)
       {
                 if (beltb_current > Fill_Item1)
                 //think = dump/pickup/fill //FillItem1 = beltb_current       
                { think = "drop fill item1";}
       }
       else if (!Fill_Item2_Button)
       {
            if (beltb_current > Fill_Item2)
                 //think = dump/pickup/fill //FillItem2 = beltb_current    
                { think = "drop fill item2";}
       }
       else 
       {
            //belt b current isn't bigger than the fill items.
       }
     } //end else if belta_current
     cout << "BANANA: " << button_pickup2 << " " << button_pickup1; 
	return think;
 }
 //if both belts == -1 then stop the belt
 think = "stop belt";
 cout << "plus";
 return think;
 
}

int main( int argc, char *argv[] )
{
   bool button_pickup1, button_pickup2;
  ifstream belta, beltb;
  int belta_current,belta_next, beltb_current, beltb_next, power =20, Fill_Item1=0, Fill_Item2=0;
  string action;
  cout << argv[1] << endl; //reads the first number from both belts, and the next number from both belts.
  cout << argv[2] << endl;
  belta.open(argv[1], ios::in);
  beltb.open(argv[2], ios::in);
  
  //call to the agent to pick a belt
  belta >> belta_next;
  beltb >> beltb_next;
  
  //  action = ReflexAgentWithState(belta_current, belta_next, beltb_current, beltb_next, power, button_pickup1, button_pickup2, Fill_Item1, Fill_Item2 );
  while(power > 0)
  {
////////STARTS HERE (1)
    //to generate a random belt to advance.
    int rand_num = rand() % 2 + 1;          
    
    if (rand_num == 1)
    {
    belta_current = belta_next; //contains current and next from belt1
    belta >> belta_next;
    }
    else
    {
    beltb_current = beltb_next; //contains current and next from belt2
    beltb >> beltb_next;
    }
    //why?  cout<<"INPUT PERCEPTION: "<<belta_current<<" "<<belta_next<<endl;
    
//////HERE IS THE FUNCTION CALL (2)
action = ReflexAgentWithState(belta_current, belta_next, beltb_current, beltb_next, power, button_pickup1, button_pickup2, Fill_Item1, Fill_Item2 );
	//my added code
	if (action == "pick up belta")
	{
	//necessary in order to output the value before pickup.
		cout << "OUTPUT ACTION: "<< action <<endl;
		Fill_Item1 = belta_current;
		belta_current = 0;
		power = (power - 1);
		cout << "\nINPUT PERCEPTION (power-left): " << power << endl;
          cout << "AGENT STATE SUM: " << (Fill_Item1 + Fill_Item2) << "\n belta current: " << belta_current
               << " belta next: " << belta_next << "\n beltb current: " << beltb_current << " beltb next: " << beltb_next <<endl;
			 cout << "\nOUTPUT ACTION: " << action<<endl;
	}
	else if (action == "pick up beltb")
	{
	//necessary in order to output the value before pickup.
		cout << "OUTPUT ACTION: "<< action <<endl;
		Fill_Item2 = beltb_current;
		beltb_current = 0;
		power = (power - 1);
		cout << "\nINPUT PERCEPTION (power-left): " << power << endl;
          cout << "AGENT STATE SUM: " << (Fill_Item1 + Fill_Item2) << "\n belta current: " << belta_current
               << " belta next: " << belta_next << "\n beltb current: " << beltb_current << " beltb next: " << beltb_next <<endl;
		cout << "\nOUTPUT ACTION: " << action<<endl;
	}
	else if (action == "drop fill item1")
	{
/// (5) So we come here with default values for button_pickups, dunno why. please help, then after this we just loop in the while loop until the power runs out, which is a descending counter.
          Fill_Item1 = 0;
          power = (power - 1);
          cout << "\nINPUT PERCEPTION (power-left): " << power << endl;
          cout << "AGENT STATE SUM: " << (Fill_Item1 + Fill_Item2) << "\n belta current: " << belta_current
               << " belta next: " << belta_next << "\n beltb current: " << beltb_current << " beltb next: " << beltb_next <<endl;
          cout << "\nOUTPUT ACTION: " << action<<endl;
          action = ReflexAgentWithState(belta_current, belta_next, beltb_current, beltb_next, power, button_pickup1, button_pickup2, Fill_Item1, Fill_Item2);
          
    }
    else if (action == "drop fill item2")
	{
          Fill_Item2 = 0;
          power = (power - 1);
          cout << "\nINPUT PERCEPTION (power-left): " << power << endl;
          cout << "AGENT STATE SUM: " << (Fill_Item1 + Fill_Item2) << "\n belta current: " << belta_current
               << " belta next: " << belta_next << "\n beltb current: " << beltb_current << " beltb next: " << beltb_next <<endl;
          cout << "\nOUTPUT ACTION: " << action<<endl;
          action = ReflexAgentWithState(belta_current, belta_next, beltb_current, beltb_next, power, button_pickup1, button_pickup2, Fill_Item1, Fill_Item2);
          
    }
    
    cout<<"OUTPUT ACTION: "<<action<<endl;
  }
  belta.close();
  beltb.close();
  return 0;
}

//here are the test cases

belt1

5
3
9
8
10
4
16
4
5
6
-1
-1

belt2

4
17
4
5
2
-1
-1
This article has been dead for over six months. Start a new discussion instead.