-
July 18th, 2013, 09:39 PM
#1
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 positive
cout << "Please enter positive values.\n";
}
while(average[monkey][day] < 0);
}
cout << endl;
}
Last edited by veryNew; July 18th, 2013 at 09:47 PM.
-
July 18th, 2013, 10:47 PM
#2
Re: Input Validation Error Using Arrays
You have the line:
Code:
if(average[monkey][day] < 0) ;
The semi-colon should not be there. Basically, it says: if the average is less
than 0, execute a noop (empty statement). The code then goes to the next
line (the cout statement).
-
July 19th, 2013, 05:31 AM
#3
Re: Input Validation Error Using Arrays
This is the sort of bug that programs like lint can help with. Using the online demo of FlexeLint at http://www.gimpel-online.com/OnlineTesting.html (choose the Simple Example (C++) and replace the code with yours), I got this warning (amonst other):
Code:
45 if(average[monkey][day] < 0);
simple.cpp 45 Info 721: Suspicious use of ;
Using a lint type program, and turning on all the warnings on your compiler, can be very helpful in preventing subtle bugs.
-
July 19th, 2013, 06:55 AM
#4
Re: Input Validation Error Using Arrays
You don't need these statements
Code:
memset(&monkeys[0], 0, sizeof(monkeys));
memset(&average[0], 0, sizeof(average));
You can do the initialisation to 0 as part of the definiton
Code:
double average[NUM_MONKEYS][NUM_DAYS] = {0.0},
highest = 0.0,
lowest = 0.0,
monkeys[NUM_MONKEYS] = {0.0};
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.
See
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.learncpp.com/cpp-tutorial/62-arrays-part-ii/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 19th, 2013, 09:41 PM
#5
Re: Input Validation Error Using Arrays
Originally Posted by Philip Nicoletti
You have the line:
Code:
if(average[monkey][day] < 0) ;
The semi-colon should not be there. Basically, it says: if the average is less
than 0, execute a noop (empty statement). The code then goes to the next
line (the cout statement).
Oh...I didn't catch that, and apparently my compiler didn't either. Thank you for pointing that out.
-
July 19th, 2013, 09:44 PM
#6
Re: Input Validation Error Using Arrays
Originally Posted by Peter_B
This is the sort of bug that programs like lint can help with. Using the online demo of FlexeLint at http://www.gimpel-online.com/OnlineTesting.html (choose the Simple Example (C++) and replace the code with yours), I got this warning (amonst other):
Code:
45 if(average[monkey][day] < 0);
simple.cpp 45 Info 721: Suspicious use of ;
Using a lint type program, and turning on all the warnings on your compiler, can be very helpful in preventing subtle bugs.
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.
-
July 19th, 2013, 09:54 PM
#7
Re: Input Validation Error Using Arrays
Originally Posted by 2kaud
You don't need these statements
Code:
memset(&monkeys[0], 0, sizeof(monkeys));
memset(&average[0], 0, sizeof(average));
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.
-
July 20th, 2013, 02:46 AM
#8
Re: Input Validation Error Using Arrays
Originally Posted by veryNew
Yeah, I was browsing a little and I found that. I had never used that keyword before,
memset is not a keyword. It is a function that is part of the standard runtime library.
Keywords in C++ are words such as for, while, switch, virtual, etc.
Regards,
Paul McKenzie
-
July 20th, 2013, 05:16 AM
#9
Re: Input Validation Error Using Arrays
Originally Posted by veryNew
Oh...I didn't catch that, and apparently my compiler didn't either. Thank you for pointing that out.
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 20th, 2013, 06:25 AM
#10
Re: Input Validation Error Using Arrays
Originally Posted by 2kaud
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!
I'm pretty sure the newer VCs will give a warning about an empty control statement and ask if that's your intent.
-
July 20th, 2013, 06:45 AM
#11
Re: Input Validation Error Using Arrays
Originally Posted by GCDEF
I'm pretty sure the newer VCs will give a warning about an empty control statement and ask if that's your intent.
With MSVC at warning level 3 and above gives
warning C4390: ';' : empty controlled statement found; is this the intent?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 20th, 2013, 10:53 AM
#12
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.
There is an excellent list of static code analysers on wikipedia at http://en.wikipedia.org/wiki/List_of..._code_analysis
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|