C++: So, the code here is working but, i would like to add in a yes and no input
ID: 3874838 • Letter: C
Question
C++:
So, the code here is working but, i would like to add in a yes and no input string or loop. Open to any suggestions. Thanks for reading! *NOTE: the yes and no command begins at line 66.*
#include <iostream>
using namespace std;
int main()
{
// Constants
const int QUARTERS = 25; // Quarter value
const int DIMES = 10; // Dime value
const int NICKELS = 5; // Nickle value
const int PENNIES = 1; // Penny value
// Variables
int change; // To hold value of change
int numQuarters; // To hold value of quarter
int numDimes; // To hold value of dime
int numNickels; // To hold value of nickel
int numPennies; // To hold value of penny
cout << "Enter a positive number of coins, >= 0 ";
cin >> change;
if (change > 0)
{
cout << "The following is a breakdown of the least possible number of coins" << endl;
// Calculate least possible number of coins
numQuarters = change / QUARTERS;
change -= numQuarters * QUARTERS;
numDimes = change / DIMES;
change -= numDimes * DIMES;
numNickels = change / NICKELS;
change -= numNickels * NICKELS;
numPennies = change / PENNIES;
change -= numPennies * PENNIES;
// Display breakdown of least possible number of coins
if (numQuarters != 0 && numQuarters == 1)
cout << "There is " << numQuarters << " Quarter ";
if (numQuarters != 0 && numQuarters > 1)
cout << "There are " << numQuarters << " Quarters ";
if (numDimes != 0 && numDimes == 1)
cout << "There is " << numDimes << " Dime ";
if (numDimes != 0 && numDimes > 1)
cout << "There are " << numDimes << " Dimes ";
if (numNickels != 0 && numNickels == 1)
cout << "There is " << numNickels << " Nickel ";
if (numNickels != 0 && numNickels > 1)
cout << "There are " << numNickels << " Nickels ";
if (numPennies != 0 && numPennies == 1)
cout << "There is " << numPennies << " Penny ";
if (numPennies != 0 && numPennies > 1)
cout << "There are " << numPennies << " Pennies ";
cout << "Would you like to try another number? (y/Y). Anything else will quit " << endl;
}
else
{
cout << "You entered zero, there are no coins for zero!" << endl;
}
system("pause");
return 0;
}
Explanation / Answer
You can get a character variable having y or n depending on your choice and use a while loop till the variable has n assigned to it. Try it and comment if you still have problem. Happy learning :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.