Thread: help with an if statement

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    50

    help with an if statement

    hi.

    I'm trying to write a statement that says:

    if ( a=1) and (b= 1)

    then

    store 1 in c .

    Can someone help ?

    I was looking in a tutorial and saw this but get errors:

    if (a=1) && (b=1)
    c=1;

    That is what I had in my program and it gave me errors telling me that I needed a " ; " before the &&

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    50
    ok. I figured that one out. Anyone know why I would only get one error and it says "unexpected end of file found " ? I was looking at my brackets and they all look good.

    I end my file with

    cin.get();
    cin.get();


    return 0;

    }


    That last } is to close the int main () statement

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (a=1) && (b=1)
    Because your && is outside the if
    and = should be ==

    if ( (a==1) && (b==1) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    50
    oh,

    maybe I didn't figure it out

    I got this on my program and it compiled finally

    if (a==1 && b==1)
    c=1;


    Also, nevermind the end of file problem. I was missing a " } " bracket.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Yea, you have to put both the statements in the same set of parentheses.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    50
    I'm certainly no expert at C++ but I just tested my program and :

    if (a==1 && b==1)
    c=1;


    works. You could probably do it either way.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes you do not haft to have braces with if statments but your going to want to IF you want your code to be easy to read.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM