logic error and it not reading well

how to check if the user key in the correct pin number? it doesn't take the data from the txt file.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


struct Detail
{
char FirstName[30];
char LastName[30];
int AccNum;
int pinNum;
float deposit;
float balance;
}data[4];



int mainMenu();
void checkBalance(ifstream&,Detail data[]);
void withdrawl(Detail data[]);
void transfer_fund(Detail data[]);


int main()
{

int respond,option,pinN,i;

ifstream inFile("Data.txt");
char choice;



cout<<"\t\tEnter Your Pin Number"<<endl;
cout<<"\t\t\t";
cin>>pinN;
for(int i; i<4; i++)
if(data[i].pinNum==pinN)
{
do
{
option= mainMenu();

if (option ==1)
{
checkBalance(inFile,data);
}

else if (option ==2)
{
withdrawl(data);
}

else if (option ==3)
{
transfer_fund(data);
}

else if (option ==4)
{
cout<<"Continue? (y/n):";
cin>>choice;
}

}while(choice!='y');
}
else
cout<<"ERROR!";
inFile.close();


return 0;
}

int mainMenu()
{
int selection;

cout<<"======================================="<<endl;
cout<<"======================================="<<endl;
cout<<" WELCOME TO 3A BANKING "<<endl;
cout<<"======================================="<<endl;
cout<<"======================================="<<endl;
cout<<"\n\n";
cout<<"1)Check Account Balance 2)Withdrawl Money"<<endl;
cout<<"3)Deposit/Transfer Fund 4)Exit "<<endl;
cout<<"\t\t";
cin>>selection;
cout<<endl<<endl;

return selection;
}


void checkBalance(ifstream& inFile, Detail data[])
{
int i;

cout<<"\t\tCurrent Balance "<<endl;
cout<<data[i].balance<<endl<<endl;
cout<<"\t\t\tThank You"<<endl<<endl;
}


void withdrawl(Detail data[])
{
int i;
float amount;

cout<<"\t\tEnter your amount to withdraw "<<endl;
cout<<"\t\t\t";
cin>>amount;
data[i].balance = data[i].balance - amount;
cout<<"\n\t\tWithdrawl Amount : "<<amount<<endl;
cout<<"\t\tBalance :"<<data[i].balance<<endl;
cout<<"\n\n Thank You";


}


void transfer_fund(Detail data[] )
{
int otherAccNum, i;
float amount;

cout<<"\t\tEnter your amount "<<endl;
cout<<"\t\t";
cin>>amount;
cout<<"\t\tEnter the Account "<<endl;
cout<<"\t\t";
cin>>otherAccNum;
data[i].balance = data[i].balance - amount;
cout<<"\n\t\tTransfer Amount : "<<amount<<endl;
cout<<"\t\tBalance : "<<data[i].balance<<endl;
cout<<"\n\n Thank You";

}
this is content from my txt file.

data.txt
1234 123456789 NANA HANA 50000.00
4567 987654321 KIM NAMJOON 50000.00
1309 234567890 KIM TAEHYUNG 50000.00
1111 877546352 NANA YODA 50000.00
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
struct Detail 
{
char FirstName[30];
char LastName[30];
int AccNum;
int pinNum;
float deposit;
float balance;
}data[4];



int mainMenu();
void checkBalance(ifstream&,Detail data[]);
void withdrawl(Detail data[]);
void transfer_fund(Detail data[]); 


int main()
{

int respond,option,pinN,i;

ifstream inFile("Data.txt");
char choice;



cout<<"\t\tEnter Your Pin Number"<<endl;
cout<<"\t\t\t";
cin>>pinN;
for(int i; i<4; i++) 
if(data[i].pinNum==pinN)
{
do
{
option= mainMenu();

if (option ==1)
{
checkBalance(inFile,data);
}	

else if (option ==2)
{
withdrawl(data);
}

else if (option ==3)
{
transfer_fund(data);
}

else if (option ==4)
{
cout<<"Continue? (y/n):";
cin>>choice;
}

}while(choice!='y');
}
else
cout<<"ERROR!";
inFile.close();


return 0;
}

int mainMenu()
{
int selection;

cout<<"======================================="<<endl;
cout<<"======================================="<<endl;
cout<<" WELCOME TO 3A BANKING "<<endl;
cout<<"======================================="<<endl;
cout<<"======================================="<<endl;
cout<<"\n\n";
cout<<"1)Check Account Balance 2)Withdrawl Money"<<endl;
cout<<"3)Deposit/Transfer Fund 4)Exit "<<endl;
cout<<"\t\t";
cin>>selection;
cout<<endl<<endl;

return selection;
}


void checkBalance(ifstream& inFile, Detail data[])
{
int i;

cout<<"\t\tCurrent Balance "<<endl;
cout<<data[i].balance<<endl<<endl;
cout<<"\t\t\tThank You"<<endl<<endl;
}


void withdrawl(Detail data[])
{
int i;
float amount;

cout<<"\t\tEnter your amount to withdraw "<<endl;
cout<<"\t\t\t";
cin>>amount;
data[i].balance = data[i].balance - amount;
cout<<"\n\t\tWithdrawl Amount : "<<amount<<endl;
cout<<"\t\tBalance :"<<data[i].balance<<endl;
cout<<"\n\n Thank You";


}


void transfer_fund(Detail data[] )
{
int otherAccNum, i;
float amount;

cout<<"\t\tEnter your amount "<<endl;
cout<<"\t\t";
cin>>amount;
cout<<"\t\tEnter the Account "<<endl;
cout<<"\t\t";
cin>>otherAccNum;
data[i].balance = data[i].balance - amount;
cout<<"\n\t\tTransfer Amount : "<<amount<<endl;
cout<<"\t\tBalance : "<<data[i].balance<<endl;
cout<<"\n\n Thank You";

}
Start by reading from the file into the array. After that, the program can search the array for user supplied values such as PIN and Account number.

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct Detail      
{
    char FirstName[30];
    char LastName[30];
    int AccNum;
    int pinNum;
    float deposit;
    float balance;
};


bool readDetail(ifstream& inFile, Detail& det);
void printDetail(const Detail& det);
    
int main()
{
    const int size = 4;
    Detail data[size];
    
    ifstream inFile("Data.txt");
    
    // Read from input file into data array 
    int count = 0;
    while (count < size && readDetail(inFile, data[count]))
        ++count;
        
    // For testing purposes, display entire contents of array.
    for (int i=0; i<count; ++i)
        printDetail(data[i]);

    // Here add code to ask user for Account Number and PIN
    // and search the array for matching entry.
    
    return 0;
}

bool readDetail(ifstream& inFile, Detail& det)
{
    inFile >> det.pinNum >> det.AccNum 
           >> det.FirstName >> det.LastName >> det.balance;
    det.deposit = 0; // initialise value not in file.
    return bool(inFile);         
}

void printDetail(const Detail& det)
{
    cout << det.pinNum << '\t'
         << det.AccNum << '\t'
         << det.FirstName << ' ' << det.LastName << '\t'
         << det.balance << '\n';
}


Notice function printDetail() processes just a single item from the array. This seems like the way the other functions should work as well (remove const if the values should be modified, such as deposit or withdrawal).

I used this data.txt for testing:
1234	123456789	NANA HANA	50000.00
4567	987654321	KIM NAMJOON	50000.00
1309	234567890	KIM TAEHYUNG	50000.00
1111	877546352	NANA YODA	50000.00
Last edited on
Topic archived. No new replies allowed.