• Welcome to Valhalla Legends Archive.
 

Assignment in If statement

Started by j0k3r, March 21, 2004, 03:11 PM

Previous topic - Next topic

j0k3r

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?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Adron

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

Eli_1

Quote
5: if (c = (a-b))

shouldn't that be


if (c == (a - b))

iago

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Ah.

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?

j0k3r

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.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

No, assignment expressions return the new value of the variable.

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

Will display "4".
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

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.

j0k3r

#8
I blame my schoolboard for teaching us a bad programming language. Thanks for help.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Eibro

Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync().
Eibro of Yeti Lovers.

Adron

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.

j0k3r

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?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Adron

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?

j0k3r

Chat? Text for something (in a campaign for example).
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Adron

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.