C++ windows system ("path") not working if there is space somewhere - c++

My path to the executable file is:
C:\Users\FirstName LastName\Desktop\Saturated.exe
My program is:
while (s != "Exit")
{
cin >> s;
system (s.c_str());
}
Where s is string.
I tried to write:
C:\\Users\\FirstName LastName\\Desktop\\Saturated.exe
\"C:\\Users\\FirstName LastName\\Desktop\\Saturated.exe\"
C:/Users/FirstName LastName/Desktop/Saturated.exe
But none of this worked because of space between FirstName and LastName. What should I do?

If you used the command line arguments to input the string, your OS would parse it correctly. If you want to input the path while running the program, your best chance is to go with std::getline, you'll read the whole line no matter what, no need for ".
Or, if you want to implement that same parsing behavior, you'll check if the first character is " (with cin.peek()), if that's the case, you'll cin.ignore() and std::getline until another ", otherwise you'll just cin >> s;.

Related

How to promt the user correctly? [duplicate]

Here is the code:
string str;
cin>>str;
cout<<"first input:"<<str<<endl;
getline(cin, str);
cout<<"line input:"<<str<<endl;
The result is that getline never pauses for user input, therefore the second output is always empty.
After spending some time on it, I realized after the first call "cin>>str", it seems '\n' is still stored in cin (using cin.peek() to check), which ends getline immediately. The solution will be adding one more line between the first usage and the second one:
cin.ignore(numeric_limits::max(), '\n');
However, I still don't understand, why is '\n' left there after the first call? What does istream& operator>> really do?
The \n is left in the input stream as per the way operator>> is defined for std::string. The std::string is filled with characters from the input stream until a whitespace character is found (in this case \n), at which point the filling stops and the whitespace is now the next character in the input stream.
You can also remove the \n by calling cin.get() immediately after cin>>str. There are many, many different ways of skinning this particular I/O cat, however. (Perhaps a good question in and of itself?)
By default, the stream insertion operator reads until it sees whitespace. Your first call isn't returning until it sees a space, tab, newline, etc. Then the next character needs to be consumed so that you can get to the next one.
I generally recommend only doing line-oriented input from std::cin. So, your code could look something like this:
string str;
int val;
// Read an entire line and parse an integer from it
{
string line;
getline(cin, line);
istringstream iss(line);
iss >> val;
}
cout<<"first input:"<<val<<endl;
getline(cin, str);
cout<<"line input:"<<str<<endl;
Be sure to add error checking too.
The getline-only approach avoids having to think about line buffering of the input, of clearing the input, etc. If you directly read something with >>, the input does not terminate if the user hits enter instead of inputting what is required, but instead continues until a token is input (this behavior is usually not wanted).
As others have said, th problem is that the newline is left from the first extraction. One solution I do is to discard all the left characters in the stream:
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Good writeup that explains some of the reasons why you are running into this issue, primarily due to the behavior of the input types and that you are mixing them
Also was searching for most suitable solution. Implementation of this operator could produce problems. And not always is acceptable to read entire line, or not mix different types in one input line.
To solve problem, when you want to read some data from cin, and don't know if whitespaces was correctly extracted after last input operation, you can do like this:
std::string str;
std::cin >> std::ws >> str;
But you can't use this to clear trailing newline symbol after last input operation from cin to do not affect new input, because std::ws will consume all whitespaces and will not return control until first non-ws character or EOF will be found, so pressing enter will not finish input process.
In this case should be used
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
which is more flexible.
P.S. If got errors with max() function such as "identifier expected", it could be caused by max macros defined in some header (for example, by Microsoft); this could be fixed by using
#undef max

How to determine I've reached EOF with a non coded file?

I've looked up several instances in EOF, but in all instances EOF is being used on a file that is part of the program, for example:
std::fstream myFile("file.txt", std::ios::in);
while(!myFile.eof()) {
/*program*/
}
However in this instance, I'm not using a file as part of the code. I'm just using basic cin commands. There's a command to quit, but let's say a user runs the program like this:
./program <myFile.txt> myOutput
Let's say that myFile had these commands in this:
add 1
add 2
delete 1
print
That's all fine, but they forgot to add a quit command at the end, so the code won't stop. So how do I get the code to detect EOF in this situation and stop?
The correct way to detect end-of-file is to check each input operation for success.
For example, with getline you'd use
std::string line;
while (std::getline(std::cin, line)) {
// do stuff with line
}
Or with >>:
while (std::cin >> x) {
// do stuff with x
}
This applies to all input streams, whether they're from files (fstream) or e.g. cin.
End of file (EOF) means there is nothing more to read from the file buffer, it’s not something one puts explicitly at the file itself.. you should still get there fine with your code
Another way is to read the buffer until there are no more bytes to read there

Adding a Line Break to Strings

I am writing a program that lets the user input some advice on coding. I am having trouble getting the program to add a line break after each new line. I am able to append a space just fine, but I can't seem to get a line break in there. Any advice?
void add_new_advice(Advice& advice)
{
cin.ignore(10000, '\n');
// read and concatenate lines until two newlines are read
while (getline(cin, advice.advice))
{
if (advice.advice.empty()) break;
else advice.newAdvice += advice.advice + " ";
}
cout << advice.newAdvice;
return;
}
A line break is just a special character \n, but may be interpreted differently under different OS, check this wiki page for more details about NewLine.
However, when it comes to code, simply append \n behind your strings and then the compiler will convert it properly based on your OS and write it into the file.
For your information, you can check Escaped Characters to get familiar with.

Why doesn't std::getline block?

I have this code in an Objective-C class (in an Objective-C++ file):
+(NSString *)readString
{
string res;
std::getline(cin, res);
return [NSString stringWithCString:res.c_str() encoding:NSASCIIStringEncoding];
}
When I run it, I get a zero-length string, Every time. Never given the chance to type at the command line. Nothing. When I copy this code verbatim into main(), it works. I have ARC on under Build Settings. I have no clue what it going on. OSX 10.7.4, Xcode 4.3.2.
It is a console application.
It means there is input waiting to be read on the input. You can empty the input:
cin.ignore(std::numeric_limits<std::streamsize>::max();
std::getline(cin, res);
If this is happening it means you did not read all the data off the input stream in a previous read. The above code will trash any user input before trying to read more.
This probably means that you are mixing operator>> with std::getline() for reading user input. You should probably pick one technique and use that (std::getline()) throughout your application ( you can mix them you just have to be more careful and remove the '\n' after using operator>> to make sure any subsequent std::getline() is not confused..
If you want to read a number read the line then parse the number out of the line:
std::getline(cin, line);
std::stringstream linestream(line);
linestream >> value;
You can simply do:
cin.ignore();
or use
cin.clear();
cin.sync();
before using getline()

How to handle Unix pipe

How do I get the filename from redirection from the shell in my C++ code? i.e. ./MyProgram < myfile , I want to get myfile as the filename and its content line by line.
**Edit: I managed to get the input from file. Thanks for the help. However, after the looping through the file content, I want to keep user input with cin. It's like this:
while (true)
{
if (cin.eof() == false)
{
getline(cin, line);
cout << line;
}else{
cin >> choice;
}
}
You can't (at least not portably). This information is not provided to the program. Instead, you can access the data from myfile (in your example) by reading from standard input. E.g., you can use C++'s getline with cin as the first parameter.
Depending on the shell, you may have inherited a descriptor representing an actual file or a pipe. If it's an actual pipe (i.e. fifo), the name won't mean much. But you can get the name on linux (and on Windows, but it doesn't sound like you're interested in that).
See Getting Filename from file descriptor in C