In: Computer Science
(C++)
In a file called pp7c.cpp, write a function called
printMoney that has one parameter, a double, and it prints
this parameter out formatted like a dollar amount with $ and
exactly 2 digits to the right of the decimal. Write a driver that
declares an array of monetary amounts like this:
double amounts[MAX_AMOUNTS];
and uses a while or do while loop to ask the user for monetary
amounts with -1 for the amount as a sentinel value to put values in
the array, amounts. Do not allow the user to enter more than
MAX_AMOUNTS numbers. This loop must count how many numbers are
placed in the amounts array as the array may be partially filled.
After using a do or do while loop to fill the array, write a for
loop to call the printMoney function to print all of the
values in the amounts array. The constant and function declarations
are shown below.
const int MAX_AMOUNTS = 10;
void printMoney( double m );
Explanation:
Here is the code below which has the function printMoney and in the main , it keeps asking for the monetory amount until the person enters -1 as the sentinel value or the amounts exceed 10.
Then a for loop is used to call the function printMoney for each amount in the array.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void printMoney(double m)
{
cout<<fixed<<setprecision(2)<<"$"<<m<<endl;
}
int main()
{
const int MAX_AMOUNTS = 10;
double amounts[MAX_AMOUNTS];
int n = 0;
double amount;
while(n<10)
{
cout<<"Enter monetory amount: ";
cin>>amount;
if(amount==-1)
break;
amounts[n++] = amount;
}
for (int i = 0; i < n; i++) {
printMoney(amounts[i]);
}
return 0;
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!