Cannot verify selected GetLogicalDrive()

Hi everyone,
i have spent quite a few hours trying to solve this puzzle.. for some reason i cannot test for a matching string i input via the console app i am writing with an 'If' query. The returned values are always everything except what i need "the typed in Drive" i.e. 'F:'.

I hope that makes sense.

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
  TCHAR szDrive[] = _T(" A:");
	uLogicDriveList = GetLogicalDrives(); // re-initialise uLogicDriveList
	if (uLogicDriveList == 0)
	{
		printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError());
	}
	else
	{
		
		printf("Please type your desired Drive: ");
		cin >> szDriveString;
		printf("\n");
		while (uLogicDriveList)
		{
			if (uLogicDriveList & 1)
			{
				if(szDriveString == szDrive) // when i comment //this if(szDriveString) statement out it displays all my Logical drives.
				printf("Selected Drive is %s\n", szDrive);
			}
			else
			{
				if (szDriveString != szDrive)
					printf("%s No\n", szDrive);
			}
		
			++szDrive[1];
			uLogicDriveList >>= 1;
		}
		//printf("%s Selected: ", szDrive);
	}
What type is uLogicDriveList? It affects the right shift, and thus the while loop.

As I recall, GetLogicalDrives() returns a bit for the relevant drive, so you need to test the bit positions.

What type is szDriveString and what is it's content? The type will determine what szDriveString == szDrive means, and that determines what you see printed.
Last edited on
If szDriveString is a char array then you need to compare the strings with strcmp instead of ==.

I see that szDrive has a leading space. When you read szDriveString at line 11, the stream will probably skip leading spaces, even if you put them there.
Thanks for the fast response.

Sorry i missed that i hadn't displayed the types;

TCHAR szDriveString[] = _T(" A:");
DWORD uLogicDriveList = GetLogicalDrives();
and szDriveString is also DWORD.
Last edited on
You've not made it easy for yourself with those variables.

You really want to check the ith bit where i is ( DriverLetter - _T('A') ) where DriverLetter is a TCHAR.

So you:
1. determine i.
2. right shift uLogicDriveList by i
3. check bit 0
Topic archived. No new replies allowed.