Sorry the subject is hard to read I had to fit it all in there briefly but so in this external function getNumAccidents I need to read in the user-input number of accidents for each NYC borough to be stored in their respective array places, but also validate the input within the external function as obviously any number of accidents needs to be a positive integer, so if it's not I need to return to the beginning of the function I guess, which I can't figure out how to do really, I've tried if() and do/while and crap, but none of it's worked and in the end I just don't know. Then I need to store the returned values into another corresponding array in the main function, to be called by another function findLowest, which is passed the five accident totals [as an array of ints] and determines which is the smallest and returns the subscript for that array element.

Here's what I have (super basic... I know...):

#include <iostream>
#include <iomanip>
using namespace std;

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

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

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

	return 0;

int getNumAccidents (string instance)
	int num = 0;
	cin >> num;
	return num;

int findLowest (int arr[])



6 Years
Discussion Span
Last Post by caltech

Are you supposed to ask the user for the accident info that is stored?

Yeah. Here's the assignment:

Write a program that determines which of the 5 boroughs of NYC have the fewest reported automobile accidents in a year. The program should have the following two functions; both functions are called by the main() function.

int getNumAccidents(string) This function is passed the name of a borough. It asks the user (input) for the number of automobile accidents reported in the region for a year, validates the input and returns the value. It should be called once for each borough.

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.


The issues I'm having are 1) how to validate the input, and 2) storing the int value returned from the getNumAccidents function into the numAccidents array... because, see, line 18 of my code that I posted initially should theoretically (I would think), or is supposed to do just that (store the value in the array), but when I include that line of code -- it does work, EXCEPT the thing is it will take the user input for each borough/run the function twice for each borough, and it's the second input value that's stored in the array. So how else can I do it? (And how do I go about validating the input?) Thaaank you

NEVER MIND I just need help validating the input. The other thing was super obvious, I just took out line 17 and left line 18... problem solved... clearly....

Edited by caltech: n/a


Got everything in order except for the input validation. Need to [within the getNumAccidents function] validate that the user input is a positive integer... somehow... How? Help? Thanks a mil

Edited by caltech: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.