By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Trying to get my Program to rerun and submit errors

P: 2
i have created a program that counts change for school but i am also wanting to add the ability fot the program to rerun on request and display an error message for negatitve numers, 0 or letters. can anyone help?
thanks


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. int main()
  8. {
  9.   char question[] = "Please, enter your first name: ";
  10.   char greeting[] = "Hello, ";
  11.   char yourname [80];
  12.   cout << question;
  13.   cin >> yourname;
  14.   cout << greeting << yourname << "!";
  15.  
  16.  
  17.   //declare variables
  18.   int change = 0;
  19.   int remainder = 0;
  20.   int dollars = 0;
  21.   int halfdollars = 0;
  22.   int quarters = 0;
  23.   int dimes = 0;
  24.   int nickels = 0;
  25.   int pennies = 0;
  26.  
  27.   //get input item from user
  28.   cout << "Enter the amount of change in cents: ";
  29.   cin >> change;
  30.  
  31.   //calculate change bank will give Joe
  32.   dollars = change / 100;
  33.   remainder = change % 100;
  34.   halfdollars = change / 50;
  35.   remainder = change % 50;
  36.   quarters = remainder / 25;
  37.   remainder = remainder % 25;
  38.   dimes = remainder / 10;
  39.   remainder = remainder % 10;
  40.   nickels = remainder / 5;
  41.   remainder = remainder % 5;
  42.   pennies = remainder / 1;
  43.  
  44.   //display output items
  45.   cout << "You will receive " << dollars << " dollars, "<< halfdollars << " halfdollars, "  << quarters << " quarters, " << dimes << " dimes, " 
  46. << nickels << " nickels, and " << pennies << " pennies from the bank." << endl;
  47.  
  48.   return 0;
  49. } //end of main function
Nov 26 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Let's do one part at a time. How do you think you will keep the program running on command? You're going to be doing the same commands over and over again...what does that sound like?
Nov 26 '07 #2

P: 2
well for the repeat i was thinking of a goto comand but each time i tried errors would pop up everywhere.

such as goto viewtop and then placeing viewtop at the beginging
Nov 26 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Don't use goto statements. Ever. They are a poor programming practice that make your code impossible to read, analyze, and (for larger projects) modify. There's another, far simpler method to do this: a simple loop.
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.