首页 > 解决方案 > 在 C++ 中对复制的数组中的元素求和

问题描述

我编写了一个程序,通过文本文件创建餐厅菜单,在另一个程序中打开并用于创建客户订单。

我有一个函数需要在最后创建总金额,但是总和永远不会产生,程序会产生总和itemPrice * itemQuantity并打印它而不是对最终总和求和。

我将项目保存在menuItem数组中,并试图将它们复制到billingItems数组中以并行计算价格。total为了总结价格,我错过了哪一步,所以total底部等于 44 (16+28) ?

谢谢

在 main() 中创建订单:

int main(){
    int i;
    Item billingItem[MAX_ORDER];
    Item menuItem[MAX_ORDER];
    int itemNumber;
    int itemQuantity;
    int billOpt;

    std::ofstream transactionFile;
    transactionFile.open("transactionFile.txt");
    int count = 0;

    for(i = 0; i < MAX_ORDER; i++) {
        std::cout << "Item number: ";
        std::cin >> itemNumber;
        i = itemNumber - 1;
        std::cout << "Quantity: ";
        std::cin >> itemQuantity;
        menuItem[i].saveItemDetails(transactionFile);

        std::cout << "\nWould you like to add another item?" << std::endl;
        std::cout << "1. Yes\n2. No" << std::endl;
        std::cin >> billOpt;
        if (billOpt <= 0 | billOpt > 2) {
            std::cout << "Error. Please enter a valid number: ";
            std::cin >> billOpt;
        }
        if(billOpt == 2) { break; }
    }
    billingItem[i] = menuItem[i];
    billingItem[i].calculateVAT(count, itemQuantity);

    transactionFile.close();

实现文件中的函数:

void Item::calculateVAT(int count, int itemQuantity){
double total = 0.0;
double newItemPrice;

newItemPrice = itemPrice * itemQuantity;
total = newItemPrice + total;


//extra couts to see what is happening
std::cout << "ITEM QUANTITY" << itemQuantity <<"\n";
std::cout << "TOTAL" << total <<"\n";
}

运行程序时会产生什么:

Item: 2| Category: Meat| Description: Pork Chops| Price: £8
COUNT3
ITEM QUANTITY2
TOTAL16
Item: 3| Category: Meat| Description: Ribs| Price: £14
COUNT4
ITEM QUANTITY2
TOTAL28

标签: c++arraysfunctionsum

解决方案


您可以在函数中将 total 声明为 static,因此它只初始化一次。

 void Item::calculateVAT(int count, int itemQuantity){
static double total = 0.0;
double newItemPrice;

newItemPrice = itemPrice * itemQuantity;
total += newItemPrice ; // 


//extra couts to see what is happening
std::cout << "ITEM QUANTITY" << itemQuantity <<"\n";
std::cout << "TOTAL" << total <<"\n";
}

另一种解决方案是在 main 中声明一个总变量并修改您的函数签名。这意味着从您的主要打印总数。

计算增值税函数:

double Item::calculateVAT(int count, int itemQuantity)// No more void
{  
double total = 0.0; // this one only serves in the function scope
double newItemPrice;

newItemPrice = itemPrice * itemQuantity;
total = newItemPrice + total;


//extra couts to see what is happening
std::cout << "ITEM QUANTITY" << itemQuantity <<"\n";
std::cout << "TOTAL" << total <<"\n";

return total; // this number will add up in the main
}

你的主要:

int main(){
    int i;
    Item billingItem[MAX_ORDER];
    Item menuItem[MAX_ORDER];
    int itemNumber;
    int itemQuantity;
    int billOpt;
    double total = 0.0; // this here will serve as the sum of all your items prices

    std::ofstream transactionFile;
    transactionFile.open("transactionFile.txt");
    int count = 0;

    for(i = 0; i < MAX_ORDER; i++) {
        std::cout << "Item number: ";
        std::cin >> itemNumber;
        i = itemNumber - 1;
        std::cout << "Quantity: ";
        std::cin >> itemQuantity;
        menuItem[i].saveItemDetails(transactionFile);

        std::cout << "\nWould you like to add another item?" << std::endl;
        std::cout << "1. Yes\n2. No" << std::endl;
        std::cin >> billOpt;
        if (billOpt <= 0 | billOpt > 2) {
            std::cout << "Error. Please enter a valid number: ";
            std::cin >> billOpt;
        }
        if(billOpt == 2) { break; }
    }
    billingItem[i] = menuItem[i];
   total +=  billingItem[i].calculateVAT(count, itemQuantity); // modified here``
   std::cout << "TOTAL" << total << std::endl;


    transactionFile.close();

作为参考,我做了一个基本情况的简单代码:

     void calculateVAT(int count, int itemQuantity) { // Static version, is initialized only once and then only gets updated

    static double total = 0;
    double newItemPrice;

    newItemPrice = 10 * itemQuantity;
    total += newItemPrice ;


    //extra couts to see what is happening
    std::cout << "ITEM QUANTITY " << itemQuantity << "\n";
    std::cout << "TOTAL from function  " << total << "\n";
}

double d_calculateVAT(int count, int itemQuantity) { // Non void version, returns a sum to a variable in the main

    double sum = 0;
    double newItemPrice;

    newItemPrice = 10 * itemQuantity;
    sum += newItemPrice;

    return sum;
}

int main()
{
    int itemQty = 5;
    double sum = 0; // if you want to call it from the main

    calculateVAT(0, 4); // void version with static - will update its total variable as 40
    calculateVAT(0, 7); // void version with static - will update its total variable as 40 + 70 = 110

    sum+=d_calculateVAT(0, 4); // double version - will return sum = 40
    sum+=d_calculateVAT(2, 7); // double version - will return sum + 70 = 110

    std::cout << "TOTAL from main " << sum << std::endl;

    while (1)
    { }

    return 0;
}

另外,也许它是出于其他目的,但我没有看到发送给 calculateVAT 的变量“count”的意义,它没有在函数中使用。


推荐阅读