July 18th, 2013, 10:39 PM
Input Validation Error Using Arrays
I have not completed this program yet, but I have already run into a snag. For the Input Validation part, every time you enter a number regardless if it is positive it will still display the cout statement: "Please enter positive values". The program runs correctly where I currently am at, but it just keeps reading that statement even if a user enters a positive value. I have run the debugger, but it really is not showing me anything other than I notice it jumping to that statement after every value that is entered.
Here is my code, once again I have not finished this yet, but I would really like to get this fixed first before I continue. I will keep debugging in the meantime...
// A local zoo wants to keep track of how many pounds of food each of its three monkeys
// eats each day during a typical week. Write a program that stores this information in a
// two-dimensional 3 X 7 array, where each row represents a different monkey and each column
// represents a different day of the week. The program should first have the user input the
// data for each monkey. Then it should create a report that includes the following information:
// Average amount of food eaten per day by the whole family of monkeys.
// The least amount of food eaten during the week by any one monkey.
// The greatest amount of food eaten during the week by any one monkey.
// Input validation: Do not accept negative numbers for pounds of food eaten.
using namespace std;
double totalAverage = 0; // variable to store the total average, initalized to zero
int monkey = 0, // index counter, intialized to zero
day = 0; // index counter, intialized to zero
const int NUM_MONKEYS = 3; // Elements or number of rows in array
const int NUM_DAYS = 7; // Elements or number of columns in array
double average [NUM_MONKEYS][NUM_DAYS], // 2D array with a 3 x 7 (rows and columns)
highest = 0, // variable to store the highest value for pounds eaten in a week
lowest, // variable to store the lowest value for pounds eaten in a week
monkeys[NUM_MONKEYS]; // varialbe to store the total amount eaten by each monkey
memset(&monkeys, 0, sizeof(monkeys));
memset(&average, 0, sizeof(average));
for(monkey = 0; monkey < NUM_MONKEYS; monkey ++)
for(day = 0; day < NUM_DAYS; day ++)
cout << "Enter the number of pounds eaten for"
<< " Monkey # " << (monkey + 1) << ", "
<< "Day " << (day + 1) << ": ";
cin >> average[monkey][day];
if(average[monkey][day] < 0);
// Ensure that negative numbers are not accepted
// This keeps displaying regardless if the number is positive
cout << "Please enter positive values.\n";
while(average[monkey][day] < 0);
cout << endl;
Last edited by veryNew; July 18th, 2013 at 10:47 PM.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.