Solved

Variable Drive letter in C++

Posted on 2000-02-16
22
261 Views
Last Modified: 2010-04-10
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
Comment
Question by:Unreal7000
  • 8
  • 5
  • 4
  • +2
22 Comments
 
LVL 5

Expert Comment

by:laeuchli
ID: 2529096
try this:
char test[3];
cout<<"What Drive";
cin>>test;
strcpy(test,":\\games2\\SS2.exe");
0
 

Author Comment

by:Unreal7000
ID: 2529128
Thanks a bunch but it doesnt seem to work. Does there have to be a certain header file???
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 2529129
yes is has a header file, I will post it.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 5

Expert Comment

by:laeuchli
ID: 2529132
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
 
LVL 22

Expert Comment

by:nietod
ID: 2529444
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
 
LVL 5

Expert Comment

by:laeuchli
ID: 2529562
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
 

Author Comment

by:Unreal7000
ID: 2529695
laeuchli- I still cant get it to work for some reason.Thanks anyways for the help.
0
 

Expert Comment

by:dharmor
ID: 2530025
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
 
LVL 22

Expert Comment

by:nietod
ID: 2530849
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
 
LVL 5

Expert Comment

by:laeuchli
ID: 2530927
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
 
LVL 5

Expert Comment

by:laeuchli
ID: 2530930
dharmor you are snitching my question!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2530939
Or at least his answer.
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 2530972
true:-)
0
 

Expert Comment

by:dharmor
ID: 2531120
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
 
LVL 22

Expert Comment

by:nietod
ID: 2531159
>> 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
 

Expert Comment

by:dharmor
ID: 2531276
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
 
LVL 22

Expert Comment

by:nietod
ID: 2531357
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
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2531705
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
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2531713
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
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2531717
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
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2531723
ehm..


looks like EE does some fancy things at the moment.. I'll better logout :)
0
 
LVL 5

Accepted Solution

by:
laeuchli earned 50 total points
ID: 2531829
ok, I will now answer the question :-)
The correct code I have already posted.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question