How do I use quotation marks in fstream? - c++

I want to output a line into a .plt file that says "one-D Hydro" with the double quotation marks and so far I have this problem.
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
fstream gnuplot_file;
gnuplot_file.open ("sod.plt");
gnuplot_file<<"set title"<< ""one-D Hydro""<<std::endl;
gnuplot_file.close();
system("gnuplot.exe sod.plt");
return 0;
}
Line 11 will not allow it to compile because I can't seem to close the statement. The error is just as useless by the way.
gnuplot_call.cpp|11|error: expected ';' before 'one'|

With C++03 (or even C) use backslashes to escapes double-quotes in string literals:
gnuplot_file << "set title" << "\"one-D Hydro\"" << std::endl;
Notice that gnuplot may require you to also escape some characters, e.g. if you wanted the title to contain quotes!
With C++11 you could use raw string literals, e.g.
gnuplot_file<< R"*(set title "one-D Hydro")*" << std::endl;
BTW, you could be interested by popen(3) and pclose, if your operating system and C++ library provides them. You would just popen the gnuplot process and send commands to it, finally pclose-ing it.

Try to include escape character [i.e.,back slash] in the code where you are trying to add double quotes.
For example:
"\"one-D Hydro\""
btw why are you using std:: once you have defined namespace for it you can directly use endl.

Related

c++ How to print in a file a double decimal number with comma(instead of dot)

I need to print a csv file with numbers.
When the file is printed , I have numbers with dots, but I need them with commas.
Here an example.
If I print this number in terminal using locale method, I obtain a number with comma, but in the file I have the same number but with dot. I do not understand why.
How could I do?
#include <iostream>
#include <locale>
#include <string> // std::string, std::to_string
#include <fstream>
using namespace std;
int main()
{
double x = 2.87;
std::setlocale(LC_NUMERIC, "de_DE");
std::cout.imbue(std::locale(""));
std::cout << x << std::endl;
ofstream outputfile ("out.csv");
if (outputfile.is_open())
{
outputfile <<to_string(x)<<"\n\n";
}
return 0;
}
Thanks in advance.
Locales are system-specific. You probably just made a typo; try "de-DE", which will probably work (at least it does on my Windows).
However, if your program is not inherently German-centric, then abusing the German locale just for the side effect of getting a specific decimal point character is bad programming style, I think.
Here is an alternative solution using std::numpunct::do_decimal_point:
#include <string>
#include <fstream>
#include <locale>
struct Comma final : std::numpunct<char>
{
char do_decimal_point() const override { return ','; }
};
int main()
{
std::ofstream os("out.csv");
os.imbue(std::locale(std::locale::classic(), new Comma));
double d = 2.87;
os << d << '\n'; // prints 2,87 into the file
}
This code specifically states that it just wants the standard C++ formatting with only the decimal point character replaced with ','. It makes no reference to specific countries or languages, or system-dependent properties.
Your issue is that std::to_string() uses the C locale libraries. It appears that "de_DE" is not a valid locale on your machine (or Coliru for that matter), leading to the default C locale being used and using .. The solution is to use "de_DE.UTF-8". As an aside, using "" for std::locale will not always produce commas; instead, it will depend on the locale set for your machine.

Having problems compiling my first C++ program

I tried compiling the following program on XCode on my Mac, and I get these errors:
*Non-ASCII charactes are not allowed outside of literals and identifiers. Fix it: Delete ""
*Use of undeclared identifier 'Hello'
#include <iostream>
using namespace std;
int main()
{
cout << “Hello there world!”;
return 0;
}
This program is literally verbatim from the textbook, "A First Book of C++: An Introduction to Programming" so I'm not sure why it would not work. Is this a Mac vs. PC issue?
The "pretty quotes" copied from your textbook are not valid characters.
Change:
cout << “Hello there world!”;
// ^ ^ These characters are not correct.
To:
cout << "Hello there world!";
The editor you use to type code must not be one that replaces the characters you type with characters that might look nicer.
It seems that you are using non-ASCII double quotes
try with this string (cut and paste) "Hello there world", note the different leading apostrophes.
Try using the Code::Blocks compiler
http://www.codeblocks.org/

Getline Function Messing with Code

Picking up C++ and having a go at it on OS X 10.9 using XCode 5.0.2 and using Alex Allain as reference.
The following code compiles just fine and outputs correctly
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string user_first_name = "test";
std::cout << user_first_name << "\n";
return 0;
}
When I add a getline function, code appears to compile but with no output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string user_first_name = "test";
std::getline( std::cin, user_first_name, '\n' );
std::cout << user_first_name << "\n";
return 0;
}
In fact debug navigator has memory filling up with bars (although actual memory use is fixed at 276 KB). Why am I getting stumped on such a simple thing/concept?
I did a bit of digging around and its quite likely this is related to a text encoding issue. I'm using defaults which is Unicode (UTF-8). Encoding is not something I'm familiar with, never something I had to deal with when learning on Windows. How do I get past this?
I can't comment regarding the use of XCode or OS X, but it was my understanding that std::cin always gives you a narrow (single-byte) character stream. In Windows (at least with Visual Studio), I think it works whether you compile for UTF8 (single-byte for all ASCII characters) or UTF16 (2-bytes for all ASCII characters). The runtime library presumably does the conversion for you as necessary.
I'm not sure what "filling up with bars" means, but maybe it's just that you're looking at uninitialized memory. If you think that it is an encoding issue, perhaps try using wstring/wcin instead of string/cin and see if that helps.

About Use of #include Preprocessor Directive in C++

I stumble on the following compilation error in C++ with g++ compiler:
error on line line 1 with message:
invalid preprocessing directive #a
(with a caret above the character a) which is followed by another,probably consequent, error on line 4 with message:
cout was not declared in this scope.
The editor i am using is Code blocks 10.05 with mingw.I tried removing .h extension from the iostream file include statement;switching among different File encoding options;and replacing the angular bracket with single quotes and double quotes as well.i am stuck on it.Pardon if it is a duplicate(although i went through several already asked questions in relevance).
The following code illustrates the problem:
#‪include ‬<iostream.h>
int main()
{
cout<< "abc"+8;
cout<< "def"+4;
cout<< "ha";
return 0;
}
cout exists within the namespace std
So either
#‪include‬<iostream>
//...
std::cout << "abc" << 8;
//...
or
#‪include‬<iostream>
using namespace std;
//...
or
#‪include‬<iostream>
using std::cout;
//...
I tend to prefer the 1st if I'm only using it once or twice, The second if I'm using a lot of different pieces from a namespace (and only in a cpp file), or the third if I'm only using a piece or 2 from a namespace but using the same couple many times.
Additionally as stated in the comments, don't use the 2nd one in headers. See: "using namespace" in c++ headers
Also, you have an invalid character in your #include. You can see it in a hex editor or Note how stackoverflow doesn't highlight them the same:
#‪include‬<iostream>
#include<iostream>
Fully working code:
#include<iostream>
using std::cout;
int main()
{
cout << "abc" << 8;
cout << "def" << 4;
cout << "ha";
return 0;
}
Produces the following output abc8def4ha after I corrected for trying to add 8 to a char*
You have to use std::cout, which means that the "cout" keyword is part of the standard library.
The "invalid directive" error is caused by some invisible Unicode characters in the #include directive; perhaps you copied this from a website that embedded some formatting characters in the code. They can be seen in the question, if you look at the source in a hex editor. That error should be fixed by deleting and retyping the #include line.
You'll probably have other errors, since the code is fifteen years out of date; most modern compilers don't provide pre-standard libraries. These days, the standard library headers don't have a .h extension:
#include <iostream>
and nearly all the names they declare are scoped inside the std namespace:
std::cout << "ha";
Finally, "abc"+8 doesn't do anything sensible. The string literal is an array of four characters, and +8 tries to give you a pointer to the ninth character, which doesn't exist. The result is undefined behaviour.
If you want to print "abc" followed by "8", then you want:
std::cout << "abc" << 8;
Try using it like this:-
#‪include ‬<iostream>
using std :: cout;
cout is the part of std library
If you've got caret above a, try retyping your #include.
You might accidentally type alternative i which looks similar but has different code.
Suggestions about std:: are only relevant for the second error you're getting.
I also didn't fully understand what you were trying to achieve with "abc"+8.

C++ Program to execute another Program with Command line arguments

How do you execute a command line program with arguments from a c++ program? This is what I found online:
http://www.cplusplus.com/forum/general/15794/
std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
But it doesn't seem to accept an actual program location, so I am not sure how to apply this. My hope was to have something like this:
std::stringstream stream;
stream <<"C:\Tests\SO Question\bin\Release\HelloWorld.exe "<<"myargument";
system(stream.str().c_str());
This gives several warnings related to the backslashes - and the program does not work. Is it expecting you to have the program in some specific location?
This is the output I get in the console:
'C:\Tests' is not recognized as an internal or external command,
operable program or batch file.
ADDENDUM:
So based on Jon's answer the correct version for me looks like this:
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
int main(int argc, char *argv[])
{
std::stringstream stream;
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
system(stream.str().c_str());
return 0;
}
First of all, you should use double backslashes in literal strings whenever you want a single backslash to appear in the actual string value. This is according to the language grammar; a conforming compiler could do worse than simply warning about this.
In any case, the problem you are experiencing is due to the fact that paths containing spaces must be enclosed in double quotes in Windows. Since the double quotes themselves need to be escaped inside a C++ string literal, what you need to write is
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
This gives several warnings related to the backslashes
I believe \ is an escape character in C++ using \\ instead will probably solve this problem.