int findLowest(int arr[]) Is passed the five accident totals as an array of ints. It determines which is the smallest and returns the subscript for that array element.

The main function declares two arrays, a string array containing the names of the boroughs and an int array which will hold the accident numbers for each borough. The main() function calls the getNumAccidents() function to fill the int array. It then calls findLowest() to learn which borough had the fewest accidents. It should print out the accident numbers for each borough and then print out the borough and the accident number with the fewest accidents.

What is meant by "validates the input"? The number of accidents in a year cannot be negative, so only a value of 0 or greater is an acceptable input.

So far I have everything except for the code to validate the input, that's the only part still missing, and what I do have is all in working order... I've tried playing around with if/else, do/while, etc., can't figure it out. Code I have is as follows:

#include <iostream>
using namespace std;

int getNumAccidents (string);
int findLowest (int arr[]);

int main()
{
	string Boroughs[5] = {"Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island"};
	int numAccidents[5];
	int x;

	cout << "For each borough, input the number of automobile accidents for that borough for the year.\n" << endl;

	for (int i=0; i<5; i++)
	{
		numAccidents[i] = 0;
		cout << Boroughs[i] << "\t";
		numAccidents[i] = getNumAccidents(Boroughs[i]);
		cout << endl;
	}

	x = findLowest(numAccidents);

	cout << "Borough with the fewest accidents this year: " << Boroughs[x] << " (" << numAccidents[x] << ")";

	return 0;
}

int getNumAccidents (string instance)
{
	int numAccidents;
	cin >> numAccidents;
	return numAccidents;
}

int findLowest (int arr[])
{
	int Lowest = arr[0], hold;
	for (int i=0; i<4; i++)
	{
		if (arr[i+1] < Lowest)
		{
			Lowest = arr[i+1];
			hold = i+1;
		}
	}
	return hold;
}

The number of accidents in a year cannot be negative, so only a value of 0 or greater is an acceptable input.

how about an if statement or a loop where if the user inputs a number less than 0 then the user must input another number

Edited 4 Years Ago by zeroliken: n/a

how about an if statement or a loop where if the user inputs a number less than 0 then the user must input another number

Right I tried that, but how exactly can I make that work. Theoretically, in the case of an invalid input, for one thing I would want to clear that input (to convey that it is invalid, since the function can only return an integer) -- which I think is done by the command cin.clear(); -- and then the function would need to start over for the same instance... Something like this:

int getNumAccidents (string instance)
{
	int numAccidents;
	cin >> numAccidents;
	if (numAccidents >= 0)
		continue;
	else if (numAccidents < 0)
	{
		cin.clear();
		// break/retry -- somehow return to start of function here
	}
	return numAccidents;
}

But I would also think that the input would need to be validated as being an integer; I'm not sure if just this that I'm trying for (above) would do the trick. But at least for now, it will surely suffice... Pointers please?

Edited 4 Years Ago by caltech: n/a

for your last code you can't use a continue statement if it's not in a loop

next, why not just input a new value for numAccident if it's less than 0 like so?

int getNumAccidents (string instance)
{
	int numAccidents;
	cin >> numAccidents;
        while(numAccidents<0){
            cout << "enter value greater greater than or equal to 0" << endl;
            cin >> numAccidents;
        }
	return numAccidents;
}

Edited 4 Years Ago by zeroliken: n/a

This article has been dead for over six months. Start a new discussion instead.