Add and minus from the text file arent give the right value.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Add and minus from the text file arent give the right value.

  1. #1
    Join Date
    Feb 2014
    Posts
    1

    Add and minus from the text file arent give the right value.

    I'm going to try explain the best way i can.

    I'm creating a bank system.

    So I know to make a deposit to the balance, which add ups what is the balance to i have add.

    When i run the deposit function, its work well in some ways. If balance(text file) has the value 10, add i addSum 20, the balance will become 30, same as the text file will become 30. so its work well to add positive number.

    Code:
    double deposit(double balance)
    
    {
    
    	double addSum = 0;
    
    			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';
    
    			system("CLS");  
    			return balance;
    				 
    	
    }
    When I withdraw from 30 which is the balance, then i takeSum, for example i take away 30. The balance will become 30 - 30 = 0
    When i make another withdraw from example -150, it will be -150.

    Which shows correct.

    But when i make a deposit from -150 and i addSum 130, the balance shows -500, and it should had been -20.

    Code:
    double withdraw(double balance)
    
    {
    double takeSum = 0;
    
    			system("CLS");
    			cout<< "Welcome to withdraw."<<endl;
    			cout<<"Enter a sum you wish to take away from your account:";
    			
    				cout << balance << '\n';
    
    				 ofstream myfile;
    				 myfile.open ("money.txt");
    			
    				 cin>> takeSum;
    
    				 balance = balance - takeSum;
    			  
    				 myfile << balance;
    				
    				 cout << balance << '\n';
    
    			system("CLS");  
    			return balance;
    }
    My case statement to understand better.

    Code:
    int main()
    			
    {
    	double balance = 0;
    	balance = currentBalance(balance);
    	menu(balance);
    	return 0;
    }		
    
    	// menu function
    
    	int menu(double balance)
    {
    	
    		int menuOption = 0;
    			
    					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 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>>menuOption;
    	
    	switch(menuOption)
    	{
    		case 1:
    			unscreen();
    			menu(balance);
    			break;
    				
    		case 2: 
    			readBalance(balance);
    			menu(balance);
    			break;
    
    		case 3:
    			balance = withdraw(balance);
    			readBalance(balance);
    			menu(balance);
    			break;			
    					
    		case 4:
    			balance = deposit(balance);
    			readBalance(balance);
    			menu(balance);
    			break;
    							
    		case 9:
    			exit(0);
    			
    		default:
    			cout<<"You did not choose from the menu options, reloading the menu!"<<endl;
    			main();	
    
    			return 0;
    	}
    		
    }
    What is causing this problem, also when function deposit and withdraw close, it goes to readBalance function, should go to menu.


    Code:
    double readBalance(double balance)
    {
    
    		int option;
    
    		system("CLS");
    		cout<<"Welcome to balance."<<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';
    
    
    		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);
    			}
    			
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Add and minus from the text file arent give the right value.

    Did you try to debug your code step-by-step?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    Re: Add and minus from the text file arent give the right value.

    Why are you recursivly calling menu() after every action? You want a loop that gets and processes a menu option until option is exit.

    In pseudo code something like this
    Code:
    do {
        show menu
        get menu option
        if valid menu option
             process option
        else
            display message
    while option not exit
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center