So my program and everything works. I just need to be able to allow the user to rerun the program with char y, but it just ends. Do you guys have any ideas? I can't use :
for(;;)
while(1)
while(true)
do{//code}while(1);
#include <iostream>
usingnamespace std;
int main()
{
char choice = 'y';
int QUARTERS = 25;
int DIMES = 10;
int NICKELS = 5;
int PENNIES = 1;
int change, numQuarters, numDimes, numNickels, numPennies;
{
cout << " Enter an amount : ";
cin >> change;
if (change > 0)
{
numQuarters = change / QUARTERS;
change -= numQuarters * QUARTERS;
numDimes = change / DIMES;
change -= numDimes * DIMES;
numNickels = change / NICKELS;
change -= numNickels * NICKELS;
numPennies = change / PENNIES;
change -= numPennies * PENNIES;
if (numQuarters != 0) // this represent the amount of quarter there are
if (numQuarters > 1)
cout << " There are Quarters " << numQuarters;
else
cout << " There is Quarter " << numQuarters << endl;
cout << endl;
if (numDimes != 0) // this represent the amount of dimes there are
if (numDimes>1)
cout << " There are Dimes " << numDimes;
else
cout << " There is Dime " << numDimes << endl;
cout << endl;
if (numNickels != 0) // this represent the amount of nickels there are
if (numNickels > 1)
cout << " There are Nickels " << numNickels;
else
cout << " There is Nickel " << numNickels;
cout << endl;
if (numPennies != 0) // this represent the amount of pennies there are
if (numPennies > 1)
cout << " There are Pennies " << numPennies;
else
cout << " There is Penny " << numPennies;
cout << endl;
}
else
cout << "Invalid amount" << endl;
cout << "Would you like to try another number? press(y). Anything else will quit : ";
cin >> choice;
}
system("pause");
return 0;
}.
Output:
Enter an Amount:13
There is dime 1
There are pennies 3
Would you like to try another number? press(y). Anything else will quit: y
press any key to continue...
No where does it say you're not allowed to use loops. It looks like he just doesn't want you using infinite loops.
Can I see the exact text of the requirements?
No global variables
No labels or go-to statements
No infinite loops, examples include:
for(;;)
while(1)
while(true)
do{//code}while(1);
No break statements to exit loops
If you violate any of these restrictions, you will automatically get a score of ZERO!
No. Somewhere in the loop you will be able to set the value of choice, therefore it is not an endless loop. An endless loop has no way to exit the loop other than break, exit(), or return.
It still does infinite loops when I inputted the code.
Show the code...
jlb wrote:
An endless loop has no way to exit the loop other than break, exit(), or return
Heh, technically the requirements didn't mention return statements, depends on how literal we're being about the term "break". It also doesn't mention using undefined behavior, or using OS calls to end the loop as well. So many possibilities!
Your indentation is super misleading. Remove the closing curly brace on line 10. And you'll then need to fix and remove probably another curly brace somewhere else in the code.
#include <iostream>
usingnamespace std;
int main()
{
char choice = 'y';
int QUARTERS = 25;
int DIMES = 10;
int NICKELS = 5;
int PENNIES = 1;
int change, numQuarters, numDimes, numNickels, numPennies;
while (choice == 'y')
{
cout << " Enter an amount : ";
cin >> change;
if (change > 0)
{
numQuarters = change / QUARTERS;
change -= numQuarters * QUARTERS;
numDimes = change / DIMES;
change -= numDimes * DIMES;
numNickels = change / NICKELS;
change -= numNickels * NICKELS;
numPennies = change / PENNIES;
change -= numPennies * PENNIES;
if (numQuarters != 0) // this represent the amount of quarter there are
if (numQuarters > 1)
cout << " There are Quarters " << numQuarters;
else
cout << " There is Quarter " << numQuarters << endl;
cout << endl;
if (numDimes != 0) // this represent the amount of dimes there are
if (numDimes > 1)
cout << " There are Dimes " << numDimes;
else
cout << " There is Dime " << numDimes << endl;
cout << endl;
if (numNickels != 0) // this represent the amount of nickels there are
if (numNickels > 1)
cout << " There are Nickels " << numNickels;
else
cout << " There is Nickel " << numNickels;
cout << endl;
if (numPennies != 0) // this represent the amount of pennies there are
if (numPennies > 1)
cout << " There are Pennies " << numPennies;
else
cout << " There is Penny " << numPennies;
cout << endl;
}
else
cout << "Invalid amount" << endl;
cout << "Would you like to try another number? press(y). Anything else will quit : ";
cin >> choice;
}
system("pause");
return 0;
}
D:\code\cplusplus243597>make main
g++ main.cpp -o main
D:\code\cplusplus243597>main
Enter an amount : 42
There is Quarter 1
There is Dime 1
There is Nickel 1
There are Pennies 2
Would you like to try another number? press(y). Anything else will quit : y
Enter an amount : 1791
There are Quarters 71
There is Dime 1
There is Nickel 1
There is Penny 1
Would you like to try another number? press(y). Anything else will quit : n
Press any key to continue . . .
It is, however, possible for it to become an infinite loop.... if std::cin fails.... I figured this was out of the scope of the problem. I will provide an example that can prevent this.
In a nutshell, it resets the std::cin stream to make it usable again.
std::cin.clear(); resets the failure state of the stream, allowing it to take in input again. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); Ignores (clears out from the buffer) everything the user entered the last time before they pressed Enter/Return.
Edit: Oh, and checking if (cin >> choice) means "were we correctly able to put the value the user entered into the given variable".