I can't get my text file to be read.

My text file is in the correct folder and the name of my text file is the same as the code. I dont get no errors i dont see why i cant get my text to be read.

my code.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>


using namespace std;
	void main();
	int menu();	
	double currentBalance(double balance);
	double withdraw();
	double deposit ();
	double balance;

	float payment();
	int option;
		
	bool unscreen();


void main()
		
{
	
		

	int menu;

	
	
		cout<<" \n---------------------------------------------------"<<endl;
		cout<<"If you require help, press 1 for unscreen help. \n"<<endl;
		
		cout<<"Please choose from the following option \n"<<endl;
		
		cout<<"Press 2 for to check the Balance."<<endl;
		cout<<"Press 3 for to make a Withdraw."<<endl;
		cout<<"Press 4 for to make a Deposit."<<endl;
		cout<<"Press 9 to exit"<<endl;
		
					
		cin>>menu;
		

		switch(menu)
		{
		case 1:
			unscreen();
			main ();
		
		case 2: 
			currentBalance(balance);
			main ();

		case 3: 
		withdraw();
		main ();

		case 4: 
		deposit();
		main ();

		
		case 9:
			exit(0);

		default:
			system("CLS");
			cout<<"You did not choose from the menu options, reloading the menu!"<<endl;
			cout<<"Please input the correct input, to make account action."<<endl;
			main ();

		}
	
}



double currentBalance(double balance)
		{

			

			system("CLS");
			cout<<"Welcome to the balance area"<<endl;
			cout<< "Your balance is:" <<(char)156 <<endl; 		
			
			
				fstream readaccountfile;
				readaccountfile.open("money.txt");
				char output[100];
				if (readaccountfile.is_open()) 
				{
					
					while(!readaccountfile.eof()) 
					
					{
						readaccountfile >> output;
					}
				}

				readaccountfile.close();
				balance = atof(output);
				
				cout<<" \n"<<endl;
				cout<<"Press 0 for further action or press 9 to exit." <<endl;
				
				cin >> option;
				
				if (option == 9)
			
							{
							exit(0);
							}

							else if (option == 0 )
							{
							return balance;
							}

							else
							{
							exit(0);
							}
			

		}	



	
Can someone take a look at my code, if something something is wrong, why i cant get my text file to read.
What makes you think it isn't being read?

What unexpected behaviour are you seeing when you run your code?

I will note that:

1) When you call currentBalance(), you ignore the return value, so the result of the calculation is being thrown away and never used.

2) Within currentBalance(), you've defined the argument to be called balance. This hides the global variable also called balance. This is likely to cause confusion for you, leading to errors.

3) In fact, using global variables at all is a bad idea. Best to get out of the habit of doing it now.

4) At several points in your code, you are attempting to call the main() function. This is illegal. Do not do it. Ever.

5) You should use a consistent indentation style. The way you're doing it now makes the code harder to read. Using a consistent style will help you see the flow of control in your code more easily, and will help you to see your errors more easily.
Last edited on
You have some serious problems to fix here.

You must use int main() instead of void main() int main also does not need to be declared before being used. Also, do not call main() anywhere in your program. It calls a new copy of main() i.e. It does not just jump back to main() like a goto statement (don't use those either though) or something, it calls up a whole new copy. This is very bad and both these things need to be sorted. If you compiler is not throwing up a big error about void main(), you need to get another compiler.

Secondly, read this article: http://www.cplusplus.com/articles/j3wTURfi/

balance = atof(output);

To use atof() you need to include <cstdlib>.

 
cout<<" \n"<<endl;


As for the text file, is it in the same folder as your exe?

This line is just crazy. Why are you not just doing cout << "\n\n"; ???
So never to return to main, like i did here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
case 1:
			unscreen();
			main ();
		
		case 2: 
			currentBalance(balance);
			main ();

		case 3: 
		withdraw();
		main ();

		case 4: 
		deposit();
		main ();


And i thought i was call the balance, here

1
2
3
4
else if (option == 0 )
							{
							return balance;
							}


So never to return to main, like i did here

That's not returning to main. That's calling main again. Never do it.

And i thought i was call the balance, here

That's not "call" the balance. That's returning the balance. And, yes, your currentBalance() function returns the balance, but your main function doesn't do anything with that return value. It gets thrown away.

Last edited on
text file is in correct place, ive check numerous times and i check if the names are the same, which their are.
is this incorrect.

1
2
3
case 2: 
			currentBalance(balance);
			int menu (balance);
WORRKKSS...

1
2
3
case 2: 
			currentBalance(balance);
			int menu ();


i change to int menu, instead main.
int menu ();

doesn't do anything. It declares a function, which means that it tells the compiler what the symbol menu means, but it doesn't actually perform any actions.

And you already declare menu() at line 9 of your original post. Adding another declaration here is completely redundant. You could leave it out altogether and it would make no difference.
Last edited on
you were right, doesnt do nothing. Still cant get my file to be read.
Have you read my post above about the return value from currentBalance()?

MikeyBoy wrote:
That's not "call" the balance. That's returning the balance. And, yes, your currentBalance() function returns the balance, but your main function doesn't do anything with that return value. It gets thrown away.
Last edited on
I find a new way to do it, is it less reliable?

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
double currentBalance(double balance)
		{
 				string line;
				ifstream myfile ("money.txt");
				if (myfile.is_open())
				{
					while ( getline (myfile,line) )				
					{
						cout << line << '\n';
					}
					myfile.close();
				}
				else cout<< "unable to open file";


			cout<<"\n"<<endl;
			cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;


				if (option == 9)
			
							{
							exit(0);
							}

							else if (option == 0 )
							{
							return 0;
							}

							else
							{
							exit(0);
							}
}	
Now you're not even calculating returning the balance. You're just returning 0.

Is that really what you want?

EDIT: If that's the way you really want it to work, you should remove balance from the arguments to the function. This new version of your function doesn't use it any more.
Last edited on
After few hours, researching and change code.

I have this.

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// please.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using std::cout;
using std::ifstream;
using std::string;


using namespace std;
	int main();
	int menu();	
	double currentBalance(double balance);
	double withdraw();
	double deposit (double balance);
	double balance;
	double addSum;
	float payment();
	int option;
		
	bool unscreen();


int main()
		
{
	
		

	int menu;

	
	
		cout<<" \n---------------------------------------------------"<<endl;
		cout<<"If you require help, press 1 for unscreen help. \n"<<endl;
		
		cout<<"Please choose from the following option \n"<<endl;
		
		cout<<"Press 2 for to check the Balance."<<endl;
		cout<<"Press 3 for to make a Withdraw."<<endl;
		cout<<"Press 4 for to make a Deposit."<<endl;
		cout<<"Press 9 to exit"<<endl;
		
					
		cin>>menu;
		

		switch(menu)
		{
		case 1:
			unscreen();
			main ();
		
		case 2: 
			currentBalance(balance);
			main ();
					
		case 3: 
			withdraw();
			main ();


		case 4: 
		deposit(balance);
		main ();

		
		case 9:
			exit(0);

		default:
			system("CLS");
			cout<<"You did not choose from the menu options, reloading the menu!"<<endl;
			cout<<"Please input the correct input, to make account action."<<endl;
			main ();

		}
	
}



double currentBalance( double balance)
		{



			cout<<"Welcome to balance."<<endl;
			cout<<"Your balance is:"<<endl;
 				
			
			
			       // Declare your variables
			 balance = 0;   // Set them all to 0

				string path = "money.txt";          // Storing your filename in a string
				ifstream fin;                          // Declaring an input stream object

				  fin.open(path);                        // Open the file
				 if(fin.is_open())                      // If it opened successfully
		    {
		   fin >> balance;  // Read the values and
                           // store them in these variables
		   fin.close();                   // Close the file
	 }

    cout << balance << '\n';
  


			cout<<"\n"<<endl;
			cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;


				if (option == 9)
			
							{
							exit(0);
							}

							else if (option == 0 )
							{
							return 0;
							}

							else
							{
							exit(0);
							}
}	

double deposit(double balance)

{
	double addSum;

cout<< "Welcome to deposit."<<endl;

cout<<"Enter a sum you wish to add to your account:";
			
			cin>> addSum;

			balance = addSum + balance;

			cout << "Your new balance is:" << balance <<endl;

			 fstream myfile;
			  myfile.open ("money.txt");
			 
			  
			  myfile << balance;
			  myfile.close();


			cout<<"\n"<<endl;
			cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;

			if (option == 9)
			
			{
			exit(0);
			}

			else if (option == 0 )
			{
			return balance;
			}

			else
			{
			exit(0);
			}


}


double withdraw()

{
cout<< "Please work. why"<<endl;
return 0;
}

	

bool unscreen()
	{

		cout<<"\n"<<endl;
cout<<"Welcome to the screen help area"<<endl;

			
				string line;
				ifstream myfile ("unscreen.txt");
				if (myfile.is_open())
				{
					while ( getline (myfile,line) )				
				{
						cout << line << '\n';
					}
					myfile.close();
				}

				else cout<< "unable to open file";


			cout<<"\n"<<endl;
			cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;


				if (option == 9)
			
							{
							exit(0);
							}

							else if (option == 0 )
							{
							return 0;
							}

							else
							{
							exit(0);
							}
}



I have no errors, for now. What the deposit is doing is add what is been input on the (addSum) and what is in the balance, but nothing is the balance, because i havent call the value/intenger from text file.

Right now if i add 12, it will say my balance is 12. Even if the balance is 10 in my text file and i add 12, it will store 12, because balance will start in 0.

How do i read what is on text file and add what has been input on (addSum) and store to balance.
1) You're still making calls to main() within your code. Don't do that. It is not legal C++. If your compiler happens to allow it, it may cause unexpected and unpredictable behaviour.

2) You still have currentBalance() returning a value that is completely ignored by the calling code. And that value is always 0. Why do you have your function returning a value, if the value is never used?

Presumably, what you want is for the function to actually return the value that it reads from the file, and then the calling code passes that return value into the deposit function, right?

3) Your currentBalance() and deposit() functions still define an argument called balance that hides the definition of the global variable declared on line 22. This is an unhelpul and confusing thing to do.

4) Similarly, you have a local variable addSum declared in your deposit() function, that hides the global variable of the same name on 23.

5) In fact, nowhere in your code is that global variable addSum used at all. Why is it there?

And possibly the most important question of all:

6) What's the point of coming to us for help, and then totally ignoring the help you're being given?


I've made some changes, to the text file before add a sum to the balance.


1
2
3
4
		case 4: 
		balance = deposit(balance);
		writeBalance(balance);
		main ();


1
2
3
4
5
6
7
8
9
10

double writeBalance(double balance)
{
ofstream accountfile;
accountfile.open("money.txt");
accountfile <<balance;
accountfile.close();
return 0;

}
1) You're still making calls to main() within your code. Don't do that. It is not legal C++. If your compiler happens to allow it, it may cause unexpected and unpredictable behaviour.

Instead of having main(). What do i put, because if i remove main(), as once the a function is done is goes straight to another function, i want to back to the menu.

Should i use do while loop??

4) Similarly, you have a local variable addSum declared in your deposit() function, that hides the global variable of the same name on 23.

It's has been remove, sorry sir. But i forgot why i had addSum, on the gloval variable.

Presumably, what you want is for the function to actually return the value that it reads from the file, and then the calling code passes that return value into the deposit function, right?

Yes that what i want do, for example i have 10 in the text file, on the deposit function i want to make deposit and the number that has bbeen input to the text file and have it store.
Last edited on
> What do i put, because if i remove main(), as once the a function is done is goes straight to another function,
http://www.cplusplus.com/doc/tutorial/control/ (`Another selection statement: switch.')

> i want to back to the menu.
use a loop
MikeyBoy wrote:
Presumably, what you want is for the function to actually return the value that it reads from the file, and then the calling code passes that return value into the deposit function, right?
Renato wrote:
Yes that what i want do

So... um... do that.
Last edited on
Topic archived. No new replies allowed.