0

fairly fresh coder that is learning to create a program that will output the fewest coins possible from any given cent input between 0 and 99. I don't need to add any limits to this code which is why you don't see anything limiting between 0 and 99.

Here is what I have, but I can't seem to figure out how to properly use a modulus to carry over the leftover number from the previous calculation.

I appreciate any advice you can offer! I think I am pretty close, but am at a mental wall with the % modulus. I know I can calculate the previous number using long arithmetic but I would like to figure out how to make it shorter with the % modifier.

#include <iostream>
using namespace std;

int main()
{
int cents; 
const int quarter = 25; 
const int dime = 10;
const int nickel = 5;
const int penny = 1;

// Get the amount of cents
cout << "Please enter an amount in cents less than a dollar." << endl;
cin >> cents;

// Calculate how many of each coin you can get from cent input
int Q = cents % quarter;
int D = Q % dime;
int N = D % nickel;
int P = N % penny;

// Display the coins you can get from cent input
cout << "Your change will be:" << endl;
cout << "Q: " << Q << endl;
cout << "D: " << D << endl;
cout << "N: " << N << endl;
cout << "P: " << P << endl;

return 0;
}
n0mad
  • 3
  • 1
  • 2
  • 1
    The first line `int Q = cents % quarter;` seems to assign to `Q` how many cents are left over after you account for quarters, but you never calculate the number of quarters. QDNP all contain remainders, and you display them as if they were coin counts. You simply never calculated how many coins you need. – François Andrieux Oct 03 '17 at 20:48
  • [Best way to get integer division and remainder](https://stackoverflow.com/q/7070346/1460794) – wally Oct 03 '17 at 21:02
  • missing $1 and $0.50 coins? – 2785528 Oct 03 '17 at 21:33

4 Answers4

2

% returns the remainder of the division, not the rounded figure. So it is assigning to Q the remainder of the divison. Id suggest you first calculate the number of X type coin, and then pass the remainder on to the next calculation. Something like

int Q = cents / quarter;
int D = (cents%quarter) / dime;

And so on

Ryan Turnbull
  • 3,766
  • 1
  • 25
  • 35
  • Thats okay, welcome to Stack Overflow! see [How to accept an answer](https://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow) – Ryan Turnbull Oct 03 '17 at 20:53
  • Thank you for the input! Would the nickel output then be int N = (cents%quarter%dime) / nickel; ??? – n0mad Oct 03 '17 at 20:54
  • 1
    Sure would be, you could consider keeping some sort of 'int remainer' but what youve suggested works :-) – Ryan Turnbull Oct 03 '17 at 20:57
  • I would use this instead: `int Q = cents / quarter; cents %= quarter; int D = cents / dime; cents %= dime; int N = cents / nickel; cents %= nickel; int P = cents;` – Remy Lebeau Oct 03 '17 at 21:53
2

Here is another solution using std::div and C++17 structured bindings:

#include <iostream>
#include <string>
#include <cstdlib>
#include <utility>
#include <vector>

int main()
{
    std::vector<std::pair<std::string, int>> coins {
        {"quarter", 25},
        {"dime", 10},
        {"nickel", 5},
        {"penny", 1} };

    std::cout << "Please enter an amount in cents less than a dollar.\n";
    int cents;
    std::cin >> cents;

    std::cout << "Your change will be:\n";
    for(const auto& [coin_name, value] : coins) {
        auto[coin_count, remainder] = std::div(cents, value);
        cents = remainder;
        std::cout << coin_name << "\t: " << coin_count << '\n';
    }
}
wally
  • 10,717
  • 5
  • 39
  • 72
1

For total number of each coin type:

int Q = cents / quarter;
int D = cents / dime;
int N = cents / nickel;
int P = cents / penny;

For least amount of coins

int Q = cents / quarter; cents %= quarter; 
int D = cents / dime; cents %= dime; 
int N = cents / nickel; cents %= nickel; 
int P = cents;
Peter
  • 1,006
  • 7
  • 15
  • For least amount of coins, I would use this instead: `int Q = cents / quarter; cents %= quarter; int D = cents / dime; cents %= dime; int N = cents / nickel; cents %= nickel; int P = cents;` Much cleaner – Remy Lebeau Oct 03 '17 at 21:55
1

I don't quite understand what you want but as far as I understand, this is what I beleive you are trying to do

int cointypes[4]={25,10,5,1};
int coinnumber[4];
int temp=cents;
for(int i=0;i<=3;i++)
{
 coinnumber[i]=temp/cointypes[i];
 temp=temp%cointypes[i];

}
cout << "Your change will be:" << endl;
cout << "Q: " << coinnumber[0] << endl;
cout << "D: " <<coinnumber[1] << endl;
cout << "N: " << coinnumber[2]<< endl;
cout << "P: " << coinnumber[3]<< endl;
Reema Amro
  • 11
  • 4