Home > OS >  Trying to display a string of words from an array that contains integers
Trying to display a string of words from an array that contains integers

Time:04-29

I am working on this problem, and I'm stuck at displaying what I need to. Here is the question:

Write a program that determines which of a company's four divisions (Northeast, Southeast, Northwest, and Southwest) had the greatest sales for a quarter. It should include the following two functions, which are called by main.

  • double getSales() is passed the name of a division. It asks the user for the division's quarterly sales figure, validates the input, then returns it. It should be called once for each division.

  • void findHighest() is passed the four sales totals. It determines which is the largest and prints the name of the high-grossing division, along with its sales figure.

I was able to figure everything out except for showing the division name. I'm new to coding in general, so please bear with me if I have other mistakes here that I'm unaware of. I have looked up everything I could, and made so many changes, and am still lost. Please help!

Here is my code:

#include <iostream>
#include <string>

using namespace std;

// Variable Declerations


const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";

double getSales(string);
void findHighest(double, double, double, double);

int main()
{
    double salesNE, salesSE, salesNW, salesSW;
    string division;


    salesNE = getSales(NE);
    salesSE = getSales(SE);
    salesNW = getSales(NW);
    salesSW = getSales(SW);

    findHighest(salesNE, salesSE, salesNW, salesSW);

    cout << "by " << division << endl;

    return 0;

}

double getSales(string division)
{
    double sales;
    cout << "Enter the quarterly sales figure for " << division << endl;
    cin >> sales;

    while (sales < 0)
    {
        cout << "Invalid input. Please enter a positive integer.";
        cin >> sales;
    }

    return sales;
}


void findHighest(int salesNE, int salesSE, int salesNW, int salesSW)
{
    int divisions[] = { salesNE, salesSE, salesNW, salesSW};
    int max;

    max = divisions[0];
    for (int count = 1; count > 5; count  )
    {
        if (divisions[count] < max)
            max = divisions[count];
    }

    /*
    cout << division << endl; - does not show.
    */

    cout << " is the division with the highest sales total of: " << max << endl;
}

CodePudding user response:

You already solved the problem, you just didn't finish it. In findHighest(), you are putting the sales figures into an array. So already, you know which value goes with which division. Simply do the same thing with the division names, and then find the array index with the highest sales, so you can display the division name at the same index.

There are some other problems with your code:

  • the declaration and definition of findHighest() do not match.

  • you are trying to display the division in main(), when the instructions clearly told you to display it in findHighest() instead.

  • getSales() does not account for the possibility that the user may enter input that is not convertible to double.

  • your for loop in findHighest() is coded wrong. It needs to use < 4 instead of > 5 in the counter, and it needs to use > max instead of < max in the comparison.

Try this:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

// Variable Declerations

const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";

double getSales(string);
void findHighest(double, double, double, double);

int main()
{
    double salesNE, salesSE, salesNW, salesSW;

    salesNE = getSales(NE);
    salesSE = getSales(SE);
    salesNW = getSales(NW);
    salesSW = getSales(SW);

    findHighest(salesNE, salesSE, salesNW, salesSW);

    return 0;
}

double getSales(string division)
{
    double sales;
    cout << "Enter the quarterly sales figure for " << division << endl;

    do
    {
        if (cin >> sales)
        {
            if (sales >= 0) break;
            cout << "Invalid input. Please enter a positive number.";
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please enter a valid number.";
        }
    }
    while (true);

    return sales;
}

void findHighest(double salesNE, double salesSE, double salesNW, double salesSW)
{
    double sales[] = { salesNE, salesSE, salesNW, salesSW };
    string divisions[] = { NE, SE, NW, SW };
    int highest;
    double max;

    highest = 0;
    max = sales[0];

    for (int index = 1; index < 4;   index)
    {
        if (sales[index] > max)
        {
            highest = index;
            max = sales[index];
        }
    }

    cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}

Which can then be simplified a little further, if you move the divisions[] array into global scope, and use an array in main() to hold the sales figures. The instructions just say to ask for the figures and pass them into findHighest(), but they don't say how to save and pass them in:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

// Variable Declerations

const string divisions[] = { "Northeast", "Southeast", "Northwest", "Southwest" };
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);

double getSales(string);
void findHighest(double[]);

int main()
{
    double sales[numDivisions];

    for(int index = 0; index < numDivisions;   index)
    {
        sales[index] = getSales(divisions[index]);
    }

    findHighest(sales);

    return 0;
}

double getSales(string division)
{
    double sales;
    cout << "Enter the quarterly sales figure for " << division << endl;

    do
    {
        if (cin >> sales)
        {
            if (sales >= 0) break;
            cout << "Invalid input. Please enter a positive number.";
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please enter a valid number.";
        }
    }
    while (true);

    return sales;
}

void findHighest(double sales[])
{
    int highest = 0;
    double max = sales[0];

    for (int index = 1; index < numDivisions;   index)
    {
        if (sales[index] > max)
        {
            highest = index;
            max = sales[index];
        }
    }

    cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}

Alternatively:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

// Variable Declerations

struct Division
{
    string name;
    double sales;
}

Division divisions[] =
{
    {"Northeast", 0.0},
    {"Southeast", 0.0},
    {"Northwest", 0.0},
    {"Southwest", 0.0}
};
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);

double getSales(string);
void findHighest();

int main()
{
    for(int index = 0; index < numDivisions;   index)
    {
        divisions[index].sales = getSales(divisions[index].name);
    }

    findHighest();

    return 0;
}

double getSales(string division)
{
    double sales;
    cout << "Enter the quarterly sales figure for " << division << endl;

    do
    {
        if (cin >> sales)
        {
            if (sales >= 0) break;
            cout << "Invalid input. Please enter a positive number.";
        }
        else
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input. Please enter a valid number.";
        }
    }
    while (true);

    return sales;
}

void findHighest()
{
    int highest = 0;
    double max = divisions[0].sales;

    for (int index = 1; index < numDivisions;   index)
    {
        if (divisions[index].sales > max)
        {
            highest = index;
            max = divisions[index].sales;
        }
    }

    cout << divisions[highest].name << " is the division with the highest sales total of: " << max << endl;
}
  • Related