Author | Message | Time |
---|---|---|
j0k3r | Can somebody explain why it doesn't output the value of C? [code]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: } [/code] Also, what is the difference between "\n" and endl? | March 21, 2004, 9:11 PM |
Adron | [quote author=j0k3r link=board=30;threadid=5898;start=0#msg50704 date=1079903491] Can somebody explain why it doesn't output the value of C? [code]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: } [/code] Also, what is the difference between "\n" and endl? [/quote] 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 | March 21, 2004, 9:22 PM |
Eli_1 | [quote] 5: if (c = (a-b)) [/quote] shouldn't that be [code] if (c == (a - b)) [/code] | March 21, 2004, 9:45 PM |
iago | [quote author=Eli_1 link=board=30;threadid=5898;start=0#msg50711 date=1079905552] [quote] 5: if (c = (a-b)) [/quote] shouldn't that be [code] if (c == (a - b)) [/code] [/quote] 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. | March 21, 2004, 10:01 PM |
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? | March 21, 2004, 10:02 PM |
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. | March 21, 2004, 11:30 PM |
iago | No, assignment expressions return the new value of the variable. printf("%d\n", a=4); Will display "4". | March 21, 2004, 11:40 PM |
Adron | [quote author=j0k3r link=board=30;threadid=5898;start=0#msg50734 date=1079911821] It's *hard* to find websites which compare "\n" to endl, or comarisons for specific things in general (even google). [/quote] 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. | March 21, 2004, 11:59 PM |
j0k3r | I blame my schoolboard for teaching us a bad programming language. Thanks for help. | March 22, 2004, 12:04 AM |
Eibro | Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync(). | March 22, 2004, 12:19 AM |
Adron | [quote author=Eibro link=board=30;threadid=5898;start=0#msg50748 date=1079914791] Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync(). [/quote] So, the conclusion is that for text files you should use "\n", and not endl. | March 22, 2004, 12:27 AM |
j0k3r | [quote author=Adron link=board=30;threadid=5898;start=0#msg50750 date=1079915252] [quote author=Eibro link=board=30;threadid=5898;start=0#msg50748 date=1079914791] Yes, std::endl prints a newline then calls std::ostream::flush(), which in turn calls std::streambuf::sync(). [/quote] So, the conclusion is that for text files you should use "\n", and not endl. [/quote] Are you talking about writing to text files? Which would be best to use in a game? | March 22, 2004, 3:08 AM |
Adron | [quote author=j0k3r link=board=30;threadid=5898;start=0#msg50781 date=1079924903] [quote author=Adron link=board=30;threadid=5898;start=0#msg50750 date=1079915252] So, the conclusion is that for text files you should use "\n", and not endl. [/quote] Are you talking about writing to text files? Which would be best to use in a game? [/quote] Yes, for writing to text files. I'm not sure where you'd use endl or \n in a game? | March 22, 2004, 10:26 AM |
j0k3r | Chat? Text for something (in a campaign for example). | March 22, 2004, 12:22 PM |
Adron | [quote author=j0k3r link=board=30;threadid=5898;start=0#msg50834 date=1079958163] Chat? Text for something (in a campaign for example). [/quote] 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. | March 22, 2004, 9:41 PM |