函数和数组有问题

Having troubles w/functions and arrays

本文关键字:有问题 数组 函数      更新时间:2023-10-16

我正在上一门课,我正在使用Visual studio(C++),我不得不自学函数,我在路上遇到了一个小障碍,希望能给我一些建议。

我遇到的问题是,任务中规定我必须

"利用打印(找不到)最大/平均/最小佣金的功能"

按照我读这篇文章的方式,我假设她只想打印出来,而不想在函数中进行计算。

一个朋友建议我尝试void print(等等)。但是我不确定如何获取主要的计算结果并将其提供给我试图打印的函数,还是我完全错了?

我已经注释掉了我试图运行的代码部分。您也可以在代码的最底部找到它。

我们非常感谢任何建议/帮助,因为我查找的大多数内容都没有真正解决这个问题(我发现了)

#include <iostream>
#include <iomanip>
using namespace std;
#define TITLE "Alva's"
#define STANDARD 0.05
#define HYBRID   0.10
#define ELECTRIC 0.15
#define HOLD     50
void dashLine();
int getSalesId();
int runProgram();
char vehicleType();
double sellingPrice();
void print(double largeSmallAverage);
int main()
{
    int tot_count,
        id_num[HOLD], //* ID
        r_ay = 0,  //* array
        s_count, //*standard
        h_count, //*Hybrid
        e_count, //*electric
        hold_id, //*array Id
        compare, //*Pass
        change, //*change made when change has value
        yesno;
    double tot_standard,
        tot_hybrid,
        tot_electric,
        tot_price,
        price[HOLD],
        hold_price, //*Array hold
        comm_l,  //* Large
        comm_s,  //* Small
        hold_comm, //*Array hold
        avg_comm,
        tot_commission,
        commission[HOLD];
    char car[HOLD],
        temp_car;
    tot_count = 0;
    s_count = 0;
    h_count = 0;
    e_count = 0;
    tot_price = 0;
    tot_standard = 0;
    tot_hybrid = 0;
    tot_electric = 0;
    cout << "n" << TITLE << " Commission Calculator";
    yesno = runProgram();
    while (yesno == 1)
    {
        dashLine();
        id_num[r_ay] = getSalesId();
        car[r_ay] = vehicleType();
        price[r_ay] = sellingPrice();
        tot_price += price[r_ay];
        switch (car[r_ay])
        {
        case 'S':
        case 's':
            commission[r_ay] = (price[r_ay] * STANDARD);
            tot_standard += commission[r_ay];
            s_count++;
            break;
        case 'H':
        case 'h':
            commission[r_ay] = (price[r_ay] * HYBRID);
            tot_hybrid += commission[r_ay];
            h_count++;
            break;
        case 'E':
        case 'e':
            commission[r_ay] = (price[r_ay] * ELECTRIC);
            tot_electric += commission[r_ay];
            e_count++;
            break;
        }
        cout << "n The commission for this sale, for Employee ID: " << fixed << setprecision(0) << id_num[r_ay] << " is:$ " << fixed << setw(5) << setprecision(2) << commission[r_ay];
        cout << "n";
        yesno = runProgram();
        r_ay++;
        if (r_ay >= HOLD)
            yesno = 0;
    }
    tot_count = (s_count + h_count + e_count);
    tot_commission = (tot_standard + tot_hybrid + tot_electric);
    {
        cout << "n Number of standard vehicle commissions calculated =  " << fixed << setw(8) << setprecision(0) << s_count;
        cout << "n Number of hybrid vehicle commissions calculated   =  " << fixed << setw(8) << h_count;
        cout << "n Number of electric vehicle commissions calculated =  " << fixed << setw(8) << e_count;
        cout << "n Number of vehicle commissions calculated          =  " << fixed << setw(8) << tot_count;
        cout << "n Total Overall price calculated                    =$ " << fixed << setw(8) << setprecision(2) << tot_price;
        cout << "n Total amount of standard vehicle commissions      =$ " << fixed << setw(8) << tot_standard;
        cout << "n Total amount of hybrid vehicle commissions        =$ " << fixed << setw(8) << tot_hybrid;
        cout << "n Total amount of electric vehicle commissions      =$ " << fixed << setw(8) << tot_electric;
        cout << "n Total amount of all commissions paid out          =$ " << fixed << setw(8) << tot_commission;
        cout << "n";
        cout << "n " << "Sales ID   " << "Car type    " << "Selling price    " << "Commission    ";
        for (r_ay = 0; r_ay < tot_count; r_ay++)
        {
            cout << "n " << fixed << id_num[r_ay] << "         " << setprecision(2) << car[r_ay] << "          " << setw(10) << price[r_ay] << "         " << setw(10) << commission[r_ay];
        }
        if (tot_count > 0)
        {
            avg_comm = (tot_commission / tot_count);
            comm_s = commission[0];
            comm_l = commission[0];
            for (r_ay = 1; r_ay < tot_count; r_ay++)
            {
                if (commission[r_ay] < comm_s)
                    comm_s = commission[r_ay];
                if (commission[r_ay] > comm_l)
                    comm_l = commission[r_ay];
            }
            void print(double largeSmallAverage);
            //{
            //  cout << "n ";
            //  cout << "n The smallest commission computed totals               =$ " << fixed << setw(10) << comm_s;
            //  cout << "n The largest commission computed totals                =$ " << fixed << setw(10) << comm_l;
            //  cout << "n Total average of commissions computed       =$ " << fixed << setw(10) << avg_comm;
            //}
        }
        cout << "n";
        change = 1;
        compare = tot_count - 1;
        do
        {
            change = 0;
            for (r_ay = 0; r_ay < compare; r_ay++)
            {
                if (commission[r_ay] > commission[r_ay + 1])
                {
                    temp_car = car[r_ay];
                    hold_id = id_num[r_ay];
                    hold_price = price[r_ay];
                    hold_comm = commission[r_ay];
                    commission[r_ay] = commission[r_ay + 1];
                    commission[r_ay + 1] = hold_comm;
                    id_num[r_ay] = id_num[r_ay + 1];
                    id_num[r_ay + 1] = hold_id;
                    car[r_ay] = car[r_ay + 1];
                    car[r_ay + 1] = temp_car;
                    price[r_ay] = price[r_ay + 1];
                    price[r_ay + 1] = hold_price;
                    change = 1;
                }
            }
            compare--;
        } while ((compare > 0) && (change == 1));
        cout << "n";
        cout << "n " << "Sales ID   " << "Car type    " << "Selling price    " << "Commission    ";
        for (r_ay = 0; r_ay < tot_count; r_ay++)
        {
            cout << "n " << fixed << id_num[r_ay] << "         " << setprecision(2) << car[r_ay] << "          " << setw(10) << price[r_ay] << "         " << setw(10) << commission[r_ay];
            cout << "n ";
        }
        system("pause");
        return 0;
    }
}
void dashLine()
{
    cout << "n -----------------------------------";
}
int getSalesId()
{
    int id_num;
    cout << "n Please enter Employee ID:                   ";
    cin >> id_num;
    while (id_num < 10000 || id_num > 99999)
    {
        cout << "n Invalid Employee ID, Please enter a 5 digit ID ";
        cin >> id_num;
    }
    return id_num;
}
int runProgram()
{
    int rp_yesno;
    cout << "n Is there a customer? 1 = yes, 0 = no ";
    cin >> rp_yesno;
    while ((rp_yesno != 1) && (rp_yesno != 0))
    {
        cout << "n Invalid Entry Please enter 1/0 ";
        cout << "n Is there a customer? 1 = yes 0 = no ";
        cin >> rp_yesno;
    }
    return rp_yesno;
}
char vehicleType()
{
    char car;
    cout << "n Please enter type of vehicle sold";
    cout << "n (S=standard, H=hybrid, E=electric):         ";
    cin >> car;
    while (!((car == 'S') || (car == 's') || (car == 'h') || (car == 'H') || (car == 'E') || (car == 'e')))
    {
        cout << "n Invalid input Please enter S/E/H ";
        cin >> car;
    }
    return car;
}
double sellingPrice()
{
    double price;
    cout << "n Please enter the selling price of the car:$ " << fixed << setprecision(2);
    cin >> price;
    while (price < 1)
    {
        cout << "n Invalid entry, Please enter an amount greater than 0 ";
        cin >> price;
    }
    return price;
}
void print(double largeSmallAverage)
{
    double comm_s,
        comm_l,
        avg_comm;
    {
        cout << "n ";
        cout << "n The smallest commission computed totals               =$ " << fixed << setw(10) << comm_s;
        cout << "n The largest commission computed totals                =$ " << fixed << setw(10) << comm_l;
        cout << "n Total average of commissions computed       =$ " << fixed << setw(10) << avg_comm;
    }
} 

您将拥有一个名为print的函数,该函数具有三个double参数:void print(double small, double large, double average);

然后,稍后您将使用print(comm_s, comm_l, avg_comm);进行调用。

您需要更改print:的定义

void print(double small, double large, double average)
{
    cout << "n ";
    cout << "n The smallest commission computed totals               =$ " << fixed << setw(10) << small;
    cout << "n The largest commission computed totals                =$ " << fixed << setw(10) << large;
    cout << "n Total average of commissions computed       =$ " << fixed << setw(10) << average;
}