Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Variable Drive letter in C++

Posted on 2000-02-16
22
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 100 total points
ID: 2531829
ok, I will now answer the question :-)
The correct code I have already posted.
0

Featured Post

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

610 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