| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Can anyone help me with this
hi how r u?
i try to open file but i cant open it and compute. could u please help me? #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { int type; int account; double balance; double amount; double y,n,x; { ofstream myfile; myfile.open("saif.txt"); cout<<"Enter account number , balance , Type , Amount"<<endl; cout<<"Enter end-of-file to end file"<<endl; myfile.close(); while(cin>>account>>balance>>type>>amount) { myfile<<account<<" "<<balance<<" "<<type<<" "<<amount<<endl; } } { ifstream myfile ("saif.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { ifstream myfile ("saif.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { cin>>account>>balance>>type>>amount; } myfile.close(); } else cout << "Unable to open file"; } myfile.close(); } else cout << "Unable to open file"; if (type==0) y=balance-amount; if (type==1) y=balance+amount; if(type==2) y=amount/2; cout<<amount<<" "<<setprecision(2)<<fixed<<y<<endl; if(y>1000) { x=y*.01; n=x+y; cout<<"This acouut has 1% to the balance and became : "<<n<<endl; } if(y<0) { cout<<setprecision(2)<<fixed<<y<<"Warning Balance is negative you will go to jill"<<endl; } } return 0; } |
|
#2
|
||||
|
||||
|
Quote:
Quote:
sure You are getting error because in between you are closing your file (see your code myfile.close) and then continuing with input operationn. then at the end you are checking through if-lause whether your file is open or not.Answer is not open due to above stated reason. |
|
#3
|
|||
|
|||
|
Quote:
thanks cirus i removed myfile.close but not open try in your computer and tell what is the erorr ok ![]() |
|
#4
|
|||
|
|||
|
Can you indent your code please I am suspecting error of braces.The essence is that at some point the file is still open.That is why you are not being able to open it.
|
|
#5
|
|||||
|
|||||
|
Quote:
After this step you have not opened myfile for input.Hence all operations will be igneored. Ignore my previous reply where I asked you to remove myfile.close(). I thought that you were using fstream.h After this line , open your myfile by Code:
mfile.open("saif.txt")
Quote:
//Why this step when you have already created an ifstream object.??????? Quote:
First open your file after declaring it as an ifstream object. the line if (myfile.is_open()) will give you FALSE as answer and hence else clause will get executed. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Can anyone help me with this |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|