关于函数调用,我的程序没有读取我的其他函数。为什么?

Regarding function calls, my program isn't reading my other functions. Why?

本文关键字:我的 函数 其他 为什么 读取 函数调用 程序      更新时间:2023-10-16
using namespace std;
int amount_total(int amount_paid, int& amountleft), quarters(int&
amountleft), dimes(int& amountleft), pennies(int& amountleft);
int amount_total(int amount_paid, int& amountleft)
{
const int TOTALVALUE = 99;
cout << "You have bought a candy bar that's 99 cents. n"
<< "Please insert the amount paid. " << endl;
cin >> amount_paid;
amountleft = TOTALVALUE - amount_paid;
cout << "You have to pay : " << amountleft << " cents. n" << endl;
return(0);
}
int quarters(int& amountleft)
{
const int QUARTERS = 25;
int total, count_Quarters;
count_Quarters = 0;
while (amountleft > 0 || amountleft >= QUARTERS)
{
total = amountleft - QUARTERS;
count_Quarters++;
}
return(count_Quarters);
}
int dimes(int& amountleft)
{
const int DIMES = 10, QUARTERS = 25;
int total, count_Dimes;
count_Dimes = 0;
while (amountleft > 0 || amountleft <= QUARTERS)
{
total = amountleft - DIMES;
count_Dimes++;
}
return(count_Dimes);
}
int pennies(int& amountleft)
{
const int PENNIES = 1, DIMES = 10;
int total, count_Pennies;
count_Pennies = 0;
while (amountleft >= 0 || amountleft <= DIMES)
{
total = amountleft - PENNIES;
count_Pennies++;
}
return(count_Pennies);
}
int main()
{
int amount_paid, amountleft, Count_Quarters,
Count_Dimes, Count_Pennies, total_amount;
total_amount = amount_total(amount_paid, amountleft);
Count_Quarters = quarters(amountleft);
Count_Dimes = dimes(amountleft);
Count_Pennies = pennies(amountleft);
cout << "You'll get : " << Count_Quarters << " quarters, " <<
Count_Dimes << " dimes, and " << Count_Pennies << " pennies. n"
<< endl;
return 0;
}

//样品运行:

你买了一块99美分的巧克力棒。请插入已支付的金额。36你必须支付:63美分。

我最初的计划是让程序运行,main只运行函数,函数会返回变量,但它没有这样做,它只运行第一个函数

正如rex在评论中提到的那样,所有while循环都是无限的,因此永远不会停止循环。你的程序在你的第二个功能"季度"内冻结。看看事情是如何发生的:当你离开函数amount_total时,它返回0并将其分配给"total_amount"变量,然后它的"amountleft"变量被分配了等于"TOTALVALUE–amount_paid"的值。将此值(在"amountleft"变量内)传递给第二个函数"quarters"。在您的示例中,"amountlef"变量等于63,它大于"0",也大于QUARTERS(因为它是25)。因此,你在这里开始循环,循环的每次迭代,你的变量"amountleft"都保持相同的值,即63,因为它在while循环中没有变化,要离开循环,你必须更改"amountelft"或设置任何类型的中断。为了让它更显眼,可以在你的循环中设置"cout",看看那里发生了什么。像这样,例如
21 int quarters( int& amountleft ) {                                                                                                  
22         const int QUARTERS = 25;                                                                                                   
23         int total, count_Quarters;                                                                                                 
24         count_Quarters = 0;                                                                                                        
25         cout << "before while-loop" << endl;                                                                                       
26         while( amountleft > 0 || amountleft >= QUARTERS ) {
27           cout << "count_Quarters = " << count_Quarters  << endl;
28           cout << "amountleft  = " << amountleft  << endl;                                                                         
29                 total = amountleft - QUARTERS;                                                                                     
30                                                                                                                                    
31                 count_Quarters++;                                                                                                  
32         }                                                                                                                                                                                                                                                   
33         cout << "after while-loop" << endl;                                                                                        
34         return count_Quarters;                                                                                                     
35 }

请记住,由于您还没有定义"total"变量,它可以包含任何垃圾,但通常不会。无论如何,在使用变量之前,将任何值(例如0)分配给变量是很好的。