Link to home
Start Free TrialLog in
Avatar of Unreal7000
Unreal7000

asked on

Variable Drive letter in C++

I am using Borland Turbo C++. Here is the source code from the program I am writing:


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <process.h>
main()
{
      int game;
      char x;

      cout <<"What drive is Cd-Rom?\n";
      cin >> x;

      cout <<"Select Game to install\n";
      cout <<"Press (0) to quit\n";
      cout <<"\n1. Demolition Racer\n";
      cout <<"2. Fighter Ace 2\n";
      cout <<"3. Final Fantasy 8\n";
      cout <<"4. Free Space 2\n";
      cout <<"5. Interstate 76\n";
      cout <<"6. Interstate 76 Arsenal\n";
      cout <<"7. Interstate 82\n";
      cout <<"8. Midtown Madness\n";
      cout <<"9. Quake 3\n";
      cout <<"10. Quake\n";
      cout <<"11. Redline Racer\n";
      cout <<"12. Sin\n";
      cout <<"13. System Shock 2\n";
      cin >> game;

if (game==0)

if (game==1)
{
 system("e:\\games2\\demol.exe");
}

if (game==2)
{
 system("e:\\games2\\fight.exe");
}

if (game==3)
{
 spawnl(P_WAIT, "c:\\progra~1\\winzip\\winzip32.exe","-a","e:\\games2\\final.zip", NULL);
}
         
if (game==4)
{
 system("e:\\games2\\Free2.exe");
}

if (game==5)
{
 system("e:\\games2\\I76.exe");
}

if (game==6)
{
 system("e:\\games2\\I76A.exe");
}

if (game==7)
{
 system("e:\\games2\\I82.exe");
}
if (game==8)
{
 system("e:\\games2\\Mid.exe");
}

if (game==9)
{
 system("e:\\games2\\Q3.exe");
}

if (game==10)
{
spawnl(P_WAIT, "c:\\progra~1\\winzip\\winzip32.exe","-a","e:\\games2\\quake.zip", NULL);
}

if (game==11)
{
 system("e:\\games2\\Red.exe");
}

if (game==12)
{
 system("e:\\games2\\Sin.exe");
}

if (game==13)
{
 system("e:\\games2\\SS2.exe");
}

return 0;
}


What i want it to do is use the drive letter for cd-rom that the user inputs. For example:

char x;
cout <<"What is drive letter";
cin x;
system("x:\\games2\\SS2.exe)

But it wont work this way. It trys to read x:\ as cd-rom. I want it to read what the user inputs.

Thank You
Avatar of laeuchli
laeuchli

try this:
char test[3];
cout<<"What Drive";
cin>>test;
strcpy(test,":\\games2\\SS2.exe");
Avatar of Unreal7000

ASKER

Thanks a bunch but it doesnt seem to work. Does there have to be a certain header file???
yes is has a header file, I will post it.
in the first place, I used the wrong function, you I meant strcat not strcpy.
You want strcat(test,":\\games2\\SS2.exe");
Then you need: #include<string.h>
But test is rather short, don't you think?

Why not insert the character into the string?  like

char x;
char Command[] = "x:\\games2\\SS2.exe";

while (true)
{
   cout <<"What is drive letter";
   cin >> x;
   x = toupper(x);
   if ('A'<= x &&  x <= 'Z')
      break;
   cout << "Invalid drive letter." << endl;
}

Command[0] = x;
system(Command) ;

Since I think Unreal7000  is a beginner, I wanted to keep if from being to complex. My code might be a tad easier for a beginer to understand :-)
laeuchli- I still cant get it to work for some reason.Thanks anyways for the help.
The code posted above has a runtime error. The conditionals beyond if (game==0) are never evaluated. The above code is actually :

if (game==0)
  {
   if (game==1)
     {
      system("e:\\games2\\demol.exe");
     }
   if (game==2)
  {
   system("e:\\games2\\fight.exe");
  }
 }

(Think about using a switch statement)

switch(game)
 {
  case 1 :
      system("e:\\games2\\demol.exe");
      break;
  case 2:
      system("e:\\games2\\fight.exe");
      break;
 default :
}

Also, a char will be promoted to an int on some compilers. So, x,
   cin >> x;
may have an extra control character at the end of it. It will need to be stripped out;

Command[0] = x;
Command[1] = '\0';

----
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <process.h>
#include <string.h>  // <-- Add

main()
{
      int game;
      char x;
      char Command[255]; // <- Add

      cout <<"What drive is Cd-Rom?\n";
      cin >> x;

      cout <<"Select Game to install\n";
      cout <<"Press (0) to quit\n";
      cout <<"\n1. Demolition Racer\n";
      cin >> game;

      Command[0] = x;
      Command[1] = '\0';


      if (game==0); // <-- Needs to be fixed ;

      if (game==1)
        {
         strcat(Command,"e:\\games2\\demol.exe");
        }

      system(Command);



return 0;
}


laeuchli, Perhaps, but also buggy--test is too short.

dharmour, isn't that basically what laeuchli said?  To use strcat() to tack the command onto the drive string?
Oh Duh! I feel dumb. Incress the size of test to something like test[15], or use nietods method. Thanks for pointing that out nietod. I forgot that test was going to hold something other then the drive letter.
dharmor you are snitching my question!
Or at least his answer.
true:-)
I was just answering the question with a working answer. Relax people, take the points for all I care, I was just helping out.

Errata :
1)
if (game==1)
        {
         strcat(Command,"e:\\games2\\demol.exe");
        }
     
should be

if (game==1)
        {
         strcat(Command,":\\games2\\demol.exe");
        }

2) The if(game==0) will only prevent if(game==0) from being evaluated not all of them.      
>> Relax people, take the points for all I care
Its not always the points.  Sometimes it is "insulting" to propose an answer, then have it rejected (usually because the client doesn't understand it or something else (unrelated) is wrong with their code)  Then have another expert come along and post the same point and get credit (i.e. recognition).  It is the EE equivalent of plagurism.  (Not to suggest that you were snubbing laeuchli, but regardless of intentions, that is often the result.)
Providing code to clarify the resolution to the question is equivalent plagurism?

I see, I did select Propsed answer instead of Comment. It was 1:00 AM.
:)

I won't take this any further,
If you post someone else's idea, or even just build on it, it is best to give recognition to that expert.  i.e.   say soemthing like "as XXXX said" etc.  and don't submit that sort of thing as an answer unless you have substancially built on it, that is unfair.
the program from where you want to get the driverletter is on the cd, isn't it?

so the driver letter from where the program was started would also work.

This one is quite easy to get. Change your main function to this:

int main (int argc, char **args)
{
  char driveletter = args[0][0];

  // your code here  

  return 0;
}


in args[0] contains the full path/filename of your executable. and if your program is on the cd, the first char will be the driveletter of your cd-rom... this is the most painless way to get the cdrom driveletter. there are other ways to get it, but these are os dependent..


Nils


ups.. sorry

I didn't understood your answer.. sorry about that. I thought you was looking for the driver letter, not to get it into your string.

I've seen two nice answers to this in the topic..

sorry, for locking the question again,
  Nils
ups.. sorry

I didn't understood your answer.. sorry about that. I thought you was looking for the driver letter, not to get it into your string.

I've seen two nice answers to this in the topic..

sorry, for locking the question again,
  Nils


btw. I can't withdraw the answer at the moment.. EE gives me a message if I try to.. I'll try it again in a couple of hours..
ehm..


looks like EE does some fancy things at the moment.. I'll better logout :)
ASKER CERTIFIED SOLUTION
Avatar of laeuchli
laeuchli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial