• Welcome to Valhalla Legends Archive.
 

Problem with If Statement

Started by Dyndrilliac, January 09, 2004, 02:25 PM

Previous topic - Next topic

Dyndrilliac

#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;
}


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?
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Siege

#1
if (d=e)needs to be changed to if (d==e)

MoNksBaNe_Agahnim

also organize your equation better to better fit order of operations...


Yours...

d = (a*b+c*a+c-b);



Better...

d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like

cleanness helps a lot in programming and math in general.


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


Kp

Quote from: iago on January 09, 2004, 04:07 PM
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 ==.

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:


if ((a = rand ())) {  /* intended to have an implied != 0 here, but VS panics and warns anyway */
}


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 }).
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Quote from: Kp on January 09, 2004, 04:40 PM
Quote from: iago on January 09, 2004, 04:07 PM
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 ==.

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:


if ((a = rand ())) {  /* intended to have an implied != 0 here, but VS panics and warns anyway */
}


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 }).

You never have to use assignments like that, but yeah, it's more convenient..
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: MoNksBaNe_Agahnim on January 09, 2004, 03:04 PM

Better...

d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like

cleanness helps a lot in programming and math in general.

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.

iago

Quote from: Adron on January 09, 2004, 05:54 PM
Quote from: MoNksBaNe_Agahnim on January 09, 2004, 03:04 PM

Better...

d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like

cleanness helps a lot in programming and math in general.

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.

I prefer brackets!
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


iago

It's just more obvious.  Perhaps somebody should post a poll about this?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


UserLoser.


MoNksBaNe_Agahnim

Quote from: Adron on January 09, 2004, 05:54 PM
Quote from: MoNksBaNe_Agahnim on January 09, 2004, 03:04 PM

Better...

d = (a*b)+(c*a)+(c-b); -- if thats what you are wanting it to look like

cleanness helps a lot in programming and math in general.

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.

i don't think it makes it harder to read, i think its easy to read... multiply all those first then add them accordingly

Adron

Quote from: MoNksBaNe_Agahnim on January 09, 2004, 08:24 PM
Quote from: Adron on January 09, 2004, 05:54 PM
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.

i don't think it makes it harder to read, i think its easy to read... multiply all those first then add them accordingly

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)