Using external files

im reading from an external file. the program is suppose to be on bank transactions. I cannot seem to get passed one step.

the file is dailytransactions.txt and contains:

Tim 5000 W 400 D 2000 E

the code i have so far :


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
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
	int bal=0,amt=0;
	string name;
	char choice;
	
	ifstream bank;
	bank.open("dailyTransactions.txt");
		
	bank>>name>>bal>>choice>>amt;
        cout<<name<<" "<<bal<<endl;
			
	cout<<"Enter Choice"<<endl;
	cin>>choice;
				
while (choice!='E')
{
        if(choice=='D')
	bal=bal+amt;
else
	if(choice=='W')
	bal=bal-amt;
							
cout<<bal<<endl;
cout<<endl;
				
cout<<"Enter Choice"<<endl;
cin>>choice;
				
				
}
		
	
	
	
system ("pause");
return 0;  
}


i cannot get the deposit value to enter when D is chosen.

any help would be grateful
Hello neilram27,

As I look at your program I see you went to the trouble of opening and reading four variables from the file. But you only use two of the variables read and do nothing withe the other two or the rest of the line.

Lines 16 and 17 ask for user input. But what happens if the user enters a lower case letter? You could use he function "toupper(choice)" either after line 17 or in the if statements. Another alternative is to add an || to the if statements to check for a lower case letter.
http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper
This comes from the "<cctype>" header file. And I noticed that you are missing "<string>" header file.

Beyond that I would have to load the program and see what happens.

Hope that helps,

Andy
Hello neilram27,

After working with he program I found that after adding the "<string>" and "<cctype>" header files, the toupper function worked best following line 17.

I could not duplicate or find any other problem with your code except that you are not fully using the file that you are reading.

If there is something I missed here let me know.

Hope that helps,

Andy
neilram27 wrote:
I cannot seem to get passed one step.
Handy Andy wrote:
you are not fully using the file that you are reading.

@Handy Andy, I think you got the point, besides all other errors. I think the original post contains the real code... I mean: it’s not a simplified version, the OP is actually not looping throughout the file.
Topic archived. No new replies allowed.