help with an if statement

This is a discussion on help with an if statement within the C++ Programming forums, part of the General Programming Boards category; hi. I'm trying to write a statement that says: if ( a=1) and (b= 1) then store 1 in c ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,806
    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, 06:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01: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, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21