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.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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);
}
elseif (option == 0 )
{
return balance;
}
else
{
exit(0);
}
}
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.
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.
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.
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.
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.
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.
// 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;
usingnamespace 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);
}
elseif (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);
}
elseif (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);
}
elseif (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?
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.
> 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.')
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?