Loop problems

Page 1 of 1

2 Replies - 1794 Views - Last Post: 22 January 2009 - 10:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=82211&amp;s=9e1935a3f0cc22426d558cd697f90065&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 MntMan

Reputation: 0
• Posts: 32
• Joined: 22-January 09

Loop problems

Posted 22 January 2009 - 08:23 AM

Hello, I have been banging my head against the wall for 2 weeks on this thing. I think I am looking at it wrong, maybe the loop is not the best thing to use in this situation. Anyway here it is, the program is supposed to give change in Quarters, Dimes and Pennies for an amount between 1 and 99 cents. I have giving quarters and pennies or dimes and pennies, but not all three at the same time. This is my third rewrite of the loop and still nothing. Any guidance would be much appreciated. Thanks.

```/* This program will compute the type of coins given/left as change.
Written by: Charles Fetterolf
Date: 13 Jan 2009
*/
#include <iostream>
#include <cstdlib>
using namespace std;
//Prototype Statements
void computeCoin (int coinValue, int& number, int& amountLeft);
//Global Declarations
int quarters, dimes, penny, change;
int main(void)
{
//Local Declarations
char ans;
//Statements
do
{
cout << "\nThis program will tell you how many and what type of coins can be given as change. \n";
cout << "Enter an amount from 1 to 99: ";
cin >> change;//User input
if ((change <= 0) || (change >= 100))//Check to ensure the input meets paremeters
{
cout << "\nYou have entered an incorrect amount, program will end. \n";//Exit program if input does not meet parameters
exit(1);
}
else
{
computeCoin (quarters, dimes, penny);//Call to computeCoin Function
cout << "\nOne Possible outcome is: \n\n";
cout << quarters << " Quarter(s), " << dimes << " Dime(s), " << " and " << penny << " Penny (Pennies) \n";
}
cout << "Do you want to try again? ";
cin >> ans;
}
while ((ans == 'y') || (ans == 'Y'));//Let's the user keep playing
cout << endl;
cout << "\nThanks for using my program \n";
cout << endl;
system ("Pause");
return 0;
}
void computeCoin (int coinValue, int& number, int& amountLeft)
{
//Statements
if (change >= 25)//Check to see if the input is large enough to have quarters
{
quarters = (change / 25);
amountLeft = (change % 25);
change = amountLeft;
}
else
{
dimes = (change / 10);
amountLeft = (change % 10);
change = amountLeft;
penny = change;
}
}

```

Is This A Good Question/Topic? 0

Replies To: Loop problems

#2 tree6014

Reputation: 2
• Posts: 14
• Joined: 30-November 07

Re: Loop problems

Posted 22 January 2009 - 08:45 AM

Just as a note, it is bad form to kill the pogram due to invalid input. It's better to have error checking that will ask for the input again and indicate the proble with the input. It is also better to not use global variables.

```/* This program will compute the type of coins given/left as change.
Written by: Charles Fetterolf
Date: 13 Jan 2009
*/
#include <iostream>
#include <cstdlib>
using namespace std;
//Prototype Statements
void computeCoin (int coinValue, int& number, int& amountLeft);
//Global Declarations
int quarters, dimes, penny, change;
int main(void)
{
//Local Declarations
char ans;
//Statements
do
{
cout << "\nThis program will tell you how many and what type of coins can be given as change. \n";
cout << "Enter an amount from 1 to 99: ";
cin >> change;//User input
if ((change <= 0) || (change >= 100))//Check to ensure the input meets paremeters
{
cout << "\nYou have entered an incorrect amount, program will end. \n";//Exit program if input does not meet parameters
exit(1);
}
else
{
computeCoin (quarters, dimes, penny);//Call to computeCoin Function
cout << "\nOne Possible outcome is: \n\n";
cout << quarters << " Quarter(s), " << dimes << " Dime(s), " << " and " << penny << " Penny (Pennies) \n";
}
cout << "Do you want to try again? ";
cin >> ans;
}
while ((ans == 'y') || (ans == 'Y'));//Let's the user keep playing
cout << endl;
cout << "\nThanks for using my program \n";
cout << endl;
system ("Pause");
return 0;
}
void computeCoin (int coinValue, int& number, int& amountLeft)
{
//Statements
quarters = 0;
dimes = 0;
if (change >= 25)//Check to see if the input is large enough to have quarters
{
quarters = (change / 25);
amountLeft = (change % 25);
change = amountLeft;
}
if(change >=10)
{
dimes = (change / 10);
amountLeft = (change % 10);
change = amountLeft;
}
penny = change;
}

```

#3 MntMan

Reputation: 0
• Posts: 32
• Joined: 22-January 09

Re: Loop problems

Posted 22 January 2009 - 10:40 AM

Thanks for help. I have made the changes you indicated and it works great. IF IF why didn't I try that...