Can somebody explain why it doesn't output the value of C?
1: #include <iostream.h>
2: int main()
3: {
4: int a = 1, b = 1, c;
5: if (c = (a-b))
6: cout << "The value of c is: " << c;
7: return 0;
8: }
Also, what is the difference between "\n" and endl?
Quote from: j0k3r on March 21, 2004, 03:11 PM
Can somebody explain why it doesn't output the value of C?
1: #include <iostream.h>
2: int main()
3: {
4: int a = 1, b = 1, c;
5: if (c = (a-b))
6: cout << "The value of c is: " << c;
7: return 0;
8: }
Also, what is the difference between "\n" and endl?
1. Because c, and the expression inside the if statement as a whole, is false
2. "\n" is a string while endl is an iostream manipulator, but they have the same effect
Quote
5: if (c = (a-b))
shouldn't that be
if (c == (a - b))
Quote from: Eli_1 on March 21, 2004, 03:45 PM
Quote
5: if (c = (a-b))
shouldn't that be
if (c == (a - b))
No, that was his entire question:
assignment in If statement#2 - Although Adron's basically correct, endl has some weird property involving Flushing the Stream. Eibro explained it to me once but I don't remember.
Ah.
http://www.cplusplus.com/ref/iostream/ostream/_endl.html (http://www.cplusplus.com/ref/iostream/ostream/_endl.html)
But then, if he didn't want a guess, he would've probably googled 5 seconds himself instead of asking?
It's *hard* to find websites which compare "\n" to endl, or comarisons for specific things in general (even google).
But why is the expression incorrect? I thought assignment expressions always turned out true.
No, assignment expressions return the new value of the variable.
printf("%d\n", a=4);
Will display "4".
Quote from: j0k3r on March 21, 2004, 05:30 PM
It's *hard* to find websites which compare "\n" to endl, or comarisons for specific things in general (even google).
Well, look up one thing at a time and see what it does, then compare them yourself.... I searched for "endl iostream" on google and picked the last hit of the first page.
I blame my schoolboard for teaching us a bad programming language. Thanks for help.
Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync().
Quote from: Eibro on March 21, 2004, 06:19 PM
Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync().
So, the conclusion is that for text files you should use "\n", and not endl.
Quote from: Adron on March 21, 2004, 06:27 PM
Quote from: Eibro on March 21, 2004, 06:19 PM
Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync().
So, the conclusion is that for text files you should use "\n", and not endl.
Are you talking about writing to text files? Which would be best to use in a game?
Quote from: j0k3r on March 21, 2004, 09:08 PM
Quote from: Adron on March 21, 2004, 06:27 PM
So, the conclusion is that for text files you should use "\n", and not endl.
Are you talking about writing to text files? Which would be best to use in a game?
Yes, for writing to text files. I'm not sure where you'd use endl or \n in a game?
Chat? Text for something (in a campaign for example).
Quote from: j0k3r on March 22, 2004, 06:22 AM
Chat? Text for something (in a campaign for example).
I suppose that depends on how you're coding it.... You're not likely to write to a standard output or file stream anyway for chat or text output, more likely to use some kind of gui control.