Basically I wrote a program that asks the user for a number of coins they have. It starts by asking how much of each coin we have. Afterwards we print to the screen and do our calculations. I was able to write a function that takes a coin_type and coin_amount for each coin to reduce repetitive checking. It looked really sloppy before.
My question is, Can I pass an object through a function (by object I mean variable not {{Object obj;}} kinda object) with my coin_amount and coin_type.
// Bradley Latreille 10/28/17
// MoneyProgram
#include "std_lib_facilities.h"
void toString(string, double);
// Global because of function using all objects
// storage for how much each variable is worth numerically
constdouble penny{0.01}; // {} assignment in case of narrowing
constdouble nickel{0.05};
constdouble dime{0.10};
constdouble quarter{0.25};
constdouble half_dollar{0.50};
constdouble dollar{1.0}; // how much each coin is worth
// storage for how many of each variable we have as a user
int pennies = 0;
int nickels = 0;
int dimes = 0;
int quarters = 0;
int half_dollars = 0;
int dollars = 0;
// storage for how much cash we have total
double TotalCash = 0.0;
int main()
/*
A program that asks how much money we have, then tells us
exactly how many of each coin as well as how much total cash
*/
{
// prompt user for data
cout << "How many pennies do you have? ";
cin >> pennies;
cout << "How many nickels do you have? ";
cin >> nickels;
cout << "How many dimes do you have? ";
cin >> dimes;
cout << "How many quarters do you have? ";
cin >> quarters;
cout << "How many half-dollars do you have? ";
cin >> half_dollars;
cout << "How many dollars do you have? ";
cin >> dollars;
// force a space in the program for cuteness
cout << '\n';
// print our results and complete our calculations
toString("penny", pennies);
toString("nickel", nickels);
toString("dime", dimes);
toString("quarter", quarters);
toString("half-dollar", half_dollars);
toString("dollar", dollars);
// print total cash at the end of the program
cout << "\ntotal cash: $" << TotalCash << '\n';
}
// toString - print out our information per coin_type
// and complete our calculations based on coin_type.
// I used a function to kill repetitiveness.
void toString(string coin_type, double coin_amount)
{
if(coin_amount <= 1) { //singular check
cout << "You have " << coin_amount
<< " " << coin_type << '\n';
}else{ // plural check
cout << "You have " << coin_amount
<< " " << coin_type << "'s\n";
}
// Still want to narrow this down if possible to one line
if(coin_type == "penny")
TotalCash += penny * coin_amount;
if(coin_type == "nickel")
TotalCash += nickel * coin_amount;
if(coin_type == "dime")
TotalCash += dime * coin_amount;
if(coin_type == "quarter")
TotalCash += quarter * coin_amount;
if(coin_type == "half-dollar")
TotalCash += half_dollar * coin_amount;
if(coin_type == "dollar")
TotalCash += dollar * coin_amount;
}
As you can see the function toString gets a type, and amount of our coin but Im still having to do a lot of repetitive checking. I want to narrow that coing_type checking to one line if possible. I don't know if its possible. Pointers? Objects? As you can see my problem is when I try to do that last TotalCash += calculation I dont have any coin_type to reference other than my string to use for checking.
<b> Notice: </b>
std_lib_facilities.h is basically just #include <iostream> and #include <string> as well as using namespace std; incase you wanna run the code just add this stuff and you'll compile. Otherwise you can get the std_lib_facilities.h header contents here: http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
I agree with goldenchicken: I like your code as it is.
To shorten it you could use some sort of container which would do the ‘dirty’ job for you, but perhaps you haven’t met them yet.
If the following example doesn’t make any sense for you, it only means what you wrote is a very good code:
Thanks for a result! I was just curious for fun I don't REALLY need it cut to one line. But your example looks interesting I'll definitely take a go at it and learn whats happening! I just started Vectors slightly, but I haven't reached the Vector chapter in my programming book yet.
Here's an alternative approach (which I tend to favour; I do not like the idea of a function called to_string doing two different unrelated things which the name of the function does not suggest. Also, while learning C++, it is a good idea to try and avoid variables at namespace scope).