OK, so I want to create a function getStrRegion(commented out at the bottom) to pass a str value N, S, E, W, C back to the main function so I can then drop it and regionSelect back into getNumAccidents.

I'm not exactly sure how to pass a string of characters around and just about every instance I've seen thus far just confuse me even more.

What's the best way to go about this?

#include <iostream>
#include <string>

using namespace std;

// function prototypes
int getNumAccidents(int regionSelect); //get region accident data; reguires user input regionSelect
void findlowest();
void showMenu();

int main()
{
    int regionSelect, northNum = 0, southNum = 0, eastNum = 0, westNum = 0, centralNum = 0; // initializes @ 0 to perform while loop

    cout << "***************************************************************************\n"
         << "**                                                                       **\n"
         << "**       Welcome to the Accident Reporting and Collection Datebase.      **\n"
         << "**  This interface allows you to store and display accident information  **\n"
         << "**   based on the respective regions of the Chicago Metropolitan area.   **\n"
         << "**                                                                       **\n"
         << "***************************************************************************\n\n";

    while (northNum == 0 || southNum == 0 || eastNum == 0 || westNum == 0 || centralNum == 0) { // will continue until each region has a value
        showMenu(); // jumps to showMenu function to show regions
        cin >> regionSelect;

        switch (regionSelect) { //switch statement to run function getNumAccidents until each region has a value > 0
            case 1:
                northNum = getNumAccidents(regionSelect);
                break;
            case 2:
                southNum = getNumAccidents(regionSelect);
                break;
            case 3:
                eastNum = getNumAccidents(regionSelect);
                break;
            case 4:
                westNum = getNumAccidents(regionSelect);
                break;
            case 5:
                centralNum = getNumAccidents(regionSelect);
                break;
            default:    // 1-5 store a value in corresponding region, anything else errors
                cout << "Invalid entry.\n";
                break;
        }
    }

    //findLowest();
    return 0;
}


int getNumAccidents(int regionSelect) // value 1-5 is handed down
{
    int accidentNum; // value returned from function
    string region;

    switch (regionSelect) { // converts 1-5 to a string in accident input
        case 1:
            region = "North";
            break;
        case 2:
            region = "South";
            break;
        case 3:
            region = "East";
            break;
        case 4:
            region = "West";
            break;
        case 5:
            region = "Central";
            break;
    }

    cout << "\nPlease enter the number of reported accidents for the " << region << " region.\t";
    cin >> accidentNum;
        while (accidentNum <= 0) { // basic error checking returned value must be > 0
            cout << "\nInvalid number! Please re-enter the number of reported accidents.\n";
            cin >> accidentNum;
        }
    return (accidentNum);
}

void showMenu() // selection menu
{
    cout << "\nPlease select the region:\n\n"
         << "1) North\n"
         << "2) South\n"
         << "3) East\n"
         << "4) West\n"
         << "5) Central\t";
}

/*
int getStrRegion(int regionSelect)
{
    string region;

    switch (regionSelect) {
        case 1:
            region = "North";
            break;
        case 2:
            region = "South";
            break;
        case 3:
            region = "East";
            break;
        case 4:
            region = "West";
            break;
        case 5:
            region = "Central";
            break;
    }
    return (region);
}
*/

Recommended Answers

All 3 Replies

Ell you can only return one variable. If you want to return information from your function to more than 1 variable you will need to pass the variable into the function by reference or a pointer. That said I think a reference will be fine for you. See how this works.

int foo(int regionSelect, string & region)
{
    switch(regionSelect)
    {
        case 1: region = "North";
    }
    return someNumber;
}

I finally came up with this; it was one of those I was just tired of staring at the same code so I twisted it a lil and said good riddance. I'll give it a day or so and revisit. Thanks for your response!

#include <iostream>
#include <string>

using namespace std;

// function prototypes
int getNumAccidents(int, string); //get region accident data; reguires user input regionSelect
void findLowest(int, int, int, int, int, string);
void showMenu(int, int, int, int, int);
string getStrRegion(int regionSelect);


int main()
{
    int regionSelect, northNum = 0, southNum = 0, eastNum = 0, westNum = 0, centralNum = 0; // initializes @ 0 to perform while loop
    string regionLocation;
    cout << "***************************************************************************\n"
         << "**                                                                       **\n"
         << "**       Welcome to the Accident Reporting and Collection Datebase.      **\n"
         << "**  This interface allows you to store and display accident information  **\n"
         << "**   based on the respective regions of the Chicago Metropolitan area.   **\n"
         << "**       To change a selection just re-select the region number.         **\n"
         << "**                                                                       **\n"
         << "***************************************************************************\n\n";

    while (northNum == 0 || southNum == 0 || eastNum == 0 || westNum == 0 || centralNum == 0) { // will continue until each region has a value
        showMenu(northNum, southNum, eastNum, westNum, centralNum); // jumps to showMenu function to show regions
        cin >> regionSelect;


        regionLocation = getStrRegion(regionSelect); // jumps to function getStrRegion to convert number to string


        switch (regionSelect) { //switch statement to run function getNumAccidents until each region has a value > 0
        case 1:
            northNum = getNumAccidents(regionSelect, regionLocation);
            break;
        case 2:
            southNum = getNumAccidents(regionSelect, regionLocation);
            break;
        case 3:
            eastNum = getNumAccidents(regionSelect, regionLocation);
            break;
        case 4:
            westNum = getNumAccidents(regionSelect, regionLocation);
            break;
        case 5:
            centralNum = getNumAccidents(regionSelect, regionLocation);
            break;
        default:    // 1-5 store a value in corresponding region, anything else errors
            cout << "Invalid entry.\n";
            break;

        }
    }

        findLowest(northNum, southNum, eastNum, westNum, centralNum, regionLocation);
    return 0;
}


int getNumAccidents(int regionSelect,string regionLocation) // value 1-5 is handed down
{
    int accidentNum; // value returned from function

    cout << "\nPlease enter the number of reported accidents for the " << regionLocation << " region.\t";
    cin >> accidentNum;
    cout << string(5, '\n');

    while (accidentNum <= 0) { // basic error checking returned value must be > 0
        cout << "\nInvalid number! Please re-enter the number of reported accidents.\t";
        cin >> accidentNum;
    }
    return (accidentNum);
}

void showMenu(int northNum, int southNum, int eastNum, int westNum, int centralNum) // selection menu
{
    cout << "\nPlease select the region:\n"
         << "1) North\t" << northNum << "\n"
         << "2) South\t" << southNum << "\n"
         << "3) East \t" << eastNum << "\n"
         << "4) West \t" << westNum << "\n"
         << "5) Central\t" << centralNum << "\n"
         << "-------------------------: ";
}


string getStrRegion(int regionSelect)
{
    string region;

    switch (regionSelect) {
    case 1:
        region = "North";
        break;
    case 2:
        region = "South";
        break;
    case 3:
        region = "East";
        break;
    case 4:
        region = "West";
        break;
    case 5:
        region = "Central";
        break;
    }
    return (region);
}

void findLowest(int northNum, int southNum, int eastNum, int westNum, int centralNum, string regionLocation)
{
    int minNum = northNum;

    if (southNum < minNum)
    {
        minNum = southNum;
    } else
    if (eastNum < minNum)
    {
        minNum = eastNum;
    } else
    if (westNum < minNum)
    {
        minNum = westNum;
    } else
    if (centralNum < minNum)
    {
        minNum = centralNum;
    }


    cout << string(5, '\n')
         << "The region with the lowest concentration of traffic accidents is the\n"
         << regionLocation << " region with " << minNum << "."
         << string(5, '\n');
}

Consider using enums or contants for direction. For example:

#include <iostream>

enum Direction{WEST,NORTH,EAST,SOUTH};

//assumes proper ordering, WEST = 0, NORTH = 1, EAST = 2, SOUTH = 3
string showDirection(const Direction& d){
 const string encoding[] = {"WEST","NORTH","EAST","SOUTH"};
 return encoding[d];
}
int main(){
 Direction currentDirection = WEST;
 cout << showDirection(currentDirection);
 return 0;
}
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.