I don't feel like taking out a book, and I'm too lazy to search on google.
How do you make a string accept a space as part of it?
Like, for example:
If the user entered:Code:#include <io.h> #include <sys/types.h> #include <sys/stat.h> #include <iostream> #include <string> using namespace std; int main( void ) { string strPath; cout << "Enter directory to check: "; cin >> strPath; if ( access( strPath.c_str(), 0 ) == 0 ) { struct stat status; stat( strPath.c_str(), &status ); if ( status.st_mode & S_IFDIR ) { cout << "The directory exists." << endl; } else { cout << "The path you entered is a file." << endl; } } else { cout << "Path doesn't exist." << endl; } return 0; }
C:\Program Files\
How would you make it accept the space in "Program Files"? When a normal string sees a space it just cuts off accepting input, seeing the space as the end of the input.
Help, anyone?
Results 1 to 5 of 5
Thread: 'Tarded programming question
- 03 Jul. 2012 02:55am #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 15.00
'Tarded programming question
- 03 Jul. 2012 03:12am #2
Knew the answer. Doubled checked and confirmed the answer in about 1 minute, except I don't even technically program. But I'm not going to tell you the answer because if you don't wish to learn about programming yourself, just don't program.
- 03 Jul. 2012 03:25am #3
lol. first of all, why are you using C++ in the first place? Even with a fair amount of experience, it's a rigorous programming language. Until you feel comfortable with the amount of knowledge you have, it's best to stick with a higher-level language like Python or Ruby. Or something with a procedural paradigm like Lua.
That aside:
Code:#include <string> using namespace std; int main(void) { string strPath; cout << "Enter directory to check: "; getline(cin, strPath); if (access(strPath.c_str(), 0 ) > -1) { struct stat status; stat(strPath.c_str(), &status); if (status.st_mode & S_IFDIR) { cout << "The directory exists." << endl; } else { cout << "The path you entered is a file." << endl; } } else { cout << "Path doesn't exist." << endl; } return 0; }
Last edited by The Unintelligible; 03 Jul. 2012 at 03:35am.
- 03 Jul. 2012 04:34am #4
Many introductory programming courses teach C/C++. It can help to understand what's going on behind the scenes, rather than taking for granted the features that a higher level language like Python hides from you.
Personally I'd recommend you download/purchase a college-level introductory C/C++ textbook and work through it.
- 03 Jul. 2012 08:36pm #5
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 709.00
I actually have one and am working through it right now, and I'm reading a book about optimizing high-level programs and writing them like you would low-level code like Assembly.
Well I was going to learn Python or VB, but I decided on C++ because I needed it for programming on my school's robotics team anyway. Thanks for the suggestion though!