Answer to Question #348529 in C++ for Ziwe

Question #348529

bestSub = 0


highestSales = monthSales(0)


for month = 1 to 11


if monthSales(month) > highestSales


highestSales = monthSales(month)


bestSub = month


endif


next month


display "The best sales for the year was in ", monthName(bestSub)


display "The amount was R”, highestSales


display "The top salesperson for that month was ", nameBestSP(bestSub)

1
Expert's answer
2022-06-09T18:20:44-0400
#include <iostream>
#include <map>
#include <string>

struct SP {
    int month = 0;
    std::map<std::string, int> salespersons;
    std::map<std::string, std::string> bestSP;
    int sales = 0;
};

int monthSales(int month);
std::string monthName(int bestSub);
std::string nameBestSP(SP bestSub);
int SPs(SP &bestSub);

int main() {
    SP bestSub = { 0 };
    int highestSales = 0;
    for (int month = 1; month <= 11; month++) {
        SP iMonth;
        iMonth.month = month;
        iMonth.sales = monthSales(month);
        SPs(iMonth);
        if (iMonth.sales > highestSales) {
            highestSales = iMonth.sales;
            bestSub = iMonth;
        }
    }
    std::cout << std::endl;
    std::cout << "The best sales for the year was in " << monthName(bestSub.month) << std::endl;
    std::cout << "The amount was R" << highestSales << std::endl;
    std::cout << "The top salesperson for that month was " << nameBestSP(bestSub) << std::endl;
    return 0;
}

int monthSales(int month) {
    if (month == 0)
        return 0;
    int sales;
    std::cout << "Enter sales for " << monthName(month) << ": ";
    std::cin >> sales;
    return sales;
}

std::string monthName(int bestSub) {
    std::map<int, std::string> months;
    months[1] = "January";
    months[2] = "February";
    months[3] = "March";
    months[4] = "April";
    months[5] = "May";
    months[6] = "June";
    months[7] = "July";
    months[8] = "August";
    months[9] = "September";
    months[10] = "October";
    months[11] = "November";
    return months[bestSub];
}

std::string nameBestSP(SP bestSub) {

    return bestSub.bestSP["best"];
}


int SPs(SP &bestSub) {
    std::string name, bestSP, more = "No";
    int sale;
    do {
        std::cout << "Enter salesperson name for that month: ";
        std::cin >> name;
        std::cout << "Enter his/her sales: ";
        std::cin >> sale;
        bestSub.salespersons[name] = sale;
        if (sale > bestSub.salespersons[bestSub.bestSP["best"]]) {
            bestSub.bestSP["best"] = name;
        }
        std::cout << "Do you want add one more salesperson? (Type \"Yes\" for add one more) ";
        std::cin >> more;
    } while( more == "Yes" || more == "yes"  || more == "YES");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS