revised program help

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. However, now the cout statements do not pop up when a number inputted is negative. 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
  
#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
    getNumAccidents(north);
    getNumAccidents(south);
    getNumAccidents(east);
    getNumAccidents(west);
    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 getNumAccidents(int& accidentInRegion)
{
    cout << "Enter the number of accidents in each region: \n"
         << "North : ";
    cin >> accidentInRegion;

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

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

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

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

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

/* 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 your original you are setting locations 25 times, because you are calling the get* function 5 times, and in the function you set the same variable 5 times, thus, all variables are the same if you follow the UI.

Try to be generic, you are writing a lot of code that the computer is supposed to work out. I added some functions that are in algorithm that'll make your life simpler, but eventually you'll work out how they work internally. Just explore a bit. en.cppreference is a good website for finding out about the standard library. I also found iomanip not used, so I removed it.

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
#include <iostream>
#include <algorithm>


void getNumAccidents (int[5]);
void findLowest (int[5]);

int main()
{
    int regions[5];

    // call getNumAccidents
    getNumAccidents(regions);

    // call findLowest to find and display the lowest region
    findLowest(regions);

    return 0;
}

void getNumAccidents(int accidentInRegion[5])
{
    const char *reg_names[5]
    {
        "North",
        "South",
        "West",
        "East",
        "Cental"
    };
    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Enter an accident count for " << reg_names[i] << " region: ";
        std::cin >> accidentInRegion[i];

        while (accidentInRegion[i] < 0)
        {
            std::cout << "Accidents can not be negative, please retry" << std::endl;
            std::cin >> accidentInRegion[i];
        }
    }
}


void findLowest (int regs[5])
{
    const char *reg_names[5]
    {
        "North",
        "South",
        "West",
        "East",
        "Cental"
    };
    int *lowest = std::min_element
    (
        regs, regs + 5
    );

    // Display the region with the least amount of accidents
    std::cout
        << "The "
        << reg_names[std::distance(regs, lowest)]
        << " region had the fewest accidents, with a count of: "
        << *lowest
        << std::endl;
} // end of findLowest
@Kefin
1
2
3
4
5
6
7
8
    const char *reg_names[5]
    {
        "North",
        "South",
        "West",
        "East",
        "Cental"
    };


What is that doing? is declaring a pointer to 5 different character arrays that hold the region names?

im really curious because i havent seen that before.

and if you know, why does it need the semicolon at the end of the bracket like an enum or struct?
The bracket is an initializer list. The entire thing is a single statement that initializes an array of const char *, thus, we need a ; to end the statement.

A const char * is a c-style string, and there are 5 of them in the array. We specify an array using the []. The number 5 can be omitted since the compiler can deduce the amount of elements by itself, but for clarity I added it.
Oh i get it now. i just never seen const char * before, thanks for the explanation :D
closed account (1v5E3TCk)


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <iomanip>
using namespace std;


int north, south, east, west, central;


/*
Function Prototypes
*/

void getNumAccidents ();
void sort (int array[], int size);
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);

/*
Main Function*/


int main()
{
    
    int array[ 5 ];

  
    // call getNumAccidents
   getNumAccidents();

   array[ 0 ] = north;
   array[ 1 ] = south;
   array[ 2 ] = east;
   array[ 3 ] = west;
   array[ 4 ] = central; 

   sort ( array , 5 ); 

   // Display the region with the least amount of accidents
    if (array[0] == north)
        cout << "The North region had the lowest amount of accidents. \n"
                << "Only " << north << " accidents were reported. \n";
    else if (array[0] == south)
        cout << "The South region had the lowest amount of accidents. \n"
                << "Only " << south << " accidents were reported. \n";
    else if (array[0] == east)
        cout << "The East region had the lowest amount of accidents. \n"
                << "Only " << east << " accidents were reported. \n";
    else if (array[0] == west)
        cout << "The West region had the lowest amount of accidents. \n"
                "Only " << west << " accidents were reported. \n";
    else if (array[0] == central)
        cout << "The Central region had the lowest amount of accidents. \n"
                << "Only " << central << " accidents were reported. \n";


    return 0;
} // end of main functin

/* getNumAccidents
*/

void getNumAccidents()
{
    cout << "Enter the number of accidents in each region: \n"
         << "North : ";
    cin >> north;

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

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

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

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

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

/* findLowest()
*/

void sort (int array[], int size)
{
	for ( int i = 0; i < size; i++ )
	{
		int index = findSmallestRemainingElement( array, size, i );
		swap( array, i, index );
	}
}

int findSmallestRemainingElement (int array[], int size, int index)
{
	int index_of_smallest_value = index;
	for (int i = index + 1; i < size; i++)
	{
		if ( array[ i ] < array[ index_of_smallest_value ]  )
		{
			index_of_smallest_value = i;
		}
	}
	return index_of_smallest_value;
}


void swap (int array[], int first_index, int second_index)
{
	int temp = array[ first_index ];
	array[ first_index ] = array[ second_index ];
	array[ second_index ] = temp;
}

thank you I got it to work!
closed account (1v5E3TCk)
No problem
Topic archived. No new replies allowed.