我的程序有两个功能。一种可以计算一美元金额的票据类型,另一种可以显示该金额。
我来自Java背景,我不太了解C ++语法,希望能提供一些帮助。
#include <iostream>
#include <iomanip>
using namespace std;
//function declaration
void calcChange(int amount, int* twenties, int* tens, int* fives, int*ones);
void showChange(int amount, int twenties, int tens, int fives, int ones);
int main() {
//Declaration
int amount =0;
//Call to function
calcChange(&twenties, &tens, &fives, &ones);
showChange(twenties,tens,fives,ones);
do {
cout << "Enter amount (or negative to terminate):" << endl;
cin >> amount;
//if loop that ans if 0 is invalid output
if (amount == 0) {
cout << "Invalid dollar amount.\n";
}
//put output here
cout << "Amount " << setw(2) << "Twenties " << setw(2) << "Tens " << setw(5) <<"fives "<< setw(5) <<"Ones "<< endl;
cout << amount << setw(2) << twenties << setw(2) << tens << setw(5) << fives << setw(5) << ones << endl;
}while (amount >= 0);
cout << "Goodbye!";
return 0;
}
//function declarations
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones)
{
while(amount >= 20){
*twenties = amount/20;amount % 20;
amount = *twenties;
twenties++;
}
while(amount >=10){
*tens = amount/10; amount % 10;
amount=*tens;
tens++;
}
while(amount >=5){
*fives = amount/5; amount % 5;
amount = *fives;
fives++;
}
while(amount >=1){
*ones = amount/1; amount % 1;
amount = *ones;
ones++;
}
return;
}
void showChange(int amount, int twenties, int tens, int fives, int ones) {
twenties = twenties;
tens = tens;
fives = fives;
ones = ones;
return;
}
答案 0 :(得分:1)
首先,不确定要使用showChange
函数声明
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones);
函数定义(不是声明)
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones)
{
*twenties = amount / 20; amount %= 20;
*tens = amount / 10; amount %= 10;
*fives = amount / 5; amount %= 5;
*ones = amount / 1; amount %= 1;
return;
}
主要功能
int main() {
int amount = 0;
do {
cout << "Enter amount (or negative to terminate):" << endl;
cin >> amount;
if (amount == 0) {
cout << "Invalid dollar amount.\n";
break;
}
int twenties, tens, fives, ones;
calcChange(amount, &twenties, &tens, &fives, &ones);
cout << "Amount " << setw(2) << "Twenties " << setw(2) << "Tens " << setw(5) <<"fives "<< setw(5) <<"Ones "<< endl;
cout << amount << setw(2) << twenties << setw(2) << tens << setw(5) << fives << setw(5) << ones << endl;
} while (amount >= 0);
cout << "Goodbye!";
return 0;
}