Author | Message | Time |
---|---|---|
Dyndrilliac | [code]#include <conio.h> #include <stdio.h> #include <iostream.h> int main(int argc, char *argv[]) { int a; int b; int c; int d; int e; cout<<"Please input a random number: "; cin>>a; cout<<"Please input another random number: "; cin>>b; cout<<"Please input another random number: "; cin>>c; cout<<endl; d = (a*b+c*a+c-b); printf("The Number To Guess Has Been Calculated!\n\n"); cout<<"Please input your guess: "; cin>>e; cout<<endl; if (d=e) { cout<<"Correct! Congratulations.\a\n"<<endl; } else { cout<<"Incorrect!"; } return 0; }[/code] For some reason no matter what inouts I give it it always tells me I gave it the correct answer, which I know I didn't. Can anyone tell me why and how to fix it? | January 9, 2004, 8:25 PM |
Siege | [code]if (d=e)[/code]needs to be changed to [code]if (d==e)[/code] | January 9, 2004, 8:40 PM |
MoNksBaNe_Agahnim | also organize your equation better to better fit order of operations... [code] Yours... d = (a*b+c*a+c-b); [/code] [code] Better... d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like [/code] cleanness helps a lot in programming and math in general. | January 9, 2004, 9:04 PM |
iago | You could also use srand() and rand(). I would recommend, like kp said in a different thread, readin a good C++ book, since that would tell you things like the difference between if(a=a) and if(a==a). Also, if you're using gcc (not sure if msvs does this), do "gcc -Wall [files...]" and it will warn you if you have an = instead of an ==. | January 9, 2004, 10:07 PM |
Kp | [quote author=iago link=board=30;threadid=4666;start=0#msg39056 date=1073686062] Also, if you're using gcc (not sure if msvs does this), do "gcc -Wall [files...]" and it will warn you if you have an = instead of an ==.[/quote] MSVS can be made to do this, but I would strongly recommend against doing so. When it does, it warns about all constructs in which an assignment is performed, even if there's more going on. For instance, the following triggers that warning in MSVS: [code] if ((a = rand ())) { /* intended to have an implied != 0 here, but VS panics and warns anyway */ }[/code] It's very annoying, as I have a great deal of code that legitimately does an assignment in a conditional (like, if ((result = conversion ()) != BAD_RESULT) { do stuff }). | January 9, 2004, 10:40 PM |
iago | [quote author=Kp link=board=30;threadid=4666;start=0#msg39061 date=1073688025] [quote author=iago link=board=30;threadid=4666;start=0#msg39056 date=1073686062] Also, if you're using gcc (not sure if msvs does this), do "gcc -Wall [files...]" and it will warn you if you have an = instead of an ==.[/quote] MSVS can be made to do this, but I would strongly recommend against doing so. When it does, it warns about all constructs in which an assignment is performed, even if there's more going on. For instance, the following triggers that warning in MSVS: [code] if ((a = rand ())) { /* intended to have an implied != 0 here, but VS panics and warns anyway */ }[/code] It's very annoying, as I have a great deal of code that legitimately does an assignment in a conditional (like, if ((result = conversion ()) != BAD_RESULT) { do stuff }). [/quote] You never have to use assignments like that, but yeah, it's more convenient.. | January 9, 2004, 11:40 PM |
Adron | [quote author=MoNksBaNe_Agahnim link=board=30;threadid=4666;start=0#msg39044 date=1073682293] [code] Better... d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like [/code] cleanness helps a lot in programming and math in general. [/quote] I think that's bad code. Adding unneeded parenthesis makes the code much more confusing and harder to read. If you want to make it clearer how the values fit together, just use white-space to separate them out. | January 9, 2004, 11:54 PM |
iago | [quote author=Adron link=board=30;threadid=4666;start=0#msg39084 date=1073692472] [quote author=MoNksBaNe_Agahnim link=board=30;threadid=4666;start=0#msg39044 date=1073682293] [code] Better... d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like [/code] cleanness helps a lot in programming and math in general. [/quote] I think that's bad code. Adding unneeded parenthesis makes the code much more confusing and harder to read. If you want to make it clearer how the values fit together, just use white-space to separate them out. [/quote] I prefer brackets! | January 9, 2004, 11:55 PM |
Adron | [quote author=iago link=board=30;threadid=4666;start=0#msg39085 date=1073692526] I prefer brackets! [/quote] Brackets? How? | January 9, 2004, 11:58 PM |
iago | It's just more obvious. Perhaps somebody should post a poll about this? | January 10, 2004, 1:08 AM |
UserLoser. | [quote author=Dyndrilliac link=board=30;threadid=4666;start=0#msg39042 date=1073679927] [code]#include <conio.h> [/code] [/quote] What's conio.h being used for? | January 10, 2004, 1:23 AM |
MoNksBaNe_Agahnim | [quote author=Adron link=board=30;threadid=4666;start=0#msg39084 date=1073692472] [quote author=MoNksBaNe_Agahnim link=board=30;threadid=4666;start=0#msg39044 date=1073682293] [code] Better... d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like [/code] cleanness helps a lot in programming and math in general. [/quote] I think that's bad code. Adding unneeded parenthesis makes the code much more confusing and harder to read. If you want to make it clearer how the values fit together, just use white-space to separate them out. [/quote] i don't think it makes it harder to read, i think its easy to read... multiply all those first then add them accordingly | January 10, 2004, 2:24 AM |
Adron | [quote author=MoNksBaNe_Agahnim link=board=30;threadid=4666;start=0#msg39106 date=1073701457] [quote author=Adron link=board=30;threadid=4666;start=0#msg39084 date=1073692472] I think that's bad code. Adding unneeded parenthesis makes the code much more confusing and harder to read. If you want to make it clearer how the values fit together, just use white-space to separate them out. [/quote] i don't think it makes it harder to read, i think its easy to read... multiply all those first then add them accordingly [/quote] Multiplying first and adding later is the default. Look at these, written the way you'd write any math expression: a + 2*b + 3*c*d + e a + (2*b + 3*c) * d + e (a + 2*b + 3*c) * d + e a + 2*b + 3*c*d + e (a + 2*(b+3)*c) * d + e Remember, doing multiplications first is the math way of doing it. Adding priorities that don't change the default precedence only gives you more to think about trying to make out what they mean: ((a + (2 * b)) + ((3 * c)*d)) + e ((a + (2 * b + (3 * c))*d) + e) (((a + 2 * b) + (3 * c))*d + e) (a + (2 * b + (3 * (c * d) + e))) (((a + (2*(b + 3))*c) * d) + e) | January 10, 2004, 2:42 AM |