[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
Unreal7000
Asked:
Unreal7000
  • 8
  • 5
  • 4
  • +2
1 Solution
 
laeuchliCommented:
try this:
char test[3];
cout<<"What Drive";
cin>>test;
strcpy(test,":\\games2\\SS2.exe");
0
 
Unreal7000Author Commented:
Thanks a bunch but it doesnt seem to work. Does there have to be a certain header file???
0
 
laeuchliCommented:
yes is has a header file, I will post it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
laeuchliCommented:
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>
0
 
nietodCommented:
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) ;

0
 
laeuchliCommented:
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 :-)
0
 
Unreal7000Author Commented:
laeuchli- I still cant get it to work for some reason.Thanks anyways for the help.
0
 
dharmorCommented:
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;
}


0
 
nietodCommented:
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?
0
 
laeuchliCommented:
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.
0
 
laeuchliCommented:
dharmor you are snitching my question!
0
 
nietodCommented:
Or at least his answer.
0
 
laeuchliCommented:
true:-)
0
 
dharmorCommented:
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.      
0
 
nietodCommented:
>> 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.)
0
 
dharmorCommented:
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,
0
 
nietodCommented:
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.
0
 
nils pipenbrinckCommented:
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


0
 
nils pipenbrinckCommented:
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
0
 
nils pipenbrinckCommented:
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..
0
 
nils pipenbrinckCommented:
ehm..


looks like EE does some fancy things at the moment.. I'll better logout :)
0
 
laeuchliCommented:
ok, I will now answer the question :-)
The correct code I have already posted.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now