Can this code be shortened?
Mar 29, 2013 at 3:23am UTC
Hi I am a beginner and I am just wondering if this code can be shortened and how it would be done. Its not an important project or anything I was just messing around.
There is a float for the price of whatever, you enter the amount of money you have and then it will print out what change you would receive. Please note that I am in Australia and we have $1 coins here haha.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
int main(int argc, const char * argv[])
{
float price = 96.5;
float moneyin;
int hundreds;
int fifties;
int twenties;
int tens;
int fives;
int twos;
int ones;
int fiftyc;
int twentyc;
int tenc;
int fivec;
std::cout << "The price is $" << price << std::endl << "Insert cash!\n" ;
std::cin >> moneyin;
if (moneyin < price) {
std::cout << "Not enough\n" ;
}
else {
moneyin = moneyin-price;
hundreds = moneyin / 100; moneyin = moneyin - hundreds*100;
fifties = moneyin / 50; moneyin = moneyin - fifties*50;
twenties = moneyin / 20; moneyin = moneyin - twenties*20;
tens = moneyin / 10; moneyin = moneyin - tens*10;
fives = moneyin / 5; moneyin = moneyin - fives*5;
twos = moneyin / 2; moneyin = moneyin - twos*2;
ones = moneyin / 1; moneyin = moneyin - ones*1;
fiftyc = moneyin / 0.5; moneyin = moneyin - fiftyc*0.5;
twentyc = moneyin / 0.2; moneyin = moneyin - twentyc*0.2;
tenc = moneyin / 0.1; moneyin = moneyin - tenc*0.1;
fivec = moneyin / 0.05; moneyin = moneyin - fivec*0.05;
std::cout << hundreds << " $100 note/s:" << std::endl;
std::cout << fifties << " $50 note/s:" << std::endl;
std::cout << twenties << " $20 note/s:" << std::endl;
std::cout << tens << " $10 note/s:" << std::endl;
std::cout << fives << " $5 note/s:" << std::endl;
std::cout << twos << " $2 coin/s:" << std::endl;
std::cout << ones << " $1 coin/s:" << std::endl;
std::cout << fiftyc << " 50c coin/s:" << std::endl;
std::cout << twentyc << " 20c coin/s:" << std::endl;
std::cout << tenc << " 10c coin/s:" << std::endl;
std::cout << fivec << " 5c coin/s:" << std::endl;
}
return 0;
}
Last edited on Mar 29, 2013 at 3:25am UTC
Mar 29, 2013 at 3:46am UTC
Here's one method using arrays:
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 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
int main(int argc, const char * argv[])
{
float price = 96.5;
float moneyin;
const int NUMVALUES = 11;
float values[] = {100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05};
float count[NUMVALUES] = {0};
std::cout << "The price is $" << price << std::endl << "Insert cash!\n" ;
std::cin >> moneyin;
if (moneyin < price)
{
std::cout << "Not enough\n" ;
}
else
{
float changedue = moneyin - price;
for (int i = 0; i < NUMVALUES && changedue > 0; i++)
{
while (changedue >= values[i])
{
changedue -= values[i];
count[i]++;
}
if (count[i] > 0)
{
if (values[i] >= 5)
std::cout << count[i] << " $" << values[i] << " note(s)" << std::endl;
else if (values[i] >= 1)
std::cout << count[i] << " $" << values[i] << " coin(s)" << std::endl;
else
std::cout << count[i] << " " << values[i] * 100 << "c coin(s)" << std::endl;
}
}
}
return 0;
}
If you don't have pennies ($0.01 coins), you could wind up paying more than change due.
Last edited on Mar 29, 2013 at 3:47am UTC
Topic archived. No new replies allowed.