gpt4 book ai didi

c++ - 如何可以根据需要多次执行我的功能?

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:12 24 4
gpt4 key购买 nike

我有一个学校项目和一个简单的自动售货机。我想知道如何根据用户的需要多次执行我的功能,同时跟踪机器内部的可用变化。

#include <iostream>
#include <string>
using namespace std;
void changecalculator (int purchaseAmount, int& Qav, int& Dav, int& Nav)
{
int QBack ,DBack ,NBack ;
int chaneBack = 100 - purchaseAmount ;

if (purchaseAmount %5 == 0)
{
cout << "You entered a purchase amount of " << purchaseAmount << " cents." <<endl;
cout <<endl;
QBack = std::min(chaneBack / 25, Qav) ;
chaneBack -= QBack * 25 ;
DBack = std::min(chaneBack/10, Dav);
chaneBack -= DBack* 10;
NBack = std::min(chaneBack/ 5, Nav);
chaneBack = QBack*25 + NBack*10 + DBack*5 ;
int coinsNum = QBack + DBack + NBack;
cout <<"Your change of " <<chaneBack <<" cents is given as " <<QBack <<" Q, " <<DBack <<" D,and " <<NBack <<" N." <<endl;
cout << "The value of your " <<coinsNum <<" coins adds up to " <<chaneBack <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
Qav -= QBack;
Dav -= DBack;
Nav -= NBack;
cout << "Quarters available: " <<Qav <<endl;
cout << "Dimes available: " <<Dav <<endl;
cout << "Nickels available: " <<Nav <<endl;



}
else
{
cout << "Unable to process an invalid purchase amout of " <<purchaseAmount <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;

}
}
int main()
{
//set the variables
int Qav=5 ;
int Dav=5 ;
int Nav=5 ;
int purchaseAmount ;
string answer;
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;

changecalculator (purchaseAmount, Qav, Dav, Nav);
cout << "Process again (y/n)?";
cin >> answer;
}

最佳答案

类似于 do{} while() 循环?

do 
{
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;

changecalculator (purchaseAmount, Qav, Dav, Nav);
//get userResponse;
}
while(userResponse =='y' || userResponse =='Y');

关于c++ - 如何可以根据需要多次执行我的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25842696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com