3 Replies - 743 Views - Last Post: 10 August 2015 - 08:39 AM Rate Topic: -----

#1 neville1967  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-June 15

User input validation

Posted 08 August 2015 - 01:28 PM

My program is a ATM C++ program. I am using gedit in ubuntu to create the program.
I have a txt file with pin,cardNumber,balance. The data is all numbers
seperated by commas.(5644,23454443434555543,230.44) I would like to be able to create an array of some sort
At the present time I am just entering a card number that has
no value, i am using a variable for Pin and assigning it
a pin value but need to get away from doing it this way.

I would like to be able to get the user to enter a card number
so that the program would search to locate the correct card number
from the txt file and then store the pin and balance so that i
can update the balance with user input.

After the program has validated the correct card number I would be
able to carry on and do some transactions.

I have a few other issues that I would like some help with please.

1: when cash withdraw is first selected , the program ends due to no funds.
I would like to be able to go back to the main menu instead of crashing.

2:On deposit when you have selected an amount it ask if you would like another transaction,
when you press 1 for yes it ends the program.

3: On balance Inquiry when it ask if you would like another transaction,
when you press 1 for yes it ends the program.

I am not the best programmer and I know there are a lot better ways of doing
this program but I have come a long way in a few months and I am trying hard to get to grips with each ellement.

Can some one please help.
Many thanks.

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector> 
#include <functional> 
#include <stdlib.h>



using namespace std;


double balance = 0, current_amount;
int option;
int another_transaction = 1;
long cardNumber;
bool cardFound = true;
bool pinOk = true;
int pin = 1234;
int tries = 1;

int main(void) {

               
	cout << "\n\n"<<endl;
	cout << " **************************************\n";
	cout << " **************************************\n";
	cout << " **************************************\n";
	cout << " ***** WELCOME TO MAINLAND`S BANK:*****\n";
	cout << " **************************************\n";
	cout << " **************************************\n";
	cout << " **************************************\n";
	cout << "\n\n"<<endl;

                     
                

void();
{
    string pin;
    int x = 1;

                 cout << " Please enter the long card number :";
                 cin >>cardNumber;
                 
                     
                  if(!cardNumber)
                     { 
                      cout << " Error Reading card: "<<endl;
                      ++cardNumber;
                     }
                     
 
                    else{
                     cout << " Card details excepted\n";
                     cout << " **************************************\n";
                }

    while (1)  //This creates an overall top level infinite loop
    {
        cout << " Input pin here: ";
        cin >> pin;

       if ( pin == "teddy") //This sets the condition for success
       {
           cout << " Access Granted!!!!\n";
           break; //The break is applied here to stop the cycle after success is made
       }

       else if ( pin != "teddy") //This sets the condition for failure

       {
       cout << " Incorrect Pin!!! " << "\n" " You have made "<< x << " " << "wrong attempts" << "\n";
       ++x;

       if ( x > 3 ) // This is the counter limit portion. Limit set to 5 attempts

       {
           cout << " You have exceeded the amount of tries and your account is frozen" <<endl;
           cout << " Please Contact your local branch" <<endl;
           return 0;
       }
     }
}
}
 //exit main if pin wrong}



                   while(another_transaction == 1){
		   
                   cout<<" Login Successful"<<endl;
                   cout<< " **************************************\n";
                   cout<< "\n";
		   cout<<" Main Menu: "<<endl;
                   cout<< "----------------------\n";
		   cout<<" 1. Cash Withdraw:"<<endl;
		   cout<<" 2. Cash Deposit: "<<endl;
		   cout<<" 3. Balance Inquiry  "<<endl;
		   cout<<" 4. Exit: "<<endl;
		   cin>>option;


		if(option == 1)
                 {
                   cout<<" The balance in your account is "<<balance<<endl;
		
                if(balance <= 0)
                 {
		   cout<<" Your account is empty. \n Please make a deposit:"<<endl;
		   
		 return 0; // exit option if balance <0
		}
	         cout<<" How much do you want to withdraw? "<<endl;
	         cout<<" Amount: ";
	         cin>>current_amount;
                 cout<<"\n You are about to withdraw "<< current_amount <<endl;
                 cout<<" Is this correct"<<endl;
	         cout<<" 1 - YES "<<endl;
	         cout<<" 2 - NO "<<endl;
	         cin>>another_transaction;

                if(current_amount < balance){
		balance = balance - current_amount ;
		cout<<" Withdraw Successful "<<current_amount<<endl;
		cout<<" Your new Balance is "<<balance;
		cout<<endl<<endl;
                

	  // let them withdraw amount
	   } else {
	      cout<<" You do not have enough funds in your account "<<endl<<endl;
	   }

	 } else if(option == 2){
	    cout<<" Enter deposit amount ";
	    cin>>current_amount;
            balance = current_amount + balance ;
            cout<<"\n You are about to Deposit "<< current_amount <<endl;
            cout<<" Is this correct?"<<endl;
	    cout<<" 1 - YES "<<endl;
	    cout<<" 2 - NO "<<endl;
            cin>>another_transaction;
	    cout<<" You have deposited "<<current_amount<<endl; 
            cout<<" Your new balance is "<<balance<<endl<<endl;
            

	} else if (option == 3){
	    cout<<" Your balance is "<<balance;
	    cout<<endl<<endl;
	} else if (option == 4){
	    cout<<"_____________________________\n";
	    cout<<" Thank You for your custom   \n";
	    cout<<" PLease come back again soon\n ";
	    cout<<"_____________________________\n";
            return 0 ;
	    
	} else {
	  cout<<" You enterd invalid choice "<<endl<<endl<<endl;
	}

another_transaction = 0;

	while(another_transaction !=1 && another_transaction !=2){
	     cout<<"\n Would you like another transaction? \n\n"<<endl;
	     cout<<" 1 - YES "<<endl;
	     cout<<" 2 - NO "<<endl;
	     cin>>another_transaction;
	     cout<<" Thank you and good bye "<<endl;
	     
	     cout<<"\n"<<endl;
	  }
	

	return 0; //end of main if option is to exit
	}}



Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: User input validation

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3778
  • View blog
  • Posts: 13,687
  • Joined: 08-August 08

Re: User input validation

Posted 08 August 2015 - 03:46 PM

Take it one step at a time. First, can you set up a struct or class to store the information from the file? Next, can you write a function to read the data from the file into the struct/class?
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5186
  • View blog
  • Posts: 13,916
  • Joined: 18-April 07

Re: User input validation

Posted 08 August 2015 - 03:52 PM

Line 39 and 40 where you have this code...

void();
{



This is not right at all. What exactly are you trying to do there?


When you go to withdraw you show the balance and then test if it is less than or equal to zero. You then return there. This is going to cause you to return from the entire function. Return terminates the function and returns the value you specify after the return keyword. It is not just kicking you out of that option. I would suggest you take a look at your program and break the whole thing into functions. Create a withdraw function, a print balance function, a deposit function etc. Then call each function from your menu. That way if you want to return from withdraw because the balance is zero, you will quit the withdraw function and it will return you back to the calling menu. That menu you can then have in a loop.

continue = "yes";

// Go into a loop where you show menu, then get an option.
// Options will kick off various functionality. Returning from them will return back to this while loop.
// At end of loop, you ask to continue.

while (continue == "yes") {
   showMenu();
   cin >> option;

   if (option == 1) {
      withdraw();
   }
   else if (option == 2) {
      deposit();
   }
   else if (option == 3) {
      showBalance();
   }
   else {
      cout << "I don't recognize that option" << endl;
   }

   cout << "Do you want to do another transaction?";
   cin >> continue;
}



The code above gives you the general format of how this mechanism may work. :)

This post has been edited by Martyr2: 08 August 2015 - 03:54 PM

Was This Post Helpful? 1
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: User input validation

Posted 10 August 2015 - 08:39 AM

Also:
int main(void) {
    //code
    return 0;
}

As a good practice in C++, when a function is not going to take an argument, the parentheses should be empty (no void):
int main() {
    //code
    return 0;
}

Unless for some reason you want to stay with the C style for compatibility reasons.

Edit: if you try Martyr2's code above, just watch out for continue as it is a keyword in C++, so you can't use it for a variable's name.

This post has been edited by Anarion: 10 August 2015 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1