Hello. I wrote a program which works fine, all i need is to add one more thing. When I ask the user to input an answer (withdraw, balance...) I don't know how to prompt them if they enter something else, such as "golfball" and loop them back to entering the correct one.

Here is what I have:

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


              
       do
       {
           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;
               }
           }
           
           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;
           }
           
           if ((ans == 'b') || (ans == 'B'))
               cout << "Your balance is:  " << balance <<endl;
           
           if ((ans == 'q') || (ans == 'Q'))
           {
               cout << "\nThank you for banking with Whatever ATM!" << endl;
               return 0;
           }
       
      }
      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;
}

Data validation isn't an easy process, but it isn't rocket science either. There are two common approaches.

The first is to never accept input as any type other than as a string because strings are always valid. Doing this you would then validate that all the characters in the string are valid for the type you want and then convert the string to that type.

The second way is to check if the stream goes into a failed state or not when the user enters input. If it does then you need to clear the input buffer and reset/clear the input stream.

I'm still confused.
First of all, for a beginner, are the ways I set up my loops okay?.
Second, how and where do I check to see if the input is valid? I don't know what you mean about "input buffer" and "reset/clear the input stream"
And lastly, am I not reading them in as strings?

Well, a student project but at least there was an effort to solve that.

#include <iostream>
#include <string>

 
int main()
{ 
   double withdraw, deposit, balance, newBalance, amount;
   char ans;
   string choice;

   balance = 1000;
    
[B]    bool good = false;[/B]
   cout << "Welcome to Whatever ATM!" << endl;

   do
   {
[B]        while(!good){[/B]

            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];
            [B]switch(ans)
            {
    case 'w':
    good = true;
    break;
    /*go on with your cases
        to cover all your  allowed values*/
            }
[/B]        [B] }[/B]
       newBalance = balance;


              
       do
       {
           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;
               }
           }
           
           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;
           }
           
           if ((ans == 'b') || (ans == 'B'))
               cout << "Your balance is:  " << balance <<endl;
           
           if ((ans == 'q') || (ans == 'Q'))
           {
               cout << "\nThank you for banking with Whatever ATM!" << endl;
               return 0;
           }
       
      }
      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 don't see why you need the inner do/while loop. The logic for the while condition of the inner while loop is wrong since answer can only have one value at a time and using the logic you have will only be true if ans has more than one value at a time.

choice is a string but amount is a double, though now that I reread your origninal post you probably aren't looking for data validation. sorry.

It looks like what you want to do is to use a default. The routine for that is something like this;

int choice;
cout << "select one of the following using the number of the line listed" << endl;
cout << "1) withdrawal" << endl;
cout << "2) deposit" << endl;
cin >> choice;
do
{
   if(choice == 1)
     //do this
   else if(choice == 2)
     //do that
   else  //this is the default to catch any input thats wrong
     //do something else

    cout << "go again y/n" << endl;
    char s;
    cin >> s;
}while(s == 'y')

Often, instead of a series of if/else if/else statements a switch statement is used like thisl

switch(choice)
{
    case 1:
        //do this
        break;
     case 2:
        //do that
        break;
      default:
        //do something else
        break;
}

but if you've never heard of switch statements before, then the sequential if/else statement work, too.

Oops, I see tostrinj already posted the switch option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.