I am doing some code challenges from a book and am stumped on this particular challenge. I am supposed to pass an array to a function, use the function to determine the minimum, pass that value back to main and print a response varied by the returned value. The problem is that after it requests the string and value variables, it skips the last function and ends. I think the problem may be a logic error since Visual Studio isn't calling me on syntax errors, but if so I can't find it. Advice? Critique? I'm new to this so anything helps!

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>
using namespace std;

int getNumAccidents (string reg);
int findLowest(int * accidents);

int main ()
{
	string region1, region2, region3, region4, region5;
	int a1, a2, a3, a4, a5, min;
	cout << "Please enter a region: ";
	getline(cin, region1);
	a1 = getNumAccidents (region1);
	cout << "Please enter a region: ";
	cin.ignore();
	getline(cin, region2);
	a2 = getNumAccidents (region2);
	cout << "Please enter a region: ";
	cin.ignore();
	getline(cin, region3);
	a3 = getNumAccidents (region3);
	cout << "Please enter a region: ";
	cin.ignore();
	getline(cin, region4);
	a4 = getNumAccidents (region4);
	cout << "Please enter a region: ";
	cin.ignore();
	getline(cin, region5);
	a5 = getNumAccidents (region5);
	int accidents [5] = {a1, a2, a3, a4, a5};
	min = findLowest(accidents);

	if (min == a1)
		cout << region1 << " had the fewest accidents at " << a1 << " for the last year.\n";
	if (min == a2)
		cout << region2 << " had the fewest accidents at " << a2 << " for the last year.\n";
	if (min == a3)
		cout << region3 << " had the fewest accidents at " << a3 << " for the last year.\n";
	if (min == a4)
		cout << region4 << " had the fewest accidents at " << a4 << " for the last year.\n";
	if (min == a5)
		cout << region5 << " had the fewest accidents at " << a5 << " for the last year.\n";
	cout << "Press enter to end.";
	cin.ignore();
	cin.get();
	return 0;
}

int getNumAccidents(string region)
{
	int a;
	cout << "How many accidents were reported in " << region << " for the last year?" << endl;
	cin >> a;
	while(a < 0)
	{
		cout << "Please enter a valid number." << endl;
		cin >> a;
	}

	return a;
}

int findLowest (int * accidents)
{
	int i;
	int min = accidents[0];
	for (i = 0; i <= 5; i++)
	{
		if (accidents[i] < min)
		{
			min = accidents[i];
		}
	}
	return min;
}

I did check the forums and found some good info on passing arrays, but nothing with this specific problem. If it is answered in a post I missed, please direct me there! Thanks!

Recommended Answers

All 6 Replies

Made a mistake, was thinking about the memory location versus the size. Oi.

Hi, how can this condition to be ever true ?

cin << a;
while(a < 0)

Hi, how can this condition to be ever true ?

cin << a;
while(a < 0)

That's the validation loop. If the value entered is less than 0 it won't input. Or at least, that's what I was going for. It probably would help if I posted code tags.

oops, sorry i dont know what i was thinking you are right.

on line 72 you are checking to i <= 5 which will put you out of the bounds of the array. try changing it to i < 5 .

on line 72 you are checking to i <= 5 which will put you out of the bounds of the array. try changing it to i < 5 .

That did it! Thanks so much. I stared at that program for about 2 hours trying to find my logic error. It's always something simple - if only you could see it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.