Variable not storing the value.


So i'm creating bank system. W

Where I have function that reads the text file (balance), and I make a deposit, which adds what was in the balance = deposit + balance.

But this is what is happening.

In my text file i have number 10. Function balance works fine and reads 10 from the text file.

When I make a deposit add a value to the balance, if i add 7, the new balance is 17. And i check the text file shows me is 17. Which is correct.

The problem is here.
If i make another deposit, without closing the program, for example i add 5 to the balance, the new balance should be 22 = 17(balance) + 5(deposit), because 17 was store on the text file and it was the balance that was store on the text file on the last time. But it shows me 15, it adds the balance that the program first started which was 10(balance) + 5(deposit), but should had had be 17 + 5.

When I close the program the value on the text file, is 15, that was the last sum that i did.

I hope I made this clear.

1
2
3
4
5
6
7
8
int main()
			
{
	double balance = 0;
	balance = currentBalance(balance);
	menu(balance);
	return 0;
}	


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
	
double currentBalance( double balance)
{

			cout<<"Welcome to your bank system."<<endl;
			cout<<"Your balance is:"<<endl;
 				
				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';
		 return balance;
		
}

	// deposit function

double deposit(double balance)

{
	int option;
	double addSum;

			system("CLS");
			cout<< "Welcome to deposit."<<endl;
			cout<<"Enter a sum you wish to add to your account:";
			
				 fstream myfile;
				 myfile.open ("money.txt");
			
				 cin>> addSum;

				 balance = addSum + balance;
			  
				 myfile << balance;
				
				 cout << balance << '\n';
			  
			cout<<"\nPress 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);
			}


}
To me it doesn't look as if you can tell whether your text file has actually opened properly.
Your code does check this but you can't.

The reason I say this is because I thought that ifstream.open() requires a c-style string as it's path, and you've provided a string object.
Add a little cout << "Successfully loaded file"; or something after checking it's open, if you don't see it then the file's not opening and you can go from there.

If this is the problem then you can use string.c_str() or string.data() to get the C-style string of what is actually stored in your object string.
The reason I say this is because I thought that ifstream.open() requires a c-style string as it's path, and you've provided a string object.


It used to, but since C++11 an overload has been added for creating a file object from a std::string: http://www.cplusplus.com/reference/fstream/filebuf/open/
We'll need to see your menu() function, so we can see how you're passing values around.
You have not given the code for your menu() function.

If i make another deposit, without closing the program [...]

Well do you re-read the contents of the file as needed?

Minor points:

1) Your currentBalance() and deposit() functions show that you do not fully understand the purpose of parameters of functions. Which is why I suggest you read the C++ tutorial on this site, section "Functions":

http://www.cplusplus.com/doc/tutorial/functions/

2) You are going a bit overboard with indentation style. The tabs waste horizontal space and make your code look ugly.

For comparison, here's how I would rewrite your currentBalance() function.
I have not changed the function itself, and I prefer to insert tabs as spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double currentBalance(double balance)
{
    cout << "Welcome to your bank system." << endl;
    cout << "Your balance is:" << endl;

    string path = "money.txt";
    ifstream fin;

    fin.open(path);

    if (fin.is_open())
    {
        fin >> balance;
        fin.close();
    }

    cout << balance << '\n';
    return balance;
}

i did debug to my program, checking each code.

I find the problem is at switch statement and deposit function

case statements

1
2
3
4
5
6
7
8
9
10
11
case 2: 
			readBalance(balance);
			menu(balance);

		case 3:
			withdraw();
			menu(balance);
					
		case 4:
			deposit(balance);
			menu(balance);


deposit function.

1
2
3
4
5
6
balance = addSum + balance;
			  
				 myfile << balance;
				
				 cout << balance << '\n';
			  


Because when i return back to the menu to make a new deposit, case statement needs to store the value that was added to the balance.
Well the switch wasn't even shown in your code, and to be fair I agree with Catfish666 on quite a few points.

You could simply pass a reference and handle all the required data in the function you need.

Catfish666 wrote:
You are going a bit overboard with indentation style. The tabs waste horizontal space and make your code look ugly.

Although I don't particularly agree with spaces over tabs or vice versa you have gone a little overboard... Even telling Chrome to show tabs as 4 spaces (like my compiler does) it does look excessively spaced.
The obvious mistake in switch() is that you don't break the cases.
Because of this, your cases "cascade".

For example, if you first enter case 3, you'll also enter case 4.
For example, if you first enter case 2, you'll also enter case 3 and also enter case 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
		case 2: 
			readBalance(balance);
			menu(balance);
			break;

		case 3:
			withdraw();
			menu(balance);
			break;
	
		case 4:
			deposit(balance);
			menu(balance);
			break;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int i = 0;

    switch (i)
    {
        case 5: std::cout << "Don't print this!";
        case 0: std::cout << "Hello ";
        case -1: std::cout << "C/C++ ";
        case 283: std::cout << "World";
        default: std::cout << "!\n";
    }
}
Hello C/C++ World!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	case 1:
			unscreen();
			menu(balance);
				
		case 2: 
			readBalance(balance);
			menu(balance);

		case 3:
			withdraw();
			menu(balance);
					
		case 4:
			balance = deposit(balance);
			currentBalance(balance);
			menu(balance);
I had tabs so is easy to read.

but my problem aren't the tags.
Thanks guys, I'm going to read the links that are sent to me, which are always useful.

Learn more about function and parameter and have full knowledge how functions and parameters works.

Show my older brother, that girls can too learn c++.
For example, if you first enter case 3, you'll also enter case 4.
For example, if you first enter case 2, you'll also enter case 3 and also enter case 4.


So all case statements needs to be, the same.
So all case statements needs to be, the same.

What? No, not at all. That would be pointless!

You need to actually read what Catfish666 wrote. S/he showed you exactly what to do. Any tutorial or textbook section on switch statements would also tell you the same.
misunderstood, sorry.
Okay please excuse me for my ignorance but I haven't experimented too much with cases, and just taken it as good practice to break all my cases (which of cause reduces chance of errors) but I thought in this kind of case it'll only speed performance up rather than do anything else.

If the value is 2 then case 1 is tested and is false, case 2 is carried out, once completed case 3 is tested and is false, case 4 is tested and is false, etc.
Is this not correct?
from what i understood, from catfish. If you press case 2, it will cascade case by case, from case 2 to case 4. Break helps cascade from helping.

The obvious mistake in switch() is that you don't break the cases.
Because of this, your cases "cascade".

For example, if you first enter case 3, you'll also enter case 4.
For example, if you first enter case 2, you'll also enter case 3 and also enter case 4.


Also since i add break on the case statements i find much quicker to program to load.
case's are not tests. They're more like labels that are goto'd.

The switch statement will examine/test the variable exactly once: When the switch starts. At which point it will jump to the appropriate case (or default) label. After that... once the code is actually inside the switch block... the case labels themselves are meaningless and code will flow right through them.
Last edited on
case's are not tests. They're more like labels that are goto'd.

The switch statement will examine/test the variable exactly once: When the switch starts. At which point it will jump to the appropriate case (or default) label. After that... once the code is actually inside the switch block... the case labels themselves are meaningless and code will flow right through them.

And hence the need to add the break; - because a break jumps out of the switch block, just like it jumps out of a for or while loop. So it stops the execution carrying on past the next case label.
Thanks a lot Disch, that makes sense to me :)
Topic archived. No new replies allowed.