954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Not sure why [continue] is not working

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

#include <iostream>
#include <string>
using namespace std;
 
int main()
{ 
   double withdraw, deposit, balance, newBalance, amount;
   char ans;
   string choice;

   balance = 1000;

   cout << "Welcome to Whatever ATM!" << endl;

   do
   {
       cout << "\nWhat would you like to do from the following?\n"
        << "\nWithdraw from account\n"
        << "Deposit money into account\n"
        << "Balance inquiry\n"
        << "Quit\n"
        << "\nPlease select now" << endl;
  
       cin >> choice;
       ans = choice[0];
       newBalance = balance;

       if ((ans == 'w') || (ans == 'W'))
       {
           cout << "\nWhat amount would you like to withdraw?" << endl;
           cin >> amount;
               
               while (amount > balance)
               {
                   cout << "Sorry, you have insufficient funds.\n"
                   << "Please try again:  ";
                   cin >> amount;
               }
               if (amount <= balance)
               {
                   balance = balance - amount;
                   newBalance = balance;
                   cout << "Thank you, your new balance is: " << newBalance << endl;
               }
       }
       
       else if ((ans == 'd') || (ans == 'D'))
       {
           cout << "\nWhat amount would you like to deposit?" << endl;
           cin >> amount;
           balance = balance + amount;
           newBalance = balance;
           cout << "Thank you, your new balance is: " << newBalance <<endl;
       }
       else if ((ans == 'b') || (ans == 'B'))
           cout << "Your balance is:  " << balance <<endl;
       else if ((ans == 'y') || (ans =='Y'))
       {
           cout << "\nThank you for banking with Whatever ATM!" << endl;
           return 0;
       }
       else
       {
           cout << "\nSorry, input not valid, please select again" << endl;
           continue;
       }
       
       cout << "\nWould you like to make another transaction? (Y/N)  ";
       cin >> choice;
       ans = choice[0];
   }
   while ((ans == 'y') || (ans =='Y'));

   cout << "\nThank you for banking with Whatever ATM!" << endl;
}
dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

I believe so.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Well for some reason it doesn't, it just breaks the loop and I don't understand why.

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Part of my code i pasted was wrong, either way, it doesnt effect the whole thing

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
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 thebottom of the loop.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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?

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

First figure out why the loop exits. Then before the continue , defeat the exit condition.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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,

else if ((ans == 'y') || (ans == 'Y'))


I would want it to quit the program, not just break the loop.

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Look at the do-while . What causes the loop to exit?
Then look at the if with the continue . Is there a correlation?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Okay, I changed it, adding another

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?

#include <iostream>
#include <string>
using namespace std;
 
int main()
{ 
   double withdraw, deposit, balance, newBalance, amount;
   char ans;
   string choice;

   balance = 1000;

   cout << "Welcome to Whatever ATM!" << endl;

   do
   {

       do
       {
           cout << "\nWhat would you like to do from the following?\n"
                << "\nWithdraw from account\n"
                << "Deposit money into account\n"
                << "Balance inquiry\n"
                << "Quit\n"
                << "\nPlease select now" << endl;
  
           cin >> choice;
           ans = choice[0];
           newBalance = balance;

           if ((ans == 'w') || (ans == 'W'))
           {
               cout << "\nWhat amount would you like to withdraw?" << endl;
               cin >> amount;
               
               while (amount > balance)
               {
                   cout << "Sorry, you have insufficient funds.\n"
                   << "Please try again:  ";
                   cin >> amount;
               }
               if (amount <= balance)
               {
                   balance = balance - amount;
                   newBalance = balance;
                   cout << "Thank you, your new balance is: " << newBalance << endl;
                   break;
               }
           }
           else if ((ans == 'd') || (ans == 'D'))
           {
               cout << "\nWhat amount would you like to deposit?" << endl;
               cin >> amount;
               balance = balance + amount;
               newBalance = balance;
               cout << "Thank you, your new balance is: " << newBalance <<endl;
               break;
           }
           else if ((ans == 'b') || (ans == 'B'))
           {        
               cout << "Your balance is:  " << balance <<endl;
               break;
           }
           else if ((ans == 'q') || (ans =='q'))
               break;
           else 
           {  
               cout << "\nSorry, input not valid, please select again" << endl;
               cout << "\nWhat would you like to do from the following?\n"
                    << "\nWithdraw from account\n"
                    << "Deposit money into account\n"
                    << "Balance inquiry\n"
                    << "Quit\n"
                    << "\nPlease select now" << endl;
  
               cin >> choice;
               ans = choice[0];
               continue;
           }
       }
       while ((ans == 'w') || (ans == 'W') || (ans == 'd') || (ans == 'D')
           || (ans == 'b') || (ans == 'B') || (ans == 'q') || (ans == 'Q'));
       cout << "\nWould you like to make another transaction? (Y/N)  ";
       cin >> choice;
       ans = choice[0];
   }
   while ((ans == 'y') || (ans =='Y'));

   cout << "\nThank you for banking with Whatever ATM!" << endl;
}
dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

I tried getting rid of a loop and did this:

#include <iostream>
#include <string>
using namespace std;
 
int main()
{ 
   double withdraw, deposit, balance, newBalance, amount;
   char ans;
   string choice;

   balance = 1000;

   cout << "Welcome to Whatever ATM!" << endl;

   do
   {
       cout << "\nWhat would you like to do from the following?\n"
            << "\nWithdraw from account\n"
            << "Deposit money into account\n"
            << "Balance inquiry\n"
            << "Quit\n"
            << "\nPlease select now" << endl;
  
       cin >> choice;
       ans = choice[0];
       newBalance = balance;

       if ((ans == 'w') || (ans == 'W'))
       {
           cout << "\nWhat amount would you like to withdraw?" << endl;
           cin >> amount;
               
           while (amount > balance)
           {
               cout << "Sorry, you have insufficient funds.\n"
                   << "Please try again:  ";
               cin >> amount;
           }
           if (amount <= balance)
           {
               balance = balance - amount;
               newBalance = balance;
               cout << "Thank you, your new balance is: " << newBalance << endl;
               cout << "Would you like to make another transaction? (Y/N)" << endl;
               cin >> ans;
               if ((ans == 'y') || (ans == 'Y'))
                   continue;
               else
                   break;
           }
       }
       else if ((ans == 'd') || (ans == 'D'))
       {
           cout << "\nWhat amount would you like to deposit?" << endl;
           cin >> amount;
           balance = balance + amount;
           newBalance = balance;
           cout << "Thank you, your new balance is: " << newBalance <<endl;
           cout << "Would you like to make another transaction? (Y/N)" << endl;
           cin >> ans;
           if ((ans == 'y') || (ans == 'Y'))
               continue;
           else
               break;
       }
       else if ((ans == 'b') || (ans == 'B'))
       {
           cout << "Your balance is:  " << balance <<endl;
           cout << "Would you like to make another transaction? (Y/N)" << endl;
           cin >> ans;
           if ((ans == 'y') || (ans == 'Y'))
               continue;
           else
               break;
       }
       else if ((ans == 'q') || (ans =='q'))
           break;
       else
           continue;
       }
       while ((ans == 'w') || (ans == 'W') || (ans == 'd') || (ans == 'D')
           || (ans == 'b') || (ans == 'B') || (ans == 'q') || (ans == 'Q'));

   cout << "\nThank you for banking with Whatever ATM!" << endl;

}


There is still something wrong and I cant figure it out. Could someone give me some insight?

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

:icon_rolleyes:
So you didn't bother with my questions at all and just made a convoluted while statement? Then all you can say is "something is wrong -- go find it and tell me what to do"?

I don't think so...

I realize you're probably frustrated (so am I), but if you had followed my posts you might have learned something about how to debug your code.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
First figure out why the loop exits. Then before the continue , defeat the exit condition.


Sorry Walt, I should have said that this statement was not clear to me before, but after re-reading it I understand and now my program works the way I want.

dabu
Newbie Poster
12 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You