I have a basic assignment and can't get the program right. The assignment is to make a program that displays the minimum amount of banknotes and coins necessary to pay.
#include <iostream>
using namespace std;
int main()
{
int pari;
cin >> pari;
switch (pari)
{
case 1: cout << pari/5000 << "*5000" << endl;
break;
case 2: cout << pari/1000 << "*1000" << endl;
break;
case 3: cout << pari/500 << "*500" << endl;
break;
case 4: cout << pari/100 << "*100" << endl;
break;
case 5: cout << pari/50 << "*50" << endl;
break;
case 6: cout << pari/10 << "*10" << endl;
break;
case 7: cout << pari/5 << "*5" << endl;
break;
case 8: cout << pari/2 << "*2" << endl;
break;
case 9: cout << pari/1 << "*1" << endl;
break;
default: cout << "WRONG";
}
return 0;
}
For example:
Input:
54321
Output:
10x5000
4x1000
0x500
3x100
0x50
2x10
0x5
0x2
1x1
I tried with switch
case, with if
statements, but nothing works.
CodePudding user response:
To get the kind of output you have shown, use logic that looks more like this instead:
#include <iostream>
using namespace std;
int main()
{
int pari;
cin >> pari;
cout << pari/5000 << "*5000" << endl;
pari %= 5000;
cout << pari/1000 << "*1000" << endl;
pari %= 1000;
cout << pari/500 << "*500" << endl;
pari %= 500;
cout << pari/100 << "*100" << endl;
pari %= 100;
cout << pari/50 << "*50" << endl;
pari %= 50;
cout << pari/10 << "*10" << endl;
pari %= 10;
cout << pari/5 << "*5" << endl;
pari %= 5;
cout << pari/2 << "*2" << endl;
pari %= 2;
cout << pari/1 << "*1" << endl;
return 0;
}
Which can be simplified if you put the banknotes in an array and loop through it, eg:
#include <iostream>
using namespace std;
int main()
{
const int bankNotes[] = {5000, 1000, 500, 100, 50, 10, 5, 2, 1};
const int numBankNotes = sizeof(bankNotes)/sizeof(bankNotes[0]);
int pari;
cin >> pari;
for (int i = 0; i < numBankNotes; i) {
cout << pari/bankNotes[i] << "*" << bankNotes[i] << endl;
pari %= bankNotes[i];
}
return 0;
}
CodePudding user response:
I have written several versions for you here. Hopefully it will help you to understand the procedure.
- Version 1
This is a navie version. This is how we would do it if we were doing it by hand.
int main()
{
int input_value = 0;
std::cin >> input_value; // First we get the input.
// We start with the highest value banknote.
int value = input_value;
int const number_of_5000_notes = value / 5000; // How many of these notes do
// we need?
value = value % 5000; // Now calculate the rest.
int const number_of_1000_notes = value / 1000; // How many of these notes do
// we need?
value = value % 1000; // Now calculate the rest.
int const number_of_500_notes = value / 500;
value = value % 500;
int const number_of_100_notes = value / 100;
value = value % 100;
int const number_of_50_notes = value / 50;
value = value % 50;
int const number_of_10_notes = value / 10;
value = value % 10;
int const number_of_5_notes = value / 5;
value = value % 5;
int const number_of_2_notes = value / 2;
value = value % 2;
int const number_of_1_notes = value;
// At the end we write the output
std::cout << "Input: " << input_value << std::endl;
std::cout << "Output:" << std::endl;
std::cout << number_of_5000_notes << " x 5000" << std::endl;
std::cout << number_of_1000_notes << " x 1000" << std::endl;
std::cout << number_of_500_notes << " x 500" << std::endl;
std::cout << number_of_100_notes << " x 100" << std::endl;
std::cout << number_of_50_notes << " x 50" << std::endl;
std::cout << number_of_10_notes << " x 10" << std::endl;
std::cout << number_of_5_notes << " x 5" << std::endl;
std::cout << number_of_2_notes << " x 2" << std::endl;
std::cout << number_of_1_notes << " x 1" << std::endl;
return 0;
}
- Version 2
This is a more advanced version
int main()
{
int value = 0;
std::cin >> value; // Get input
// Check input
if (value == 0)
{
std::cout << "No value or 0 has been entered";
return 0;
}
// Output on the fly
std::cout << "Input: " << value << std::endl;
std::cout << "Output:" << std::endl;
// loop over a sorted list of banknotes.
for (auto note_value_ent : {5000, 1000, 500, 100, 50, 10, 5, 2, 1})
{
int const number_of_notes = value / note_value_ent;
value %= note_value_ent;
std::cout << number_of_notes << " x " << note_value_ent << std::endl;
}
return 0;
}
Both versions give the same result (except in the case of an invalid entry).