If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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

Code:

// 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.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
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], 0, sizeof(monkeys));
memset(&average[0], 0, sizeof(average));
for(monkey = 0; monkey < NUM_MONKEYS; monkey ++)
{
for(day = 0; day < NUM_DAYS; day ++)
{
do
{
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 positivecout << "Please enter positive values.\n";
}
while(average[monkey][day] < 0);
}
cout << endl;
}

When an array is defined, each element of the array can be initialised by specifying each element's value separated by a comma enclosed by {}. If there are more elements defined for the array than specified then these are set to 0.

All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

Re: Input Validation Error Using Arrays

Yeah, I was browsing a little and I found that. I had never used that keyword before, but I read about it and it seemed to be something that I would need to go along with the highest and lowest part that I hadn't finished yet. I just thought that the array might get a little screwy if I didn't initialize it to zero that way. I made a separate variable here:

Code:

monkeys[NUM_MONKEYS]

that would end up storing the total average for the pounds of food by each monkey, so that also when into my thinking once the array got filled up by the user (to make sure it was set to zero and not 3) where it was initialized before.

Re: Input Validation Error Using Arrays

The compiler didn't catch it because it is a perfectly legal c/c++ statement - but one that is unlikely to be what is required!

All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

Re: Input Validation Error Using Arrays

Originally Posted by veryNew

I checked that out with this program and it did catch it. Looks like I now have an alternate compiler I can go to, thank you.

FlexeLint isn't a compiler, it is a static code analysis tool. I.e. you can use it as a supplementary tool in conjunction with a compiler. Compilers nowadays are adding more and more warnings for code which, while legal, is a bit suspicious. And as GCDEF and 2kaud say, MSVC will warn about this statement. But static code analysis tools are specifically designed to flag up lots of possible problems.