运行数学函数后,如何不将其显示在屏幕上

After running a mathematical function, how do I not display it to the screen?

本文关键字:显示 屏幕 何不 函数 运行      更新时间:2023-10-16

对于糟糕的标题,我提前道歉,很难找到几个词来有效地概括我的问题。我必须做一个编程项目来制作收银机。找到物品的总金额后,我必须输入一个零钱值,并用二十、十、五、单、四分之一、一角钱、镍币和便士找零。我已经完成了计算钞票和硬币变化的编程,但我的教授希望我们如果没有归还任何钞票或硬币,就不要包括纸币或硬币。

这是我到目前为止的代码-

void find_change(){
double change_given, updated_price, coin_price;
//item_price_total = 318.32;    keep this in here for testing purposes
//change_given = 405.23;        ' '
int   twenties,
        tens,
        fives,
        singles,
        quarters,
        dimes,
        nickels,
        pennies;
//finds the change in bills
do{
    cout << "How much change is given? " << endl;
    while(!(cin >> change_given)){                //tests to make sure value entered can be used
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), 'n');
    cout << "Invalid input.  Try again: ";
    }
    if(change_given < item_price_total){
        cout << "You did not give the machine enough money" << endl;
    }
}while(change_given < item_price_total);
updated_price =change_given-item_price_total;
cout << "The total price is: " << item_price_total << endl;
cout << "The change given is: " << change_given << endl;
cout << "The change back will be: " << updated_price << endl;
twenties = updated_price / 20;
cout << "Twenties: " << twenties << endl;
updated_price = updated_price -(twenties *20);
tens = updated_price/10;
cout << "Tens: " << tens << endl;
updated_price = updated_price - (tens*10);
fives = updated_price/5;
cout << "Fives: " << fives << endl;
updated_price = updated_price - (fives*5);
singles = updated_price/1;
cout << "Singles: " << singles << endl;
updated_price = updated_price - (singles*1);
//this part finds the coins left
coin_price = updated_price * 100;
//finds the change in coins
quarters = coin_price/25;
cout << "Quarters: " << quarters << endl;
coin_price = coin_price - (quarters*25);
dimes = coin_price/10;
cout << "Dimes: " << dimes << endl;
coin_price = coin_price - (dimes*10);
nickels = coin_price/5;
cout << "Nickels: " << nickels << endl;
coin_price = coin_price - (nickels*5);
pennies = coin_price/1;
cout << "Pennies: " << pennies << endl;
coin_price = coin_price - (pennies*1);
}

当我将其全部粘贴到此处时,我为它的格式错误表示歉意。该功能本身运行良好,当返回的更改不包括某个钞票或硬币时,我不知道如何不包括它。谢谢!

添加硬币/纸币价值的测试。

#include <iostream>
#include <vector>
using namespace std;
struct Currency {
    const char * name;
    int value;
};
Currency values[] = {
    {"Twenty", 2000}, 
    {"Tens", 10000},
    {"Dimes", 10},
    {"Pennies",1}
};
int main() {
    int change = 101;
    for(auto& i: values){
        int units = change /i.value;
        if(units)// test that there is some change with these coins/notes
          std::cout << i.name << units << endl;
         change -= units*i.value;
    }
    return 0;
}