Read from the user the maximum number of rolls of toilet paper that may be ordered. Require the maximum number of rools that may be ordered to be an integer from 1 to 10, inclusive. If the user enters a number that is out of range, make them try again until they enter an acceptable number.
Similarly make the user enter the price of one roll of toilet paper as a decimal number of dollars and cents from ten cents(0.10) to three dollars(3.00) inclusive.
Print a table showing the number of rolls in the table for all number of rolls from 1 to the maximum number that the user specified. Each row in the table shows the number of rolls and the price for that number of rolls.
Example:
User enters maximum number of rolls as 48; make them try again.
User enters maximum number of rolls as 4; that is acceptable.
User enters the price per roll as 14.10; make them try again.
User enters the price per roll as -2.70; make them try again.
User enters the price per roll as 0.10; which is acceptable.
Print:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int max_num;
double price;
while (true) {
cout << "Enter maximum number of rolls: ";
cin >> max_num;
if ( max_num >= 1 && max_num <= 10) {
break;
}
cout << "The number must bee between 1 and 10" << endl;
}
while (true) {
cout << "Enter the price per roll: ";
cin >> price;
if ( price >= 0.10 && price <= 3.00) {
break;
}
cout << "The number must bee between 0.10 and 3.00" << endl;
}
cout << "# rolls total price" << endl;
cout << fixed;
for (int i=1; i<=max_num; i++) {
double tot_price = i*price;
cout << " " << i << " $" << setprecision(2) << setw(5) << tot_price << endl;
}
return 0;
}
Comments
Leave a comment