char ans;
cout<< "Please answer the question with a Y or a N";
cin>> ans;
cin.ingnore();
if ( ans == Y || ans == y ) {
<Something>
}
Of course that's not the entire code, there are else and if else statements and more. But the problem is somewhere in this piece of code. When i run it (it's a console app), no matter what do i type in, it will always think of the "ans == Y || ans == y" statement as a true statement (in other words, even if i type in "N" it will think i typed in "Y")
I did a bit searching, and discovered it's quite a common problem, but i couldn't find the solution. I'm asking for help here since i know any other programming forums.
I'm using the CodeBlocks complier, if that matters.
scanf and printf, much more general purpose and imo generally better.
While the stream versions are more type safe (which the C++0x standard will essentially change since it will provide a mechanism for truly type safe variadic functions), printf and scanf do have a formatting mechanism that's easier to use and much more expressive.
Rollback Post to RevisionRollBack
Never attribute to malice what can adequately be explained by incompetence.
It's not about optimizing every little thing, it's about making beautiful code.
Type safety isn't about optimization, it's about catching potential segfaults at compile time (by not letting them happen in the first place). It does also have the advantage of being faster since appropriate code can be inlined appropriately, but that's a secondary effect.
Rollback Post to RevisionRollBack
Never attribute to malice what can adequately be explained by incompetence.
The most efficient solutions aren't always the most elegant. In fact, if you've ever seen linear algebra optimized for a particular processor architecture, you'll find that the code is anything but elegant. But it sure is damn fast.
I've also implemented an iterator type in C++ for iterating over trees* in depth-first and breadth-first manners that are O(1) in space complexity (but definitely not in time) and figured out where to go based only on the node that the iterator pointed to (so the iterator only stored a pointer to the node and no other state information). Coming up with a breadth-first constant space iterator was difficult and the solution is anything but elegant.
*The tree was stored like a doubly linked-list. A node had a pointer to its parent (well, the "fast" iterator implementation did; I also had a "slow" iterator implementation that worked on trees without this pointer but it needed a pointer to the root), two pointers to its neighboring siblings (this is the doubly linked-list) and a pointer to its first child node.
Rollback Post to RevisionRollBack
Never attribute to malice what can adequately be explained by incompetence.
But the thing is that the compiler would throw errors if he hadn't declared two variables called Y and y before that statement, yet he seems to have been able to run the program even though it should never have compiled like that. That's what's mystifying to me.
He's my code:
Of course that's not the entire code, there are else and if else statements and more. But the problem is somewhere in this piece of code. When i run it (it's a console app), no matter what do i type in, it will always think of the "ans == Y || ans == y" statement as a true statement (in other words, even if i type in "N" it will think i typed in "Y")
I did a bit searching, and discovered it's quite a common problem, but i couldn't find the solution. I'm asking for help here since i know any other programming forums.
I'm using the CodeBlocks complier, if that matters.
Single quotes (') I belive that should be.
Thank you kind sir!
Oh, of course XP
I like C and C++ the most, i tried to learn other languages but in the end C and C++ are the easiest for me to understand and use
Wait, what? O.o
scanf and printf, much more general purpose and imo generally better.
While the stream versions are more type safe (which the C++0x standard will essentially change since it will provide a mechanism for truly type safe variadic functions), printf and scanf do have a formatting mechanism that's easier to use and much more expressive.
Type safety isn't about optimization, it's about catching potential segfaults at compile time (by not letting them happen in the first place). It does also have the advantage of being faster since appropriate code can be inlined appropriately, but that's a secondary effect.
I've also implemented an iterator type in C++ for iterating over trees* in depth-first and breadth-first manners that are O(1) in space complexity (but definitely not in time) and figured out where to go based only on the node that the iterator pointed to (so the iterator only stored a pointer to the node and no other state information). Coming up with a breadth-first constant space iterator was difficult and the solution is anything but elegant.
*The tree was stored like a doubly linked-list. A node had a pointer to its parent (well, the "fast" iterator implementation did; I also had a "slow" iterator implementation that worked on trees without this pointer but it needed a pointer to the root), two pointers to its neighboring siblings (this is the doubly linked-list) and a pointer to its first child node.