Hey, Im having some trouble with my code,
it doesn't seem to stop to wait for an answer to the question about modifying the phone list when i input values in above and press Ctrl Z enter, however the code works fine
when i remove the code about inputting information.
#include <iostream>
#include<fstream>
#include<string>
#include <cstdio>
using namespace std;
int main(){
ofstream fout("f:\\telr.txt",ios::app);
string ln, fn, tel; //lastname, first name, tele
cout << "give last name, first name aand tel. no: "
<< "terminate with Ctrl-Z" << endl;
while(cin >> ln && cin >> fn && cin >> tel)
fout << ln << ' ' << fn << ' ' << tel << endl;
fout.close();
ifstream fin("f:\\telr.txt", ios::in);
cout << "This is the telephone list:" << endl;
cout << fin.rdbuf();
fin.close();
string fn_input;
string yes = "Y";
string answer;
cout << "Do You want to modify the phone list (Y/N):";
cin >> answer;
cout << answer;
if(yes.compare(answer) == 0){
cout << "here";
ofstream fnout("temp.txt",ios::app);//creates/opens a temporary file
ofstream fout("f:\\telr.txt",ios::app);//opens the location of the permanent file
cout << "Enter the first name:" << endl;
cin >> fn_input;
if (fout.is_open()){//only continue this subroutine if it has been opened correctly
ifstream fin("f:\\telr.txt", ios::in);
fin >> fn >> ln >> tel;//scans in the first name, last name, tel in that format!
do{
if (fn.compare(fn_input) == 0){//scans through permanent copy until finds a match (compare function)
cout << "Name: " << fn << " " << ln<< endl;
cout << "New number: ";
cin >> tel;
fnout << fn << " " << ln << " " << tel << endl;//outputs the new number and all the name information to the temporary file
}
if (fn.compare(fn_input) != 0){//if there is no match, output the data scanned in from the permamnt copy to the temporary file
fnout << fn << " " << ln << " " << tel << endl;//and then continue the scanning cycle
}
fin >> fn >> ln >> tel;
}while(!fin.eof());//continue to scan the document until the end of the file flag is activated
std::ofstream file("f:\\telr.txt", std::ios::trunc);//clears the permanent copy of all the data inside
ifstream fitn("temp.txt", ios::in);
fitn >> fn >> ln >> tel;
do{//this routine copies all the data from the temporary file to the permamnt one
fout << fn << " " << ln << " " << tel << endl;
fitn >> fn >> ln >> tel;
}while(!fitn.eof());
}
else cout << "Unable to open file";
fout.close();//closing files in use
fnout.close();
remove("temp.txt");//removes the temp file
return 0;
}
}