Computer Science Canada Please help fix my program :) |
Author: | Cancer Sol [ Thu Mar 07, 2013 2:55 pm ] | ||
Post subject: | Please help fix my program :) | ||
Please help fix my program. I use Code::Blocks (GNU-GCC Compiler).
Umm... sorry, how do I show the c++ syntax on my post? I thought that's how I'm supposed to do it.. |
Author: | DemonWasp [ Thu Mar 07, 2013 3:14 pm ] |
Post subject: | RE:Please help fix my program :) |
You forgot to tell us what's wrong with it. Does it compile? Does it do the wrong thing? When and how does it do the wrong thing? What have you tried to solve it? You should also remember that a single equals sign (=) is assignment, and two (==) is comparison. You want to use comparison in your if statements, but you are using assignment, which will not do what you want. |
Author: | Zren [ Thu Mar 07, 2013 3:21 pm ] |
Post subject: | RE:Please help fix my program :) |
It's syntax="cpp" btw. |
Author: | Tony [ Thu Mar 07, 2013 3:26 pm ] |
Post subject: | Re: RE:Please help fix my program :) |
DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote: You want to use comparison in your if statements, but you are using assignment, which will not do what you want.
Btw, a compiler will warn you about such mistakes. Quote: $ cat /tmp/test.c int main(){ int i=0; if(i=1){ return 1; } else { return 0; } } $ gcc -Wall /tmp/test.c /tmp/test.c: In function ?main?: /tmp/test.c:3: warning: suggest parentheses around assignment used as truth value |
Author: | Cancer Sol [ Thu Mar 07, 2013 3:32 pm ] |
Post subject: | Re: RE:Please help fix my program :) |
DemonWasp @ 3/7/2013, 3:14 pm wrote: You forgot to tell us what's wrong with it. Does it compile? Does it do the wrong thing? When and how does it do the wrong thing? What have you tried to solve it?
You should also remember that a single equals sign (=) is assignment, and two (==) is comparison. You want to use comparison in your if statements, but you are using assignment, which will not do what you want. The compiler won't compile it I think, I'll have to post the error after when I get back home. Thanks though, everyone! |
Author: | DemonWasp [ Thu Mar 07, 2013 5:57 pm ] | ||
Post subject: | Re: RE:Please help fix my program :) | ||
Tony @ Thu Mar 07, 2013 3:26 pm wrote: DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote: You want to use comparison in your if statements, but you are using assignment, which will not do what you want.
Btw, a compiler will warn you about such mistakes. Yes! Good point, and one I should repeat in every thread about C++ problems. If you are using g++ or gcc, then you should be using at least -Wall -Wextra -Werror , as that will turn on all warnings and prevent you from compiling without fixing them. This will save you TONS of trouble later on. There are equivalents for other compilers, but I don't know them offhand. Edit: The program posted by Cancer Sol is also misusing curly braces (these: {}). You have to put them in pairs, and you have to use them to group statements. For example:
|
Author: | Cancer Sol [ Thu Mar 07, 2013 7:20 pm ] | ||
Post subject: | Re: RE:Please help fix my program :) | ||
DemonWasp @ 3/7/2013, 5:57 pm wrote: Tony @ Thu Mar 07, 2013 3:26 pm wrote: DemonWasp @ Thu Mar 07, 2013 3:14 pm wrote: You want to use comparison in your if statements, but you are using assignment, which will not do what you want.
Btw, a compiler will warn you about such mistakes. Yes! Good point, and one I should repeat in every thread about C++ problems. If you are using g++ or gcc, then you should be using at least -Wall -Wextra -Werror , as that will turn on all warnings and prevent you from compiling without fixing them. This will save you TONS of trouble later on. There are equivalents for other compilers, but I don't know them offhand. Edit: The program posted by Cancer Sol is also misusing curly braces (these: {}). You have to put them in pairs, and you have to use them to group statements. For example:
Well I think I found out the problem, but for the tut I'm using, I think that's how it was used though. Sec, I'll just go check again. Thanks for all the help though. Okay actually.. yeah, I'm missing a hell lot of curly braces. LOL. Fixed. |