A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

'Tarded programming question

599 views · started by 323 ·
#1
'Tarded programming question
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:

#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;
}


If the user entered:

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?
#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.
#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:

#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;
}


Again, I'd seriously recommend that you learn a different language. lol
#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.
#5
Artificial wrote:
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.


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.

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:

#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;
}


Again, I'd seriously recommend that you learn a different language. lol


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!