Passing strings to functions

Hello, I am relatively new to the C++ world and I had a question about passing strings to a function.

This is a homework question and I do not want any solutions but I would like a hint or even to know if this can be done.

It is listed here:

http://www3.delta.edu/donaldsouthwell/cst180/cst180_Lab07.htm

The problem consists of entering 5 different regions (North, South, East, West, and Central). int getNumAccidents() is passed the name of a region. It asks the user for the number of automobile accidents reported in that region during the last year, validates the input, then returns it. It should be called once for each city region.

I was doing north = getNumAccidents("North") and using the function like so:

int getNumAccidents(char section) // passing "North"
{
cout << "How many automobile accidents were reported in the " << section << " sector last year? ";
cin >> numAccidents;
if (numAccidents < 0)
{
cout << "Values cannot be less than zero. Please try again.\n";
cout << "How many automobile accidents were reported in the " << section << " sector last year? ";
cin >> numAccidents;
}

return numAccidents;
}


This is giving me a slew of errors.

Any ideas how to pass a string to a function to get an int?

Brian
Last edited on
you should use char* or std::string as argument type, not char

where is numAccidents declared?

You should use a loop instead of an if for input validation
For better input validation read: http://www.cplusplus.com/forum/articles/6046/
Thanks I will look into that. numAccidents was declared earlier but with all my trial and error attempts I must have deleted it, thanks for reminding me to put it back in.
Topic archived. No new replies allowed.