0

I have an assignment where I must create a function that takes a filename as a parameter, opens the file, asks a user to enter a value to be searched for, and then searches the file for that value. The file I was given to use for this assignment is a file with a list of revenue and expense values. I have tried just about everything and keep receiving the "value not found" prompt even when I enter a value that i know is in the file.

The code is

void numberSearch(string fileName)
{
string searchVal;

cout << "\nWhat value would you like to search for?\n";

cin.ignore(); 
getline(cin, searchVal);

ifstream file; //create input file object that will be read from
file.open(fileName);   //"ifstream file (fileName)"

if (!file)
{
    cout << "\nUnable to open file.\n";
    exit(1);
}

string words;

int curLine = 0;  //file line counter

while (getline(file, words))  
{
    ++curLine;  //counts each line in the file

    if (words.find(searchVal) != string::npos) 
    {
        cout << "\nLine " << curLine << " contains " << searchVal << endl;

        file.close();
        return; 
    }

    else
    {
        cout << "\nThe value " << searchVal << " was not found.\n";

        file.close();
        return; 
    }
}
}

Any help is greatly appreciated

gonzato
  • 11
  • 3
  • if searchval is not in string, it will return contains? (!= instead of ==) – Charlie Sep 27 '16 at 03:29
  • if searchval is found in string, the "!= string::npos" part, its supposed to return the value and which line that value was found on, and if it isnt found it returns that searchval has not been found. Would changing it to "==" instead of "!=" help? @Charlie – gonzato Sep 27 '16 at 03:32
  • why the `cin.ignore(); ` ? – apple apple Sep 27 '16 at 03:32
  • @appleapple without cin.ignore() the program didnt stop and wait for input and it immediately said "Line *blank* contains" and that was all. and no it should just search for numbers in the format of "200.00", i was trying to get the double values entered as strings so it was searching for numbers but as string data types – gonzato Sep 27 '16 at 03:34
  • and do you need to search a string that contains space? – apple apple Sep 27 '16 at 03:34
  • if your program act like that, it's not this part of code that cause this. – apple apple Sep 27 '16 at 03:35
  • than you can use `cin>>string_variable` and no `ignore` or `getline` need – apple apple Sep 27 '16 at 03:36
  • and please do not reply by modify your comment, I almost miss it. – apple apple Sep 27 '16 at 03:37

2 Answers2

0

You need to put the else part out side of while loop. Otherwise your function will only search for the first line.

apple apple
  • 10,292
  • 2
  • 16
  • 36
0

I was bored so I decided to do it too. I'll post mine, even though its already solved. (upvoted the question for the fun of solving ;) )

using namespace std;

int testfile(string filename, int &line, int &character)
{
    ifstream is(filename, std::ios::in);
    if (!is.is_open()) return 1; //1 = no file

    cout << "Search for what value?" << endl;
    string value;
    cin >> value;

    string buf;

    while (getline(is,buf))
    {
        ++line;
        if (buf.find(value) != buf.npos)
            {
                character=buf.find(value); //the part that got lost in edit
                return 0; //value found, returning 0
            }
    }

    return 2; //return 2 since no value was found
}

which is called under main():

main()
{

    int line=0; //what line it is
    int character=0; //what character on that line

    int result=testfile("test.txt", line, character); //debug+passing as reference

    if (result == 1)cout << "could not find file" << endl;
    if (result == 2)cout << "could not find value" << endl;

    if (result == 0)
        cout << "found at line# " << line << " character# " << character << endl;

    return 0;
}

Passing values by reference lets us make use of them in our original scope. Therefore the function can both give errors for debugging, and allow useful results for our scopes purpose.

Closing the fstream is not necessary, as leaving the scope will take care of that for us: see here

Hehe, almost like being at school ;)

Community
  • 1
  • 1
Charlie
  • 585
  • 9
  • 17
  • Thank you! you made it look a lot easier than it actually was for me, haha! – gonzato Sep 27 '16 at 16:40
  • I found a mistake that got lost in editing, I forget to update the character value before returning 0, fixed it now tho @gonzato – Charlie Sep 28 '16 at 04:05