void functions

Hey guys I'm new here. I have a little bit of knowledge about c++ but I need some help with this problem. I know my program doesn't work, but if you could help me to figure it out.

Write a program that determines which of 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 following two functions which are called by main. 1) int getNumAccidents() is passed the name of a 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. 2) void findLowest() is passed the five accidents 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 0.


#include <iostream>
using namespace std;

void findLowest();
int getNumAccidents();


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

cout<<"Where going to determine which region in your city had the least number of accidents"<<endl;
getNumAccidents();


int getNumAccidents(int north, int south, int east, int west, int central);
{
cout<<"Please enter the number of accidents each region had"<<endl;
cout<<"How many accidents did the North region have:"<<endl;
cin>>north;
cout<<"How many accidents did the South region have:"<<endl;
cin>>south;
cout<<"How many accidents did the East region have:"<<endl;
cin>>east;
cout<<"How many accidents did the West region have:"<<endl;
cin>>west;
cout<<"How many accidents did the Central region have:"<<endl;
cin>>central;
}

while(north>0 && south>0 && east>0 && west>0 && central>0)
{
cout<<"Please enter a positive number of accidents for each region"<<endl;
cout<<"How many accidents did the North region have:"<<endl;
cin>>north;
cout<<"How many accidents did the South region have:"<<endl;
cin>>south;
cout<<"How many accidents did the East region have:"<<endl;
cin>>east;
cout<<"How many accidents did the West region have:"<<endl;
cin>>west;
cout<<"How many accidents did the Central region have:"<<endl;
cin>>central;

cout<<north<<south<<east<<west<<central;
return 0;
}
}
I'd help more but i have to leave. I added some comments and reformatted it a lil to make more sense an lil easier to read. Not really supposed to make a function inside them main. just use it as a place to call them.

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
#include <iostream>
using namespace std;

void findLowest();
int getNumAccidents();

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


cout<<"Where going to determine which region in your city had the least number of accidents"<<endl;
getNumAccidents();// this has zero arguments needs five 




while(north>0 && south>0 && east>0 && west>0 && central>0)// this statement can never be false bc the input is constant
{
cout<<"Please enter a positive number of accidents for each region"<<endl;
cout<<"How many accidents did the North region have:"<<endl;
cin>>north;
cout<<"How many accidents did the South region have:"<<endl;
cin>>south;
cout<<"How many accidents did the East region have:"<<endl;
cin>>east;
cout<<"How many accidents did the West region have:"<<endl;
cin>>west;
cout<<"How many accidents did the Central region have:"<<endl;
cin>>central;

cout<<north<<south<<east<<west<<central;// this is just gona display them all need to set up a function to determine lowest 
return 0;
}
}





int getNumAccidents(int north, int south, int east, int west, int central)
{
cout<<"Please enter the number of accidents each region had"<<endl;
cout<<"How many accidents did the North region have:"<<endl;
cin>>north;
cout<<"How many accidents did the South region have:"<<endl;
cin>>south;
cout<<"How many accidents did the East region have:"<<endl;
cin>>east;
cout<<"How many accidents did the West region have:"<<endl;
cin>>west;
cout<<"How many accidents did the Central region have:"<<endl;
cin>>central;
}
is this better?

#include <iostream>
using namespace std;

int getNumAccidents();
void findLowest();

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

cout<<"Where going to determine which region in your city had the least number of accidents"<<endl;
cout<<"Please enter the number of accidents each region had"<<endl;

int getNumAccidents();
{
cout<<"How many accidents did the North region have:"<<endl;
cin>>north;while(north<0)
{
cout<<"The number must be positive:"<<endl;
cin>>south;
}

cout<<"How many accidents did the South region have:"<<endl;
cin>>south;while(south<0)
{
cout<<"The number must be positive:"<<endl;
cin>>south;
}

cout<<"How many accidents did the East region have:"<<endl;
cin>>east;while(east<0)
{
cout<<"The number must be positive:"<<endl;
cin>>east;
}

cout<<"How many accidents did the West region have:"<<endl;
cin>>west;while(west<0)
{
cout<<"The number must be positive:"<<endl;
cin>>west;
}

cout<<"How many accidents did the Central region have:"<<endl;
cin>>central;while(central<0)
{
cout<<"The number must be positive:"<<endl;
cin>>central;
}
}
cout<<"The north had "<<north<<" accidents."<<endl;
cout<<"The south had "<<south<<" accidents."<<endl;
cout<<"The east had "<<east<<" accidents."<<endl;
cout<<"The west had "<<west<<" accdients."<<endl;
cout<<"The central had "<<central<<" accdients."<<endl;;
return 0;
}
Last edited on
no it not better. For starters now you have an erroneous while statement in every cin cout event.

the while statements are now always going to be false. unless you enter negative numbers in the cin part. also the entire point of the program is to find the geographical section that has the lowest number of accidents. That was not accomplished your just reiterating the number that was input by displaying "the north had "<<north ect. An last but not least you misspelled accidents two times in the last 4 lines of code.

check here for while statement info as well as for loops an if else statements http://www.cplusplus.com/doc/tutorial/control/

here is all about functions http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Topic archived. No new replies allowed.