Hey guys working on an assignment called monkey business and here is my code im getting an error on these two things.
Error:

cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}
cout << "Monkey number "  << (monkey + 1)
        << " ate the most amount of food,\n"
        << mostFood << " pounds, on day "
        << (mostDay + 1) << endl;
}
// Chapter 7, Programming Challenge 4
// Monkey Business
#include<iostream>
#include <iomanip>
using namespace std;


const int MONKEYS = 3;
const int DAYS = 7;


void getFoodEaten(double [][DAYS]);
void displayAverageDaily(double [][DAYS]);
void displayLeastEaten(double [][DAYS]);
void displayMostEaten(double [][DAYS]);

int main()
{

   double food[MONKEYS][DAYS];


   getFoodEaten(food);
   

   displayAverageDaily(food);


   displayLeastEaten(food);


   displayMostEaten(food);

   return 0;
}



void getFoodEaten(double food[][DAYS])
{
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {

         cout << "Enter the pounds eaten by monkey "
              << "number " << (monkey + 1)
              << "\non day " << (day + 1) << ": ";
         cin >> food[monkey][day];
         

         while (food[monkey][day] < 0)
         {
            cout << "Enter a non-negative amount: ";
            cin >> food[monkey][day];
         }
      }
   }
}



void displayAverageDaily(double food[][DAYS])
{
   double total;    
   double average;  
   
   for (int day = 0; day < DAYS; day++)
   {
     
      total = 0.0;
      
      
      for (int monkey = 0; monkey < MONKEYS; monkey++)
      {
         total += food[monkey][day];
      }
      
      
      average = total / MONKEYS;
      
      
      cout << "The average amount eaten on day "
           << (day + 1) << " is " << average
           << " pounds.\n";
   }
}



void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
   
 
   cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}



void displayMostEaten(double food[][DAYS])
{
   double mostFood; 
   int mostMonkey;  
   int mostDay;     
   

   mostMonkey = 0;
   mostDay = 0;
   mostFood = food[mostMonkey][mostDay];
   
  
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] > mostFood)
         {
            mostFood = food[monkey][day];
            mostDay = day;
            mostMonkey = monkey;
         }
      }
   }
   
  
   cout << "Monkey number "  << (monkey + 1)
        << " ate the most amount of food,\n"
        << mostFood << " pounds, on day "
        << (mostDay + 1) << endl;
}

Recommended Answers

All 16 Replies

variable named monkey has not been declared or is not in current scope.

How would I fix it so it is declared?

Move the declaration of monkey to the beginning of the function to make its scope function-wide. Then do not declare it in a loop

void foo()
{
   int monkey = 0;
   for(monkey = 0; // blabla)

}

Im trying that and its not working its making a error show up with my int monstMonkey now

What is the error. Please be specific.

you did something wrong, but since you didn't post any code I have no idea what you did. The same change has to be made to two different functions.

Where exactly do i need to put that code you mentioned at?

In the two functions that contain the errors. I posted an example of what needs to be changed.

Like this? Sorry I'm new and just confused why this isnt working.

// Chapter 7, Programming Challenge 4
// Monkey Business
#include<iostream>
#include <iomanip>
using namespace std;


const int MONKEYS = 3;
const int DAYS = 7;

void getFoodEaten(double [][DAYS]);
void displayAverageDaily(double [][DAYS]);
void displayLeastEaten(double [][DAYS]);
void displayMostEaten(double [][DAYS]);

int main()
{

   double food[MONKEYS][DAYS];


   getFoodEaten(food);
   

   displayAverageDaily(food);


   displayLeastEaten(food);


   displayMostEaten(food);

   return 0;
}



void getFoodEaten(double food[][DAYS])
{
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {

         cout << "Enter the pounds eaten by monkey "
              << "number " << (monkey + 1)
              << "\non day " << (day + 1) << ": ";
         cin >> food[monkey][day];
         

         while (food[monkey][day] < 0)
         {
            cout << "Enter a non-negative amount: ";
            cin >> food[monkey][day];
         }
      }
   }
}



void displayAverageDaily(double food[][DAYS])
{
   double total;    
   double average;  
   
   for (int day = 0; day < DAYS; day++)
   {
     
      total = 0.0;
      
      
      for (int monkey = 0; monkey < MONKEYS; monkey++)
      {
         total += food[monkey][day];
      }
      
      
      average = total / MONKEYS;
      
      
      cout << "The average amount eaten on day "
           << (day + 1) << " is " << average
           << " pounds.\n";
   }
}



void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
   
 void foo()
{
   int monkey = 0;
   for(monkey = 0; // blabla)
 }

   cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}



void displayMostEaten(double food[][DAYS])
{
	
	
   double mostFood; 
   int mostMonkey;  
   int mostDay;     
   

   mostMonkey = 0;
   mostDay = 0;
   mostFood = food[mostMonkey][mostDay];
   
  
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] > mostFood)
         {
            mostFood = food[monkey][day];
            mostDay = day;
            mostMonkey = monkey;
         }
      }
   }
   
  void foo()
{
   int monkey = 0;
   for(monkey = 0; // blabla)

}
   cout << "Monkey number "  << (monkey + 1)
        << " ate the most amount of food,\n"
        << mostFood << " pounds, on day "
        << (mostDay + 1) << endl;
}

delete lines 114-124 -- foo() was only the name of a fictiuous function, not one that you would actually use.

This is an example of what you want

void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
   int monkey = 0;  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
   
 
   cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}

That gets rid of the compile error by expanding the scope of monkey but that doesn't solve the real problem, which is that the OP should not have been using monkey on lines 116 and 150 in his original post in the first place. monkey is being used as a loop COUNTER variable in the code. It does NOT signify any meaningful array index. The value of monkey will be the same no matter what the array contents are, hence it should not be used in the printout.

Look for variables that have been declared and calculated in the function, but which are unused in the cout statements.

Its actually running the program now but when I run it and get to Monkey 3 Day 7 I get Debug Error

Program etc etc etc
runtime check failure #3 the variable "total" is being used without being intialized

// Chapter 7, Programming Challenge 4
// Monkey Business
#include<iostream>
#include <iomanip>
using namespace std;


const int MONKEYS = 3;
const int DAYS = 7;


void getFoodEaten(double [][DAYS]);
void displayAverageDaily(double [][DAYS]);
void displayLeastEaten(double [][DAYS]);
void displayMostEaten(double [][DAYS]);

int main()
{

   double food[MONKEYS][DAYS];


   getFoodEaten(food);
   

   displayAverageDaily(food);


   displayLeastEaten(food);


   displayMostEaten(food);

   return 0;
}



void getFoodEaten(double food[][DAYS])
{
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {

         cout << "Enter the pounds eaten by monkey "
              << "number " << (monkey + 1)
              << "\non day " << (day + 1) << ": ";
         cin >> food[monkey][day];
         

         while (food[monkey][day] < 0)
         {
            cout << "Enter a non-negative amount: ";
            cin >> food[monkey][day];
         }
      }
   }
}



void displayAverageDaily(double food[][DAYS])
{
   double total;    
   double average;  
   
   for (int day = 0; day < DAYS; day++)
   {
     
      total = 0.0;
      
      
      for (int monkey = 0; monkey < MONKEYS; monkey++)
      {
         total += food[monkey][day];
      }
      
      
      average = total / MONKEYS;
      
      
      cout << "The average amount eaten on day "
           << (day + 1) << " is " << average
           << " pounds.\n";
   }
}



void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
 void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
   int monkey = 0;  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
   
 
   cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}
{
   double mostFood; 
   int mostMonkey;  
   int mostDay;     
   

   mostMonkey = 0;
   mostDay = 0;
   mostFood = food[mostMonkey][mostDay];
   
  
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] > mostFood)
         {
            mostFood = food[monkey][day];
            mostDay = day;
            mostMonkey = monkey;
         }
      }
   }
   
  
   cout << "Monkey number "  << (monkey + 1)
        << " ate the most amount of food,\n"
        << mostFood << " pounds, on day "
        << (mostDay + 1) << endl;
}

Line 114 - Looks like the displayLeastEaten function is inside of ANOTHER displayLeastEaten function.

Okay I got it working but only thing that isnt working is the monkey counter since there is more then one monkey

// Chapter 7, Programming Challenge 4
// Monkey Business
#include<iostream>
#include <iomanip>
using namespace std;


const int MONKEYS = 3;
const int DAYS = 7;


void getFoodEaten(double [][DAYS]);
void displayAverageDaily(double [][DAYS]);
void displayLeastEaten(double [][DAYS]);
void displayMostEaten(double [][DAYS]);

int main()
{

   double food[MONKEYS][DAYS];


   getFoodEaten(food);
   

   displayAverageDaily(food);


   displayLeastEaten(food);


   displayMostEaten(food);

   return 0;
}



void getFoodEaten(double food[][DAYS])
{
   for (int monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {

         cout << "Enter the pounds eaten by monkey "
              << "number " << (monkey + 1)
              << "\non day " << (day + 1) << ": ";
         cin >> food[monkey][day];
         

         while (food[monkey][day] < 0)
         {
            cout << "Enter a non-negative amount: ";
            cin >> food[monkey][day];
         }
      }
   }
}



void displayAverageDaily(double food[][DAYS])
{
   double total;    
   double average;  
   
   for (int day = 0; day < DAYS; day++)
   {
     
      total = 0.0;
      
      
      for (int monkey = 0; monkey < MONKEYS; monkey++)
      {
         total += food[monkey][day];
      }
      
      
      average = total / MONKEYS;
      
      
      cout << "The average amount eaten on day "
           << (day + 1) << " is " << average
           << " pounds.\n";
   }
}



void displayLeastEaten(double food[][DAYS])
{
   double leastFood; 
   int leastMonkey;  
   int leastDay;     
	int monkey = 0;  
   leastMonkey = 0;
   leastDay = 0;
   leastFood = food[leastMonkey][leastDay];
   

   for (monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] < leastFood)
         {
            leastFood = food[monkey][day];
            leastDay = day;
            leastMonkey = monkey;
         }
      }
   }
   
 
   cout << "Monkey number "  << (monkey + 1)
        << " ate the least amount of food,\n"
        << leastFood << " pounds, on day "
        << (leastDay + 1) << endl;
}



void displayMostEaten(double food[][DAYS])
{
   double mostFood; 
   int mostMonkey;  
   int mostDay;     
   int monkey = 0; 

   mostMonkey = 0;
   mostDay = 0;
   mostFood = food[mostMonkey][mostDay];
   
  
   for (monkey = 0; monkey < MONKEYS; monkey++)
   {
      for (int day = 0; day < DAYS; day++)
      {
         if (food[monkey][day] > mostFood)
         {
            mostFood = food[monkey][day];
            mostDay = day;
            mostMonkey = monkey;
         }
      }
   }
   
  
   cout << "Monkey number "  << (monkey + 1)
        << " ate the most amount of food,\n"
        << mostFood << " pounds, on day "
        << (mostDay + 1) << endl;
}

Nevermind got it working thanks a bunch guys

The value for least monkeys and most monkeys is changed,when the functions is defined ! thats the error !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.