This is my first time programming with C++ after taking 3 classes in Java (so, definite beginner here). The homework is prompting me to append to a text file and then to search within the text file for a name (in this case, my name). It was working perfectly then I was trying to get it to show my whole name rather than just my last name, so I tinkered with it. Now I can't figure out why the result is always "Name not found" despite me putting in my name to get the "Name found is: " result. (Also, the homework wanted the "Name not found" added to a different .txt file, so that's why they're named differently. That works just fine.) Thanks so much!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (){
ifstream inFile("/Users/katelynwalker/Documents/CSC450_CT5_mod5.txt");
ofstream outFile;
outFile.open("/Users/katelynwalker/Documents/CSC450_CT5_mod5.txt", ios::app);//appends instead of overwrites
outFile<<"Katelyn Walker";
cout<<"Name has been added to the file. ";
outFile.close();
string search;
cout<<"Please enter a name: ";
cin>>ws;
cin>>search;
bool isFound = 0;
string name = " ";
while(getline(inFile, name)){
for(int i=0; i<search.size(); i++){
if(name[i]==search[i]){
isFound = 1;
}
else{
isFound = 0;
}
}
if(isFound){
cout<<"Name found is: ";
for(int i=search.size()+1;i<name.size();i++)
cout << name[i];
break;
}
}
if(inFile.eof()&&(!isFound)){
outFile.open("/Users/katelynwalker/Documents/CSC450-not-found_CT5_mod5.txt", ios::app);
outFile<<"Name not found.";
cout<<"Name not found.";
}
inFile.close();
return 0;
}