提问人:Loading Screen 提问时间:11/4/2023 最后编辑:Loading Screen 更新时间:11/4/2023 访问量:103
如何在 void 函数中停止从重置到 0 的总运行?
How do I stop running total from resetting to 0 in void function?
问:
我有一个任务,我应该做一个餐厅菜单。菜单必须循环,直到用户输入 4 才能获取账单。我应该使用一个函数来显示菜单,另一个函数来计算运行总额并在用户完成订购时显示账单。
问题是,我必须在使用它之前进行初始化,并且我不知道将其放在哪里以阻止它重置为初始化。最初,我的计算函数 , , 是这样设置的:runningTotal
cost()
void cost(const double ITEM_PRICE, int amtOfItemOrdered, bool isDoneOrdering) {
// initializing runningTotal and declaring other variables...
double runningTotal = 0, tip, finalBill;
// if statement so bill only displays when order is finished...
if (isDoneOrdering == false)
// calculate running total...
runningTotal += (ITEM_PRICE * amtOfItemOrdered);
else {
// when order finishes, calculate and display bill...
tip = (runningTotal * 0.20);
finalBill = (runningTotal + tip);
cout << "\nFood total: " << runningTotal <<
"\nTip: " << tip <<
"\nFinal Bill: " << finalBill;
}
我认为这会导致每次执行函数时重置为 0。当我运行程序,订购一些东西,然后按 4 查看账单时,账单中的所有内容都显示为 0。下面是输出的示例:runningTotal
Food total: 0
Tip: 0
Final Bill: 0
我认为像这样移出函数可能会有所帮助:runningTotal
cost()
int main() {
// initializing runningTotal with 0 in main...
double runningTotal = 0;
// other variables...
const double ITEM1_PRICE = 2.50, ITEM2_PRICE = 3.50, ITEM3_PRICE = 4.50;
int itemOrdered, amtOfItemOrdered;
bool isDoneOrdering = false;
// loop menu choice until order is finished...
do {
displayMenu(ITEM1_PRICE, ITEM2_PRICE, ITEM3_PRICE);
cout << "Enter your choice: ";
cin >> itemOrdered;
// don't ask "how many" when done ordering...
if (itemOrdered != 4) {
cout << "\nHow many? ";
cin >> amtOfItemOrdered;
}
switch (itemOrdered) {
case 1:
// passing runningTotal to cost() so cost() can use it...
cost(ITEM1_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 2:
cost(ITEM2_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 3:
cost(ITEM3_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 4:
isDoneOrdering = true;
cost(0, 0, isDoneOrdering, runningTotal);
// zeroes are here because cost() asks for parameters,
// but we're done adding to running total...
break;
default:
0; // empty placeholder statement...
}
} while (isDoneOrdering == false);
}
但这有同样的结果。我认为这里发生的事情是 in 与 in 不同,并且 中的值 0 被传递给 in ,这无论如何都会重置它?runningTotal
main
runningTotal
cost()
main
runningTotal
runningTotal
cost()
我考虑过为 、 和 和 做一个返回语句,并在函数之外显示账单,但我认为赋值希望我坚持使用 void 函数。runningTotal
tip
finalBill
刚刚测试初始化为 1 in ,账单显示以下内容:runningTotal
main
Food total: 1
Tip: 0.2
Final Bill: 1.2
所以我确实认为初始化是这里的问题。任何帮助将不胜感激。谢谢。
答:
由于这是 C++,因此使用类是有意义的。
考虑一个类来封装成本计算,以及一个单独的类来存储菜单。
constexpr double TIP_MARGIN = 0.2;
class BillCalculator
{
double m_RunningTotal = 0.0;
public:
void AddCost(const double price, int count)
{
m_RunningTotal += price * count;
}
double CalculateTip() const
{
return m_RunningTotal * TIP_MARGIN;
}
double CalculateTotalBill() const
{
return GetRunningTotal() + CalculateTip();
}
double GetRunningTotal() const
{
return m_RunningTotal;
}
};
在 中实例化两个类。拥有一个仅专注于 I/O 的函数将使代码更易于理解。main
如果需要传递所创建任一类的实例,请通过引用传递它。
在函数作用域中定义和初始化的变量将保留在函数中。 在您的代码中,main() 函数中的 runningTotal 和 cost() 函数中的 runningTotal 因此在内存中具有不同的内存空间(尽管它们具有相同的名称)。
我注意到您想在 cost() 函数中访问变量 runningTotal 的值并修改变量。就像 Peter 评论的那样,您可以通过 2 种方式做到这一点:
- 通过参数或指针将 runningTotal 传递到函数 cost() 中,也就是说,您通过标识该变量的内存空间来为函数提供变量。
例如:
按参数:
void cost(your variables..., double& runningTotal) //by parameter.
//calling the function:
cost(your variables..., runningTotal); //it's the same as calling normal function
//using the variable
//you can use its the same as normal.
//note that, any changes made in this function will affect the variable outside, so be carefull
通过指针:
void cost(your varialbe..., double* runningTotal) //by pointer.
//calling the funtion
cost(your variables...,&runningTotal);
//using the variable
//As pointer is simply the address of the variable itself
//you'll have to dereference it before using by adding a * before the variable
*runningTotal = *runningTotal +1;
//note that, changes here also affect the variable outside.
通过使用文件作用域(全局作用域)的变量:
//the variable double runningTotal; void cost(your variable....){//your code} int main(){ //your code}
请注意,以这种方式进行更改仍然会影响外部变量
评论
double cost(your variables)
calculate_running_total
评论
double intermediateTotal
double
runningTotal
runningTotal
runningTotal
runningTotal
cost()
cost
double
double &