1

I am trying to make a text adventure based on C++. I have made folders which contains the specific files to each path. Suppose I go south from a room, i need to go into a folder named "south". I am having problems here as I don't know how to change directory like "cd .\south" in C++. Please tell me how to change directory in C++.

I tried to use:

system("cd .\\south")

but it does not change directory. I also searched on Google but it gives link to another function called "ShellExecute" which I don't know how to use. Please help (I am a complete beginner).

5
  • Why do you want to go into a specific folder ? What do you want to do once you change the directory of the current process ? Commented Apr 17, 2013 at 9:17
  • 1
    And if you go south, then north, you are in directory "south/north", but the same room? I am not sure that's a wise design ;) Commented Apr 17, 2013 at 9:17
  • 2
    Using folder as game content data is not wise. Commented Apr 17, 2013 at 9:21
  • 3
    "go WINDOWS", "go SYSTEM32", "attack * with DEL" Commented Apr 17, 2013 at 9:23
  • @ArneMertz - if i go to south, then north then i am in the original directory. It is like doing "cd ..". I know that it's not a wise design. I will try to modify it. Thanks for feedback. Commented Apr 19, 2013 at 17:50

4 Answers 4

2

The system function create a new process for the command. This means that any directory changing will be local to that new process.

You want the _chdir function instead:

_chdir("south");

Alternatively you can use the WIN32 function SetCurrentDirectory.

Note: _chdir is the Windows CRT function name, on POSIX systems (like Linux or OSX) it's chdir (without the leading underscore).

Sign up to request clarification or add additional context in comments.

Comments

2

Direction 1: Simply What you have to do is change the current directory. for this read this article http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806(v=vs.85).aspx

But if your application is multi threaded. then you need to be careful. because current directory is common for the whole application so other thread may change the applications current directory.

Direction 2: If you need to do this by executing system command (I don't know weather it is possible). then you can execute multiple system command using && in windows environment.

EG: system("cls && date && pause");

Comments

0

The problem is that each system command will be executed in separated processes, so your cdcommand will work but won't be effective for the next commands.

You could use chdir if you're on a Linux/Unix system or SetCurrentDirectory for Win32 API but i'm not sure whether it's actually what you want to do.

1 Comment

This is not really an answer unless you provide a solution, e.g. chdir.
0

The answer to the specific question you are doing have already being given. However, I wonder why you are trying to change the current directory in order to answer to a command given by the user.

Maybe you are doing it this way because you want to gain knwoledge something specific, however, take into account that the average way to face a text adventure is not to create folders in the computer, but to create the appropriate structures.

You should have classes at least for: Location, Object, Character

You should have vector of locations and objects in order to represent all possible locations and objects in the game.

The playing character should also have a list of objects that he is able to carry with him (thogh you can make that extensible to other characters in the game).

Each location should have a: name, description, and a vector of ten positions for the common exits, such as north, south, east, west, ne, nw, se, sw, up and down. Inside that vector, you could store the number of the Location to go when that exit is chosen.

And finally, you need to parse the input of the player, in order for the game to be able to understand the commands.

This of course, are the minimums of the adventure. You can use already existing systems, such as Inform, though again I don't know if you are trying to exercise your C++ skills.

Remember you can look for the help of true experts in adventures by visiting the interactive fiction forum:

http://www.intfiction.org/forum/

1 Comment

Thank you sir! But you know what, i am not willing to learn so much about it as this project which i am making is only a part of my school assignment activities, so it must not be that complicated(Just a basic project to teach us basic cmd command exection using c++.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.