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;
}

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

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

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.

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?

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.

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

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;
}

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?

: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.

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.

This article has been dead for over six months. Start a new discussion instead.