#include <iostream>
using namespace std;
int main()
{
// char[20] name = "blah";
int ssn = 123456789;
int dob = 12742;
cout << ssn '\n';
cout << dob'\n';
return 0;
}
views:
110answers:
4
+2
A:
cout << ssn '\n';
cout << dob'\n';
It should be:
cout << ssn << '\n';
cout << dob << '\n';
Also consider using using std::cout instead of using namespace std;. Including the entire std namespace is usually frowned upon by most C++ programmers.
Andreas Bonini
2010-08-16 00:46:16
"using namespace" declarations cause problems in header files, but there is nothing wrong with using them in cpp files. Especially in an example posted online.
Jon Reid
2010-08-16 00:51:18
+10
A:
You need to change:
cout << ssn '\n';
cout << dob'\n';
to:
cout << ssn << '\n';
cout << dob << '\n';
The following code is the complete program (as I would write it without the using):
#include <iostream>
int main() {
int ssn = 123456789;
int dob = 12742;
std::cout << ssn << std::endl;
std::cout << dob << std::endl;
return 0;
}
which outputs:
123456789
12742
paxdiablo
2010-08-16 00:46:29
You really should use `std::endl` instead of `'\n'`. Newline isn't guaranteed to be just `'\n'`, it can be `"\r\n"`.
SigTerm
2010-08-16 00:51:07
That's not what endl does. "\n" -> "\r\n" translation is done if necessary and if the file is opened in non-binary mode when the iostreams library sees a newline. endl outputs a newline and flushes the stream, it is equivalent to `'\n' << std::flush`. You definitely should not mindlessly use `endl` anywhere you'd use a `'\n'`, many times it does not make sense to flush prematurely.
Logan Capaldo
2010-08-16 00:58:03
@Logan, that's interesting and one more of the vast ocean of things I still needed to learn about C++ :-) I'm assuming it's okay in this little snippet since efficiency isn't a prime concern, but it's good to know for future reference.
paxdiablo
2010-08-16 01:13:37
@Logan: 99.9% of the time, the output is line buffered in any case, which means using either form will produce the exact same results.
Billy ONeal
2010-08-16 01:44:49
Actually, @Billy, I'm not sure that's quite right. Based on Logan's comment, the `endl` appears to be _preventing_ efficient buffering (i.e., forcing a flush to happen regardless). There's a big difference (in performance rather than functionality) between writing out every 4K buffer and writing out every line.
paxdiablo
2010-08-16 02:36:23
@pax: Yes, `std::endl` forces a buffer flush. OTOH, in most standard libraries outputing any sort of `\n` is also going to force a buffer flush. If it did not, you'd not see any text after writing a line.
Billy ONeal
2010-08-16 02:51:02
@Billy, don't know specifically about C++ but I know that ISO C states something like 'line-flushing is only activated if the output device can be detected as an interactive device'. In that case, using `testprog >/tmp/junk` wouldn't use line buffering (and rightly so for efficiency). But I suspect you're right in this case, the output would most likely be interactive.
paxdiablo
2010-08-16 02:55:09
The buffering thing was sort of a side note. Really, I just didn't want anyone to have the mistaken belief that endl was somehow more portable than '\n'. Even if "99.9% of the time" it makes no difference, it's important to know why that is, and actively decide to do one thing or the other.
Logan Capaldo
2010-08-16 03:39:37
+1
A:
Missing an operator:
cout << ssn << endl;
cout << dob << endl;
Stephen Canon
2010-08-16 00:48:26
A:
However the following is legal in C++ (adjacent string literals)
int main()
{
//char name[20] = "blah"; // the commented array declaration was not correct.
cout << "blah""\n";
// ^ no << operator
return 0;
}
Chubsdad
2010-08-16 05:53:54