program won't end

Here is the problem that I have been working on. My program runs without errors, but it never says which region has the fewest number of accidents and it keeps asking for North: South: East: West: Central: over and over. I've driven myself nuts trying to figure out how to fix this, so any advice would be greatly appreciated!

[Write a program that determines which of the 5 geographic regions within a major city (north, south, east, west, and central) had the fewest reported traffic accidents last year. It should have the two functions, which are called by main:

int getNumAccidents() is passed the name of the region. It asks the user for the number of traffic accidents reported in that region during the last year, validates the input, then returns it. It should be called once for each city region.

void findLowest () is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its accident figure.

Input Validation: Do not accept an accident number that is less than zero.]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 #include <iostream>
#include <iomanip>
using namespace std;

/*
Function Prototypes
*/

int getNumAccidents (int&);
void findLowest (int, int, int, int, int);

/*
Main Function*/


int main()
{
    int north, south, east, west, central;

    // call getNumAccidents
    north = getNumAccidents(north);
    south = getNumAccidents(south);
    east = getNumAccidents(east);
    west = getNumAccidents(west);
    central = getNumAccidents(central);

    // call findLowest to find and display the lowest region
    findLowest(north, south, east, west, central);

    return 0;
} // end of main functin

/* getNumAccidents
*/
int north, south, east, west, central;
int getNumAccidents(int&)
{
    cout << "Enter the number of accidents in each region: \n"
         << "North : ";
    cin >> north;

    while (north < 0)
    {
        cout << "The number of accidents canot be negative" << endl
            << "Please re-enter the number of accidents: ";
        cin >> north;
    }

    cout << "South : ";
    cin >> south;

    while (south < 0)

    {
        cout << "The number of accidents canot be negative" << endl
            << "Please re-enter the number of accidents: ";
        cin >> south;
    }

    cout << "East : ";
    cin >> east;

    while (east < 0)
    {
        cout << "The number of accidents canot be negative" << endl
            << "Please re-enter the number of accidents: ";
        cin >> east;
    }

    cout << "West : ";
    cin >> west;

    while (west < 0)
    {
        cout << "The number of accidents canot be negative" << endl
            << "Please re-enter the number of accidents: ";
        cin >> west;
    }

    cout << "Central : ";
    cin >> central;

    while (central < 0)
    {
        cout << "The number of accidents canot be negative" << endl
            << "Please re-enter the number of accidents: ";
        cin >> central;
    }
    return north, south, east, west, central;
}

/* findLowest()
*/
void findLowest (int north, int south, int east, int west, int central)
{
    int lowest = north;

    if (south < lowest)
        lowest = south;
    if (east < lowest)
        lowest = east;
    if (west < lowest)
        lowest = west;
    if (central < lowest)
        lowest = central;
        

    // Display the region with the least amount of accidents
    if (lowest == north)
        cout << "The North region had the lowest amount of accidents. \n"
                << "Only " << lowest << " accidents were reported. \n";
    else if (lowest == south)
        cout << "The South region had the lowest amount of accidents. \n"
                << "Only " << lowest << " accidents were reported. \n";
    else if (lowest == east)
        cout << "The East region had the lowest amount of accidents. \n"
                << "Only " << lowest << " accidents were reported. \n";
    else if (lowest == west)
        cout << "The West region had the lowest amount of accidents. \n"
                "Only " << lowest << " accidents were reported. \n";
    else if (lowest == central)
        cout << "The Central region had the lowest amount of accidents. \n"
                << "Only " << lowest << " accidents were reported. \n";
} // end of findLowest
In int getNumAccidents() why are you trying to return multiple values? You cannot return more than one value from a function.

This line
return north, south, east, west, central;
doesn't cause errors because it is interpreted as the use of a comma operator, and is not doing what you think it is. If you want to manipulate and effectively return all 5 values, pass each in as a reference.

Also, in this section of your posted code:
1
2
3
4
5
/* getNumAccidents
*/
int north, south, east, west, central;
int getNumAccidents(int&)
{

Why do you have a redeclaration of the integer values? It appears like they are being declared globally, but you also declare them in main...
thank you for the advice, but how should I change the [int getNumAccidents] to only return 1 variable? I am trying to return 5 variables because there are five regions. Is that not the correct approach?

What should I be returning instead of [return north, south, east......]
Either only return 1 variable, or make the function void and don't return anything. You want to manipulate 5 variables in one function, thus you should use references. Basically instead of just passing a copy of each variable to the function (a copy of its value), you pass a reference to it, and are then able to modify that variable inside the scope of the function.

Read this, it may help clear up this topic! http://www.cplusplus.com/doc/tutorial/functions2/

Try something like this instead:
1
2
3
4
5
6
int getNumAccidents(int& north, int& south, int& east, int& west, int& central)
{
	// Code here
	// Do want you want to north, south, east and west and assign the values to those
	// variables accordingly
}

The variables that are modified inside this function now retain the changes made to them outside of the function, effectively 'returning' 5 variables instead of just 1.
Last edited on
Topic archived. No new replies allowed.