if/else question

Jun 19, 2011 at 9:52pm
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;

//Display
cout << "Your amount " << amount << " consists of \n" <<
"\t" << numberOfOneDollars << "dollars\n" <<
"\t" << numberOfQuarters << "quarters\n" <<
"\t" << numberOfDimes << "dimes\n" <<
"\t" << numberOfNickels << "nickels\n" <<
"\t" << numberOfPennies << "pennies\n";

return 0;




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....


if (numberOfOneDollars > 0)
"\t" << numberOfOneDollars << "dollars\n"
else
"\t" << numberOfOneDollars << "dollar\n"




Jun 19, 2011 at 10:51pm
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".
Jun 19, 2011 at 11:19pm
could someone please provide a code example on that?
Jun 20, 2011 at 1:35am
Try using the ? : operator. Not sure if this syntax is exactly right but you could do something like:
 
std::cout << dollars << " dollar" << ((dollars == 1) ? " " : " s") << std::endl;
Jun 20, 2011 at 1:56am
Except instead of " " just "" and instead of " s" just "s"...
Jun 20, 2011 at 2:02am
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace 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;
}
Jun 20, 2011 at 2:19am
This works perfect........


//Display
cout << numberOfOneDollars << " dollar" << ((numberOfOneDollars == 1) ? "" : "s") << endl;
cout << numberOfQuarters << " quarter" << ((numberOfQuarters == 1) ? "" : "s") << endl;
cout << numberOfDimes << " dime" << ((numberOfDimes == 1) ? "" : "s") << endl;
cout << numberOfNickels << " nickel" << ((numberOfNickels == 1) ? "" : "s") << endl;
cout << numberOfPennies << " penn" << ((numberOfPennies == 1) ? "y" : "ies") << endl;


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.
Jun 20, 2011 at 2:27am
closed account (S6k9GNh0)
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."

Test it out!
Last edited on Jun 20, 2011 at 2:28am
Jun 20, 2011 at 2:50am
I neevr thought about that but it works! sweet!
Jun 20, 2011 at 3:07am
No problem, and good point L B, I didn't test it but thanks for pointing out the extra space -_-
Jun 21, 2011 at 7:26pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void printMoney(int number, string prefix, string sSuffix, string pSuffix) { //or whatever names you want
   if( number > 0 )
      cout << number << ' ' << prefix << (number == 1 ? sSuffix : pSuffix) << endl;
}

void printMoney(int number, string prefix) {
   printMoney( number, prefix, "", "s" );
}


int main() {
   ...
   printMoney( numberOfOneDollars, "dollar" );
   printMoney( numberOfQuarters, "quarter" );
   printMoney( numberOfDimes, "dime" );
   printMoney( numberOfNickels, "nickel" );
   printMoney( numberOfPennies, "penn", "y", "ies" );
   ...
}
Jun 21, 2011 at 7:39pm
closed account (S6k9GNh0)
You could also declare it inline which might optimize it a bit :P
Jun 22, 2011 at 12:50pm
ok thanks a lot guys!
Topic archived. No new replies allowed.