Hey guys, I'm making a program that asks the user to input a folder path (string), which it then displays the contents of said directory, but the problem I have is that it won't accept space bars, it just screws up and enters into an infinite loop.

So is there any way to integrate something into this code to get the preffered effect?

string cFolder;
cout << "Enter a folder path to browse:" << endl;
cin >> cFolder;
chdir(cFolder.c_str());
opendir(cFolder.c_str());
system("DIR");

Any help would be greatly appreciated :D

Uh... pardon?

You used: cin >> cFolder;

If you type: "my folder name" then the value stored in cFolder will only be "my". The words "folder name" will be in the buffer. If you want to read the entire line then use getline instead:

getline(cin, cFolder);

Which is what "iamthwee" suggested.

>I'll be sure to spread the word about this.

Spread whatever you like. The fact is I gave you an answer and a sufficient one at that. Learn to use google.

You used: cin >> cFolder;

If you type: "my folder name" then the value stored in cFolder will only be "my". The words "folder name" will be in the buffer. If you want to read the entire line then use getline instead:

getline(cin, cFolder);

Which is what "iamthwee" suggested.

I used this, but it does the same thing as if I had used cin >> cFolder.

This is my code now

string cFolder;
getline(cin,cFolder);
cout << "Enter a folder path to browse:" << endl;
cin >> cFolder;
chdir(cFolder.c_str());
opendir(cFolder.c_str());
system("DIR");

Wow... what a bad community...

I'll be sure to spread the word about this.

I actually like this community. You can get a lot of good answers here and the posters seem to really know what they are talking about. As far as not liking the format of the help.... well, let's just say that it takes all kinds to make the world go round. You get an explanation and an example from me. You also got a google link from iamthwee. That should be more than enough to get your program working...... which was the point of your question in the first place. Right?? ;)

string cFolder;
  cout << "Enter a folder path to browse:" << endl;
  getline(cin,cFolder);
  chdir(cFolder.c_str());
  opendir(cFolder.c_str());
  system("DIR");

getline() is like cin except that it accepts the whole line instead of the first word on the line....

OH!

I see what I did thar....

Sorry for wasting your time. Haha.

Don't worry. Glad it's working for you.

You may want to read up on the differences between cin and getline() because they handle the newline character differently..... which can give you strange output if you mix the two together in sequence.

Uh, okay another problem... sorry.

It isn't prompting me for an input, it's just displaying the default directory, as if it has skipped the entire getline function. :(

Anyone ever encounter this problem?

string cFolder;
cout << "Enter a folder path to browse:" << endl;
getline(cin,cFolder);
chdir(cFolder.c_str());
opendir(cFolder.c_str());
system("DIR");

I compiled this on my computer:

int main()
{
    string cFolder;
    cout << "Enter a folder path to browse:" << endl;
    getline(cin, cFolder);
    chdir(cFolder.c_str());
    opendir(cFolder.c_str());
    system("ls");
    
    return 0;
}

It works just fine..... with or without spaces in the directory names.

Note that I'm using "ls" because I'm on OpenSolaris. This is the UNIX equivalent of dir. Everything else should be OK and exactly the same as mine.

Double check your code. If you can't find anything wrong then post your entire program so I can take a look at it.

By the way, I tried it in Code::Blocks (GCC compiler) on Windows XP and it worked as well. Have you got it working yet?

That's because you're an idiot. You use one or the other. NOT both.

Hahahahahahahahha.哈哈哈哈哈哈哈哈哈哈哈哈哈哈. 母母母母母母母母母母母.

you also can use gets()

char *str = new char[128];
gets(str);

or change a EOS symbol

char *str = new char[128];
cin.getline(str, 128, '\n'); //instead '\n' type your EOS symbol e.g. '$'
Comments
Never suggest gets().
This article has been dead for over six months. Start a new discussion instead.