****I am having trouble with this program. My error is when i run the program it
ID: 3825707 • Letter: #
Question
****I am having trouble with this program. My error is when i run the program it outputs the right numbers for the re-entered address name (address 2 & name2) but not the original address and name (address & name). The file appears to be blank. Is this something to do with the open and close file ?
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string name,name2,line;
string address, address2;
string s;
ifstream infile;
ofstream outfile1;
ofstream outfile2;
outfile1.open("info.txt");
if(!outfile1)
{
cout<<"File does not exist."<<endl;
exit(0);
}
outfile2.open("newer_info.txt");
if(!outfile2)
{
cout<<"File does not exist."<<endl;
exit(0);
}
outfile1.close();
infile.open("newer_info.txt");
if(!infile)
{
cout<<"File does not exist."<<endl;
exit(0);
}
cout<<"Please enter your name: "<<endl;
getline(cin, name);
outfile1<<name<<endl;
cout<<"Please enter the street address: "<<endl;
getline(cin, address);
outfile1<<address<<endl;
system("CLS");
cout<<"Would you like to update the street address? ";
getline(cin, s);
if(s == "yes" || s=="Yes")
{
cout<<"Current name and address: "<<endl;
getline(infile, name);
outfile1<<name<<endl;
outfile1<<address<<endl;
cout<<name<<endl;
getline(infile, address);
cout<<address<<endl;
cout<<"Please enter the new name: ";
getline(cin, name2);
outfile2<<name2<<endl;
cout<<"Please enter the new address: ";
getline(cin, address2);
outfile2<<address2<<endl;
cout<<"Information has been successfully written in newer_info file."<<endl;
}
else
{
cout<<"Thank you for using this program..!!!"<<endl;
return 1;
}
infile.close();
outfile1.close();
outfile2.close();
return 0;
}
Explanation / Answer
YES, You have closed the file and doing operation on it, I have commented out the code, Run it and let me know if it works, It works for me
#include <iostream>
using namespace std;
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string name,name2,line;
string address, address2;
string s;
ifstream infile;
ofstream outfile1;
ofstream outfile2;
outfile1.open("info.txt");
if(!outfile1)
{
cout<<"File does not exist."<<endl;
exit(0);
}
outfile2.open("newer_info.txt");
if(!outfile2)
{
cout<<"File does not exist."<<endl;
exit(0);
}
//outfile1.close(); //Commented out, you can remove this line
infile.open("newer_info.txt");
if(!infile)
{
cout<<"File does not exist."<<endl;
exit(0);
}
cout<<"Please enter your name: "<<endl;
getline(cin, name);
outfile1<<name<<endl;
cout<<"Please enter the street address: "<<endl;
getline(cin, address);
outfile1<<address<<endl;
system("CLS");
cout<<"Would you like to update the street address? ";
getline(cin, s);
if(s == "yes" || s=="Yes")
{
cout<<"Current name and address: "<<endl;
getline(infile, name);
outfile1<<name<<endl;
outfile1<<address<<endl;
cout<<name<<endl;
getline(infile, address);
cout<<address<<endl;
cout<<"Please enter the new name: ";
getline(cin, name2);
outfile2<<name2<<endl;
cout<<"Please enter the new address: ";
getline(cin, address2);
outfile2<<address2<<endl;
cout<<"Information has been successfully written in newer_info file."<<endl;
}
else
{
cout<<"Thank you for using this program..!!!"<<endl;
return 1;
}
infile.close();
outfile1.close();
outfile2.close();
return 0;
}
Thanks, let me know if there is any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.