1

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;
}
katelyn
  • 29
  • 6
  • 2
    The programmer's secret weapon is the debugger. Fire up your program in whatever debugger came with your development environment, place a breakpoint in the code just before things seem to go south, run the program and step through the code to see what really happened. If you don't know how to use a debugger, learn. It's possible that you will never again spend so little time saving so much time. – user4581301 Jun 17 '20 at 18:59
  • The compiler isn't returning "name not found", your *code* is. Just saying. Regarding searching for your full name, something to consider: What do you think `cin>>search;` actually reads when you type `Katelyn Walker` at the prompt? Hint: it isn't `Katelyn Walker`, and is probably worth printing and/or viewing in a debugger. – WhozCraig Jun 17 '20 at 19:00
  • The compiler isn't returning "name not found", your *code* is. Just saying. Regarding searching for your full name, something to consider: What do you think `cin>>search;` actually reads when you type `Katelyn Walker` in at the prompt? Hint: it isn't `Katelyn Walker`, and is probably worth printing and/or viewing in a debugger. – WhozCraig Jun 17 '20 at 19:01
  • @HAL9000 thank you so much! I was going crazy as to why it wasn't working – katelyn Jun 17 '20 at 20:17

1 Answers1

1

If you want your name to be written on a single line, you should write a complete line, including a newline:

outFile<<"Katelyn Walker" << std::endl;

And when asking for a name to search for, std::cin >> search wont let you enter spaces. If you don't want spaces, then that is ok, otherwise you should probably use std::getline(std::cin, search);

Unrelated to your question, don't use using namespace std; it is a bad habit.

HAL9000
  • 2,138
  • 1
  • 9
  • 20
  • Side note: `std::endl` is more than a mere newline. It also flushes the stream, an expensive behaviour you may or may not want. If you just want a new line, use `'\n'`. – user4581301 Jun 17 '20 at 19:18
  • thank you!!! I had no clue what the \n was, so thank you so much! – katelyn Jun 17 '20 at 20:17