Results 1 to 7 of 7

Thread: Rookie Question

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47

    Rookie Question

    How do I use the if...elseif...elseif...else statements?
    For example a program asks you what you want to do. If you press 1, 2 numbers will be added, if you press 2, the numbers will be subtracted, if you press 3 the numbers will be multiplied, and 4 is division. Thanks

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Code:
    if(variable1 = 1)  {
       //code
    }
    elseif(variable1=2) {
      //code
    }

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Steve
    Code:
    if(variable1 = 1)
    will always be true because you are asigning 1 to variable. You should use
    Code:
    if(variable1 == 1)
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    And BloodMachine34 , to get a value ino a var use
    Code:
    #include <iostream>
    usung namespace std;
    
    int variable;
    cin>>variable;
    ...
    if(variable == 1)
    //do sth
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47
    Cmon, I'm new to C++ but I'm not that stupid

  6. #6
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Sorry Man
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  7. #7
    skara_B
    Guest

    answer to rookie question

    if(yaddayadda==number)
    {your code about adding or whatever
    }
    else
    {your code about multiplying or whatever
    }

    You can also state two times if in a row if you want to, but I think you always have to end an else.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width