Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Dyndrilliac on January 09, 2004, 02:25 PM

Title: Problem with If Statement
Post by: Dyndrilliac on January 09, 2004, 02:25 PM
#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?
Title: Re:Problem with If Statement
Post by: Siege on January 09, 2004, 02:40 PM
if (d=e)needs to be changed to if (d==e)
Title: Re:Problem with If Statement
Post by: MoNksBaNe_Agahnim on January 09, 2004, 03:04 PM
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.

Title: Re:Problem with If Statement
Post by: iago on January 09, 2004, 04:07 PM
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 ==.
Title: Re:Problem with If Statement
Post by: 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 }).
Title: Re:Problem with If Statement
Post by: iago on January 09, 2004, 05:40 PM
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..
Title: Re:Problem with If Statement
Post by: 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.
Title: Re:Problem with If Statement
Post by: iago on January 09, 2004, 05:55 PM
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!
Title: Re:Problem with If Statement
Post by: Adron on January 09, 2004, 05:58 PM
Quote from: iago on January 09, 2004, 05:55 PM

I prefer brackets!

Brackets? How?
Title: Re:Problem with If Statement
Post by: iago on January 09, 2004, 07:08 PM
It's just more obvious.  Perhaps somebody should post a poll about this?
Title: Re:Problem with If Statement
Post by: UserLoser. on January 09, 2004, 07:23 PM
Quote from: Dyndrilliac on January 09, 2004, 02:25 PM
#include <conio.h>


What's conio.h being used for?
Title: Re:Problem with If Statement
Post by: MoNksBaNe_Agahnim on January 09, 2004, 08:24 PM
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
Title: Re:Problem with If Statement
Post by: Adron on January 09, 2004, 08:42 PM
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)