944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1310
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 28th, 2007
0

Not sure why [continue] is not working

Expand Post »
Shouldn't continue in my else statement return me to the top do...while loop and print the following again?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

I believe so.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

Well for some reason it doesn't, it just breaks the loop and I don't understand why.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

Part of my code i pasted was wrong, either way, it doesnt effect the whole thing
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

Click to Expand / Collapse  Quote originally posted by dabu ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

First figure out why the loop exits. Then before the continue, defeat the exit condition.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

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,
C++ Syntax (Toggle Plain Text)
  1. else if ((ans == 'y') || (ans == 'Y'))
I would want it to quit the program, not just break the loop.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

Look at the do-while. What causes the loop to exit?
Then look at the if with the continue. Is there a correlation?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 28th, 2007
0

Re: Not sure why [continue] is not working

Okay, I changed it, adding another
C++ Syntax (Toggle Plain Text)
  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?


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Two Player Craps
Next Thread in C++ Forum Timeline: Help with pascal triangle output





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC