Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1068773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:19:47+00:00 2026-05-16T20:19:47+00:00

I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;

  • 0

I have the following code:

#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

int main()
{
    // Variables
    string sDirectory;

    // Ask the user for a directory to move into
    cout << "Please enter a directory..." << endl;
    cin >> sDirectory;
    cin.get();

    // Navigate to the directory specified by the user
    int chdir(sDirectory);

    return 0;
}

The purpose of this code is pretty self explanatory: to set a user specified directory as the current directory. My plan is to carry out operations on the files contained therein. However, when I attempt to compile this code, I receive the following error

error: cannot convert ‘std::string’ to ‘int’ in initialization

with reference being made to the line reading int chdir(sDirectory). I’ve just started programming and am only now starting to have to find out about platform specific functions, which this one is, so any help on this matter would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:19:47+00:00Added an answer on May 16, 2026 at 8:19 pm

    int chdir(sDirectory); isn’t the correct syntax to call the chdir function. It is a declaration of an int called chdir with an invalid string initializer (`sDirectory).

    To call the function you just have to do:

    chdir(sDirectory.c_str());
    

    Note that chdir takes a const char*, not a std::string so you have to use .c_str().

    If you want to preserve the return value you can declare an integer and use a chdir call to initialize it but you have to give the int a name:

    int chdir_return_value = chdir(sDirectory.c_str());
    

    Finally, note that in most operating system the current or working directory can only be set for the process itself and any children it creates. It (almost) never affects the process that spawned the process changing its current directory.

    If you expect to find the working directory of your shell to be changed once your program terminates you are likely to be disappointed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
i have following code #include <iostream> using namespace std; int main(){ int i; char
I have the following code: main.cpp #include <iostream> #include <string> using namespace std; string
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct
i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following C++ code: #include <iostream> #include <string> using namespace std; class
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.