954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Validating User Input for Arrays - Not Sort

Hi ~ My program seems to work perfectly with the exception of validating user input. I do not want someone to do the work for me, I don't learn anything that way. I need to know why it works not just what or how. I just need a push in the right direction. I was going to post the whole program but thought better of it and am posting just the part not working.

Thanks for any help.

double getRainfallPerMonth(double rain[], int size)
{
	int index = 0;
	
		while(index >= 0)
		{
			for(int index = 0; index <= size - 1; index++)
			{
				cout << "Enter the rainfall for month " << (index+1) << ": ";
				cin >> rain[index];

				if(rain[index] < 0)
				{
					cout << "\nInvalid Entry. Please enter a postive number for monthly rainfall.\n\n";
					cout << "Enter the rainfall for month 1: " << endl;
				}

			}

			
			return index;

		}



	}
scarlettmoon
Light Poster
28 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

1. you don't need that while loop that starts on line 5 so just delete it2.

2. The for loop sould cout from 0 to < size, not <= size-1. If the value of size is 5, then you want it to count 0, 1, 2, 3 and 4.

3. Use a do loop to verify the value of rainfall >= 0, something like this
do {
ask question
input value of rainfall
} while( rainfall < 0);

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

1. you don't need that while loop that starts on line 5 so just delete it2.

2. The for loop sould cout from 0 to < size, not <= size-1. If the value of size is 5, then you want it to count 0, 1, 2, 3 and 4.

3. Use a do loop to verify the value of rainfall >= 0, something like this do { ask question input value of rainfall } while( rainfall < 0);

Thanks - it works great now but still adds month one into the accumulated total which is a separate function. I'll try to take it from here.

scarlettmoon
Light Poster
28 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: