Not sure why [continue] is not working

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Not sure why [continue] is not working

 
0
  #1
Oct 28th, 2007
Shouldn't continue in my else statement return me to the top do...while loop and print the following again?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double withdraw, deposit, balance, newBalance, amount;
  8. char ans;
  9. string choice;
  10.  
  11. balance = 1000;
  12.  
  13. cout << "Welcome to Whatever ATM!" << endl;
  14.  
  15. do
  16. {
  17. cout << "\nWhat would you like to do from the following?\n"
  18. << "\nWithdraw from account\n"
  19. << "Deposit money into account\n"
  20. << "Balance inquiry\n"
  21. << "Quit\n"
  22. << "\nPlease select now" << endl;
  23.  
  24. cin >> choice;
  25. ans = choice[0];
  26. newBalance = balance;
  27.  
  28. if ((ans == 'w') || (ans == 'W'))
  29. {
  30. cout << "\nWhat amount would you like to withdraw?" << endl;
  31. cin >> amount;
  32.  
  33. while (amount > balance)
  34. {
  35. cout << "Sorry, you have insufficient funds.\n"
  36. << "Please try again: ";
  37. cin >> amount;
  38. }
  39. if (amount <= balance)
  40. {
  41. balance = balance - amount;
  42. newBalance = balance;
  43. cout << "Thank you, your new balance is: " << newBalance << endl;
  44. }
  45. }
  46.  
  47. else if ((ans == 'd') || (ans == 'D'))
  48. {
  49. cout << "\nWhat amount would you like to deposit?" << endl;
  50. cin >> amount;
  51. balance = balance + amount;
  52. newBalance = balance;
  53. cout << "Thank you, your new balance is: " << newBalance <<endl;
  54. }
  55. else if ((ans == 'b') || (ans == 'B'))
  56. cout << "Your balance is: " << balance <<endl;
  57. else if ((ans == 'y') || (ans =='Y'))
  58. {
  59. cout << "\nThank you for banking with Whatever ATM!" << endl;
  60. return 0;
  61. }
  62. else
  63. {
  64. cout << "\nSorry, input not valid, please select again" << endl;
  65. continue;
  66. }
  67.  
  68. cout << "\nWould you like to make another transaction? (Y/N) ";
  69. cin >> choice;
  70. ans = choice[0];
  71. }
  72. while ((ans == 'y') || (ans =='Y'));
  73.  
  74. cout << "\nThank you for banking with Whatever ATM!" << endl;
  75. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Not sure why [continue] is not working

 
0
  #2
Oct 28th, 2007
I believe so.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Re: Not sure why [continue] is not working

 
0
  #3
Oct 28th, 2007
Well for some reason it doesn't, it just breaks the loop and I don't understand why.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Re: Not sure why [continue] is not working

 
0
  #4
Oct 28th, 2007
Part of my code i pasted was wrong, either way, it doesnt effect the whole thing
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Not sure why [continue] is not working

 
0
  #5
Oct 28th, 2007
Originally Posted by dabu View Post
Shouldn't continue in my else statement return me to the top do...while loop and print the following again?
Sorry, my mistake. No, the continue sends you to the bottom of the loop.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Re: Not sure why [continue] is not working

 
0
  #6
Oct 28th, 2007
Hmm, thats weird, my book tells me that it jumps back to the beginning of the loop. Well, if that doesn't work, how do I go about making the statement in my else to return to the top?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Not sure why [continue] is not working

 
0
  #7
Oct 28th, 2007
First figure out why the loop exits. Then before the continue, defeat the exit condition.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Re: Not sure why [continue] is not working

 
0
  #8
Oct 28th, 2007
I slept on it and still cant figure it out. Should I have another loop before my if statement? If I do, I don't know what the condition would be. Also for one of the if statements (which i accidentally posted as,
  1. else if ((ans == 'y') || (ans == 'Y'))
I would want it to quit the program, not just break the loop.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Not sure why [continue] is not working

 
0
  #9
Oct 28th, 2007
Look at the do-while. What causes the loop to exit?
Then look at the if with the continue. Is there a correlation?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Re: Not sure why [continue] is not working

 
0
  #10
Oct 28th, 2007
Okay, I changed it, adding another
  1. do-while
loop, but now the continue only goes through once. If the user enters an invalid answer, it does what I want it to, but when i enter another invalid answer, it breaks the loop. Is it something wrong with my condition?


  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double withdraw, deposit, balance, newBalance, amount;
  8. char ans;
  9. string choice;
  10.  
  11. balance = 1000;
  12.  
  13. cout << "Welcome to Whatever ATM!" << endl;
  14.  
  15. do
  16. {
  17.  
  18. do
  19. {
  20. cout << "\nWhat would you like to do from the following?\n"
  21. << "\nWithdraw from account\n"
  22. << "Deposit money into account\n"
  23. << "Balance inquiry\n"
  24. << "Quit\n"
  25. << "\nPlease select now" << endl;
  26.  
  27. cin >> choice;
  28. ans = choice[0];
  29. newBalance = balance;
  30.  
  31. if ((ans == 'w') || (ans == 'W'))
  32. {
  33. cout << "\nWhat amount would you like to withdraw?" << endl;
  34. cin >> amount;
  35.  
  36. while (amount > balance)
  37. {
  38. cout << "Sorry, you have insufficient funds.\n"
  39. << "Please try again: ";
  40. cin >> amount;
  41. }
  42. if (amount <= balance)
  43. {
  44. balance = balance - amount;
  45. newBalance = balance;
  46. cout << "Thank you, your new balance is: " << newBalance << endl;
  47. break;
  48. }
  49. }
  50. else if ((ans == 'd') || (ans == 'D'))
  51. {
  52. cout << "\nWhat amount would you like to deposit?" << endl;
  53. cin >> amount;
  54. balance = balance + amount;
  55. newBalance = balance;
  56. cout << "Thank you, your new balance is: " << newBalance <<endl;
  57. break;
  58. }
  59. else if ((ans == 'b') || (ans == 'B'))
  60. {
  61. cout << "Your balance is: " << balance <<endl;
  62. break;
  63. }
  64. else if ((ans == 'q') || (ans =='q'))
  65. break;
  66. else
  67. {
  68. cout << "\nSorry, input not valid, please select again" << endl;
  69. cout << "\nWhat would you like to do from the following?\n"
  70. << "\nWithdraw from account\n"
  71. << "Deposit money into account\n"
  72. << "Balance inquiry\n"
  73. << "Quit\n"
  74. << "\nPlease select now" << endl;
  75.  
  76. cin >> choice;
  77. ans = choice[0];
  78. continue;
  79. }
  80. }
  81. while ((ans == 'w') || (ans == 'W') || (ans == 'd') || (ans == 'D')
  82. || (ans == 'b') || (ans == 'B') || (ans == 'q') || (ans == 'Q'));
  83. cout << "\nWould you like to make another transaction? (Y/N) ";
  84. cin >> choice;
  85. ans = choice[0];
  86. }
  87. while ((ans == 'y') || (ans =='Y'));
  88.  
  89. cout << "\nThank you for banking with Whatever ATM!" << endl;
  90. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC