Author Topic: Any C++ people? Need quick help.  (Read 1282 times)

Quote
1.    HANDLE hFind; //New Handle called hFind
2.    WIN32_FIND_DATA data;
3.
4.   string searchPath = "*.txt"; //Extension
5.
6.    hFind = FindFirstFile(searchPath.c_str(), &data); //"searchPath.c_str()" - converts char* to string I guess
7.    if (hFind != INVALID_HANDLE_VALUE)
8.    {
9.        do
10.        {
11.            cout << ("%s\n", data.cFileName) << endl; //Print file names
12.            files.insert(files.end(), data.cFileName); //Adds each file name to a list.
13.        }while (FindNextFile(hFind, &data));
14.        FindClose(hFind);
15.    }
16.
17.    cout << endl << "File Name:";
18.
19.    getline(cin, userfilechoice); //Get file name from user, getline since cin alone only gets the first word, getline gets the whole line the user inputs
20.
21.   if (find(files.begin(), files.end(), userfilechoice) != files.end()) //Searches list "files" for the name the user inputs, if it doesn't find it this executes
22.    {
23.        cout << "File does not exist." << endl;
24.        Sleep(500);
25.        flagsremover();
26.        ClearScreen();
27.        main();
28.        return 0;
29.    }

Yes the variables/functions were defined before, this is just a part of the code.
The whole code compiles correctly and runs fine, except that the if statement skips. I am wondering why.
The problem is on line 21. It checks if the input from the user is inside a list called "files"
Stuff is added into the list with a loop on line 12. I'm using cFileName (char*) and stuffing it into a list (string).
No matter what the user inputs it skips the if statement, whether what they inputted is in the list or not.

If you don't understand that, my question is how exactly do I search a list for a string? If that's the way to do it, what's wrong? Is it that cFileName is a char*? But no errors are given that I'm blatantly converting char* to string when putting them into a list.

I shouldn't have posted this at night when most people are asleep, this is bugging me and I want it fixed.

have you tried rebooting


Fixed it. All I had to do was change

Quote
if (find(files.begin(), files.end(), userfilechoice) != files.end())
to
Quote
if (find(files.begin(), files.end(), userfilechoice) == files.end())
forget C++, it doesn't even make any sense