0

so i have a code that's supposed to find a string of characters in a certain .txt file, if the input is in the file, it says "yey i found it" but when it isnt, its supposed to say "didnt find anything", but it just skips that step and ends. I'm a beginner so sorry for any obvious mistakes.

#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main(void)
{
setlocale(LC_ALL, "");
string hledat;
int offset;
string line;

ifstream Myfile;
cout.flush();
cout << "Welcome, insert the string to find in the file. \n \n \n" << endl;
cin.get();
cout.flush();
Myfile.open("db.txt");


cin >> hledat;

if (Myfile.is_open())
{
    while (!Myfile.eof())
    {
        getline(Myfile, line);
        if ((offset = line.find(hledat, 0)) != string::npos)
        {
            cout.flush();
            cout << "Found it ! your input was  :   " << hledat << endl;
        }


    }
    Myfile.close();

}



else
{
    cout.flush();
    cout << "Sorry, couldnt find anything. Your input was   " << hledat << endl;
}

getchar();
system("PAUSE");
return 0;

}

raz
  • 75
  • 2
  • 12
  • 1
    Follow all the paths through the `if/else` blocks and figure out which one gives the results you see. – Mark Ransom Oct 31 '13 at 20:42
  • What is the contents of your input file? What string did you tell it to search for? What *exact* output did you expect, given that *exact* input? – abelenky Oct 31 '13 at 20:43
  • 4
    I'm curious. Who taught you to use `while (!file.eof())`? – David G Oct 31 '13 at 20:43
  • at first glance, you have a cin.get and cin >>hledat so basically you need to type twice... maybe thats the issue? – SwiftMango Oct 31 '13 at 20:45
  • I think the variable offset has a wrong type, should be size_t or something like that. – piokuc Oct 31 '13 at 20:47
  • @piokuc `std::string::size_type` is the correct type. However, this is not the issue here. – paddy Oct 31 '13 at 20:54

2 Answers2

0

There are three possible cases.

  1. The file was not successfully opened.
  2. The file was successfully opened, but the string was not found.
  3. The file was successfully opened, and the string was found.

You have a printout for cases 1 and 3, but not 2.

By the way, your loop condition is wrong. Use the result of the call to getline, which is the ostream object itself after the read attempt.

while (getline(MyFile, line))
{
    ...
}

The loop will terminate upon an unsuccessful read attempt, which will happen after you read the last line. The way you have it, you will try to read after the last line, which will be unsuccessful, but you will still try to process that non-existent line because you don't check eof until the loop starts over.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

Just comment out //cin.get(); , you dont need it.

Output:

Welcome, insert the string to find in the file.



apple
Found it ! your input was  :   apple

Other than that, it works like a charm.

Corrected code:

#include <stdio.h>

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main(void)
{
    setlocale(LC_ALL, "");
    string hledat;
    int offset;
    string line;

    ifstream Myfile;
    cout.flush();
    cout << "Welcome, insert the string to find in the file. \n \n \n" << endl;
    //cin.get();   <-----  corrected code
    cout.flush();
    Myfile.open("db.txt");


    cin >> hledat;

    if (Myfile.is_open())
    {
        while (!Myfile.eof())
        {
            getline(Myfile, line);
            if ((offset = line.find(hledat, 0)) != string::npos)
            {
                cout.flush();
                cout << "Found it ! your input was  :   " << hledat << endl;
            }


        }
        Myfile.close();

    }



    else
    {
        cout.flush();
        cout << "Sorry, couldnt find anything. Your input was   " << hledat << endl;
    }

    getchar();
    system("PAUSE");
    return 0;

} 
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • Awesome, thanks for sharing this ! Lets say i input something thats not in the file (theres just a column of names like "John, Rick" etc... So If I input George it skips the part "Sorry, couldnt find anything" – raz Oct 31 '13 at 21:17