Function error

I don't know where to input a certain file name into the function so that the function searches for the certain file. If you were trying to find a certain file on your computer, I think this would find it for you. I just don't know how to make it so that whatever you type in is searched for. For reference, here's the function.


1
2
3
4
5
6
7
8
9
10
WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile("C:\\*",&file);
    if (search_handle)
    {
        do
        {
            std::wcout << file.cFileName << std::endl;
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);
}



PS: I already know how to put what you type into a file, but I don't know where to put that file into this fuction.
Where the std::wcout statement is, you could use an if statement to compare the file name to the name of the file you are searching for.
This is my program so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cwchar>
#include <string>
#include <Windows.h>
using namespace std;

char response;
string a;


int main()
{
    

    cout << "Would you like to play a game? Y/N?";

    cin >> response;
    if (response == 'y');
        cout << "What game would you like to play? Please type in exactly what the game is called including spaces, dashes, or capitalization.";
    cin >> "a";

    WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile("C:\\*",&file);
    if (search_handle)
    {
        do
        {
            std::wcout << file.cFileName << std::endl;
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);


    }

    

    if (response == 'n')
        cout << "Goodbye!";

    //countdown loop
  for (int n=3; n>0; n--) {
    cout << n << ", ";
  }
  cout << "CIAO!\n";
  return 0;
}



What I don't know is how to make the function work. Also, I don't know where to substitute 'a' into the function. I would be grateful for advice.
Jace wrote:
cin >> "a";
How do you ask the user to input what the value of a constant should be? This code is incorrect, you cannot input to a string like that. Perhaps you wanted to create a variable named a and input to that? Why name it a?
Last edited on
what should it be then?
cin >> a;
I still don't know where to substitute file 'a' into the function.
Replace:

std::wcout << file.cFileName << std::endl;

with:

1
2
3
4
if(file.cFileName == a)
{
    std::cout << "Found the game you wanted to play!" << std::endl;
}




I would like to know how you got started on this program if you cannot even perform simple logical replacement ;)
Let's just say I'm a beginner but I'm trying to learn by diving in programs and getting help by oldies like you. ;]
You don't learn how to build a house before you learn how to build a triangle ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile("C:\\*",&file);
    if (search_handle)
    {
        do
        {
            if(file.cFileName == a)
{
    std::cout << "Found the game you wanted to play!" << std::endl;
}
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);


    }


I will take your advice of learning how to build triangles before houses as soon as I tackle this one last issue. When I use this function as soon as I type in the game I'm looking for it exits out and nothing else is executed.
Nothing is executed because it only prints out that it found the game. It continues looping through all files and then the program ends.

If you want it to actually run the game, look up ShellExecute.
Is this the Shellexecute thingymabobber you were talking about?

1
2
3
4
5
6
7
8
HINSTANCE ShellExecute(  //The ShellExecute is marked as incorrect (red)
  _In_opt_  HWND hwnd,
  _In_opt_  LPCTSTR lpOperation,
  _In_      LPCTSTR lpFile,
  _In_opt_  LPCTSTR lpParameters,
  _In_opt_  LPCTSTR lpDirectory,
  _In_      INT nShowCmd
);


That's the declaration, that's not an example of how to use it.
Say what? You just blowed my mind......
1
2
3
4
5
6
7
8
9
10
11
12
int example(int x, int y); // "declaration"

//...

int x = example(3, 4); // "how to use it"

//...

int example(int x, int y)
{ // "definition"
    return x + y;
}


http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
At the top is the "declaration"
Under "Remarks" is the "how to use it"
Last edited on
I have no idea what that means.... so far my program looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cwchar>
#include <string>
#include <Windows.h>
using namespace std;
#include <shellapi.h>
#pragma comment(lib,"shell32.lib")
static HWND hBut;
#define ShellExecute;

char response;
string a;


int main()
{
    

    cout << "Would you like to play a game? Y/N?";

    cin >> response;
    if (response == 'y');
        cout << "What game would you like to play? Please type in exactly what the game is called including spaces, dashes, or capitalization.";
    cin >> a;

    WIN32_FIND_DATA file;
    HANDLE search_handle=FindFirstFile("C:\\*",&file);
    if (search_handle)
    {
        do
        {
            if(file.cFileName == a)
		CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
		ShellExecute(handle, "find", <fully_qualified_path_to_folder>, NULL, NULL, 0);
ShellExecute(handle, NULL, <fully_qualified_path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

{
    std::cout << "Found the game you wanted to play!" << std::endl;
}
        }while(FindNextFile(search_handle,&file));
        FindClose(search_handle);


    }

    system("pause");

    if (response == 'n')
        cout << "Goodbye!";

    //countdown
 Sleep (1 * 1000);
	 cout << "5"<<endl;
	  Sleep (1 * 1000);
	  cout << "4"<<endl;
	   Sleep (1 * 1000);
	   cout << "3"<<endl;
	    Sleep (1 * 1000);
		cout << "2"<<endl;
		 Sleep (1 * 1000);
		 cout << "1"<<endl;
		 Sleep (1 * 1000);
  cout << "CIAO!\n"<<endl;
  Sleep (1 * 1000);
  return 0;
}
Topic archived. No new replies allowed.