Problem:
Write a program that prompts the user to enter some number of 1, 2, 5, 10, 20, 50 and 100 dollar bills. Query the user separately for the number of each size dollar.
I think I've completed the code pretty well but the last part of the question states:
Modify your program to use an initializer list for the vector, and use a range-based for statement to traverse the vector. Note: These C++11 features are available in g++-4.6 and Visual Studio 2012, but may not be available on other compilers.
I don't understand what the last part is asking. Hopefully someone here can explain it to me.
Any critique on my code format would also be great!
#include "std_lib_facilities_3.h"
vector<int>bills;
int bill;
int bill_tot(){
int sum = 1*bills[0]+2*bills[1]+5*bills[2]+10*bills[3]+20*bills[4]+50*bills[5]+100*bills[6];
return sum;
}
int main()
try{
cout << "Enter the number of One Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of Two Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of Five Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of Ten Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of Twenty Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of Fifty Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "Enter the number of One Hundred Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.\n");
cout << "You have " << bills[0] <<" One Dollar Bills.\n";
cout << "You have " << bills[1] <<" Two Dollar Bills.\n";
cout << "You have " << bills[2] <<" Five Dollar Bills.\n";
cout << "You have " << bills[3] <<" Ten Dollar Bills.\n";
cout << "You have " << bills[4] <<" Twenty Dollar Bills.\n";
cout << "You have " << bills[5] <<" Fifty Dollar Bills.\n";
cout << "You have " << bills[6] <<" One Hundred Dollar Bills.\n";
cout << "The value of all your Dollar Bills are $" << bill_tot() << endl;
keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << '\n';
keep_window_open();
}