Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: j0k3r on March 21, 2004, 03:11 PM

Title: Assignment in If statement
Post by: 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?
Title: Re:Assignment in If statement
Post by: Adron on March 21, 2004, 03:22 PM
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
Title: Re:Assignment in If statement
Post by: Eli_1 on March 21, 2004, 03:45 PM
Quote
5: if (c = (a-b))

shouldn't that be


if (c == (a - b))
Title: Re:Assignment in If statement
Post by: iago on March 21, 2004, 04:01 PM
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.
Title: Re:Assignment in If statement
Post by: Adron on March 21, 2004, 04:02 PM
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?
Title: Re:Assignment in If statement
Post by: 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).

But why is the expression incorrect? I thought assignment expressions always turned out true.
Title: Re:Assignment in If statement
Post by: iago on March 21, 2004, 05:40 PM
No, assignment expressions return the new value of the variable.

printf("%d\n", a=4);

Will display "4".
Title: Re:Assignment in If statement
Post by: Adron on March 21, 2004, 05:59 PM
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.
Title: Re:Assignment in If statement
Post by: j0k3r on March 21, 2004, 06:04 PM
I blame my schoolboard for teaching us a bad programming language. Thanks for help.
Title: Re:Assignment in If statement
Post by: 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().
Title: Re:Assignment in If statement
Post by: 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.
Title: Re:Assignment in If statement
Post by: j0k3r on March 21, 2004, 09:08 PM
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?
Title: Re:Assignment in If statement
Post by: Adron on March 22, 2004, 04:26 AM
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?
Title: Re:Assignment in If statement
Post by: j0k3r on March 22, 2004, 06:22 AM
Chat? Text for something (in a campaign for example).
Title: Re:Assignment in If statement
Post by: Adron on March 22, 2004, 03:41 PM
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.