Input Validation Error Using Arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Input Validation Error Using Arrays

  1. #1
    Join Date
    Jun 2013
    Posts
    22

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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

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

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    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.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Jun 2013
    Posts
    22

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by Philip Nicoletti View Post
    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.

  6. #6
    Join Date
    Jun 2013
    Posts
    22

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by Peter_B View Post
    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.

  7. #7
    Join Date
    Jun 2013
    Posts
    22

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by 2kaud View Post
    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by veryNew View Post
    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

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by veryNew View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by 2kaud View Post
    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.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by GCDEF View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Jan 2009
    Posts
    596

    Re: Input Validation Error Using Arrays

    Quote Originally Posted by veryNew View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center