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.

Here is my code:
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();
}

I think that the question intends that you use a loop to read in and print out the information about the bills. For instance, the bits like this:

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");

could be in a loop and then you'd only need to write that bit of code once. To get the text to the user right for each bill, you could have a vector of bill names that you pass into the loop, so that the cout statement would look like:

std::cout << "Enter the number of " << bill_names[ i ] << " Dollar bills that you have:";

This same list would enable you to put the output in a similar loop. That should reduce the amount of code that you have by a factor of about 7. Once you have these loops set up, then you can implement them using the range-based for etc.

If you've covered classes in your course yet, you might consider making a class that represents each bill and the number of bills that you have. Something like:

class DollarBill
{
public :
    /* Constructors and accessors here */

private :
    std::string m_strBillName;    /**< The name of the bill type (for printing) */
    unsigned m_iNumericalValue;   /**< The value of this bill type */
    unsigned m_iCount;            /**< The number of bills of this type */
};

You can then make a vector of these and everything would be a bit neater.

We've touched on it a little but not too much yet. Are user made classes more efficient than using the loop?

No. They are just a different way of doing it. And you'd still need a loop anyway. IMO, your technique is just fine without the class. Although, if you notice, every one of your input sections look exactly the same:

cout << "Enter the number of XXX Dollar Bills you have: ";
cin >> bill;
cout << '\n';
bills.push_back(bill);
if (bill<0)
    error("You can't have a negative value.\n");

except for the XXX value. You could use a single loop with the XXX values in an array.

Also note that if you enter a negative number, you
1) display an error message but do not ask for a valid value
2) store and use the negative number anyway

The error kicks the user out of the program so it shouldn't display any results. A loop does sound much simpler though so I'll go fix that. There is a third more complicated part to this.

For this one: I think I could either subtract each denomination as many times as i can as long as it doesn't go below 0. This could probably be made into a for loop. The only problem I see with that is that it would stop whenever total goes below 0. I wrote the code below for now to generalize it even though that won't work right now.

//initialize bill_count
if ((total-100)>=0){ ++bill_count[7]; total-=100;}
cout<<bill_count[7];

Or maybe I could somehow use a Token? I'm not too familiar with making a class.

Modify your program to ask for an integer dollar amount and make
change. For example,
How many dollars? 27
Your change is:
1 20-dollar bill
1 5-dollar bill
1 2-dollar bill
Continue to use an initializer list for the vector of dollar bill denominations,
and use a range-based for statement to traverse the vector. Name your program
hw4pr3.cpp. Note: These C++11 features are available in g++-4.6, but may not
be available on other compilers.

you can also use division and the mod operator to do this. if you want to find out how may 10's you can get out of 87 it would look like this.

int amount = 87;
int tens = amount / 10;  // 87 / 10 = 8
amount = amount % 10;  // 87 % 10 = 7
// now amount holds the reaminder which is 7.

The error kicks the user out of the program so it shouldn't display any results.

OK, I get it now... Wouldn't it be better to ask for the input again rather than waste all the previous inputs?

I think I could either subtract each denomination as many times as i can as long as it doesn't go below 0. This could probably be made into a for loop.

Or maybe I could somehow use a Token? I'm not too familiar with making a class.

Modify your program to ask for an integer dollar amount and make
change.

Or,

as long as the current total is > than the denomination
    add 1 to count
    subtract denomination from total

next denomination

By the way, if you are going to output the quantity immediately, there is no need for the array. Just sayin'.

Ok, that's not hard, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.