user input validation

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#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 = teddy;
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
	}}
Last edited on
The way you go about it I find very confusing.
The spacing and indents and matching brackets are a mess making it hard to follow.
int pin = teddy; throws an error as not declared in this scope.
As a relative c++ beginner myself I have not seen this before,
void();
Some kind of inner function?
The task is easy enough but unraveling the source code is too time consuming.
Your code would be easier to read and modify if you used modules.
eg.
1
2
3
4
5
6
int valid;
valid = logIn()
if (valid == -1)
    return 0;
else
    // do your stuff 
Last edited on
This is what I mean by using modules.
It also deals with repeated transactions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>

using namespace std;

struct CLIENT
{
    int pin;
    int cardNumber;
    double balance;
};
CLIENT clients[4];

void printBanner()
{
    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;
}

int getValidPin()
{
    int validMatch = -1;
    int card = 0;
    int pin  = 0;
    cout << endl << "Enter your pin number: ";
    cin  >> pin;
    cout << "Enter your card number: ";
    cin  >> card;
    // do they match?
    for (int i = 0; i<4; i++)
    {
        if (pin == clients[i].pin && card == clients[i].cardNumber)
        {
            cout << "match found" << endl;
            validMatch = i;
        }
    }
    return validMatch;
}

int getOption()
{
    int choice = 0;
    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 >> choice;
    return choice;
}


int main()
{

    // initialize some test data
    clients[0].pin = 1;
    clients[0].cardNumber = 10;
    clients[0].balance = 44.45;
    clients[1].pin = 2;
    clients[1].cardNumber = 20;
    clients[1].balance = 9032.01;
    clients[2].pin = 3;
    clients[2].cardNumber = 30;
    clients[2].balance = 5.30;
    clients[3].pin = 4;
    clients[3].cardNumber = 40;
    clients[3].balance = 877.05;

    int vPin = 0;
    int option = 0;

    printBanner();
    vPin = getValidPin();
    if (vPin == -1)
        cout << "invalid pin" << endl;
    else
    {
        cout<<" Login Successful"<<endl;
        option = getOption();
        while(option != 4)
        {
            // put code in here to carry out the transactions
            cout << endl << "Doing transaction ..." << endl;
            option = getOption();
        }
    }


    return 0;
}

Last edited on
I might have a go at this myself (also a beginner). Do you have a copy of the card numbers? You can copy and paste them in a post.

I think I write things differently to you too. I write:

int main()

and miss out the void part in the brackets. Also,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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";
                } 


Does this even need to be a void function? Wouldn't it make more sense to have a while loop here with some sort of bool variable? That way, if someone entered the wrong card number, it would output "Error Reading card" and then go back to "Please enter the long card number".

I tend to use void functions for rearranging things, or outputting text. Basically something I need to use a lot, but doesn't involve spitting out numbers.
Last edited on
fumbles22,

To test a program just make up the data or write a program to generate random numbers and save them as a text file.

This was generated by a simple program making up random digits.

3357,66299956060404888,34.45
8215,33428426743694567,9879.82
9064,09053775727479142,7160235.04
3089,15169567949812745,7297498.63
4962,68305258508678227,864.49
5194,53060717277117854,.73
0244,86586733309286366,1292863.17
7537,78721209068389342,30619.60
4455,73909490710096609,9.88
6914,88034592219455259,62.56
9150,34307172840175054,675234.12
6891,79612333222722598,30.62
6498,18214311326550430,9438.96
6969,14013991491190535,35.92
4224,13264628078407049,692314.19
6535,14550024793307595,0.58
4715,95086965695582808,7640.72
0982,58805625836773407,977069.16
5624,57600430959143291,04217.03
0280,13330251889536466,7440.45
Topic archived. No new replies allowed.