I put together a code to break down the monetary units of an inputted number. For example you input 11.56 it returns 11 dollars, 2 quarters, 1 nickel, 1 penny.
Here is the code
#include <iostream>
using namespace std;
int main()
{
//Receive amount
cout << " Enter an amount in double: ";
double amount;
cin >> amount;
int remainingAmount = static_cast<int>(amount * 100);
//Find dollar amount
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
//Find number of quarters
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
//Find number of dimes
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
//Find number of nickels
int numberOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
//Find number of pennies
int numberOfPennies = remainingAmount / 1;
My question is, I am trying to write in an if/else statement so if the numberOfOneDollars is > 1 it couts "dollars" and if numberOfOneDollars ==1 it couts "dollar." I then want to if/else all of the outputs (dimes, nickels etc) so they use singular words for single units and plural words for for more than one unit as well as display only the non-zero denominations. I am new to if/elses and booleans. Could some one please help me out and tell me what Im doing wrong.
This is what I have so far....
You can make a Pluralize function and just pass it the number, and have it return "s" if it is != 1, and nothing otherwise. It will work in this case because the plural and non-plural versions of all the words you are using differ only by an s, and don't need another thing like es:
Dollar(s)
Half-Dollar Coin(s)
Quarter(s)
Dime(s)
Nickel(s)
The special case is with:
Penny/Pennies
but it can simply be another function that returns either "y" or "ies" and you can append it to "Penn".
#include <iostream>
#include <string>
usingnamespace std;
/**
* @param word - the word to return
* @param count
* @return the word with a possible 's' appended to it if count is not 1
*/
string pluralizeWord(string word, int count) {
if( count != 1 )
word.append("s"); //of course this will not work correctly for weird words like "man", or "sheep"
return word;
}
//A test program
int main() {
string theWord;
int num;
cout << "Enter a word: ";
cin >> word;
cout << "How many are there: ";
cin >> num;
cout << "You have " << num << ' ' << pluralizeWord(theWord, num) << endl;
return 0;
}
What would I do if I wanted to display nothing if the attribute is 0? Say it returns 0 dimes, I want it to skip dimes and for example display 2quarters, 1 nickel 3 pennies.
In the English language, 0 is treated with plurality. Therefore, those statements only add plurality to the word if it does not equal 1. So, even if it were 0, it would still be correct. ;)
"I have 0 dollars."
"I have 1 dollar."
"I have 2 dollars."
jkretzer, since you are doing the same operation over and over it would make you program look cleaner and save your upgrade/debugging time if you made a function to do it for you (in my opinion.) Just look at the similarities and differences in your lines of code.