help needed in C++ with the if-else-if ladder.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lyle777
    New Member
    • Apr 2010
    • 8

    help needed in C++ with the if-else-if ladder.

    I need help with the if else if ladder.
    Here is my problem:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int lyle, dunn, charles, name;
        
        cout << "enter name:\n";
        cin >> name;
       
       
            if(name == 1) cout << "enter dunn:\n";
            cin >> dunn;
            cout << dunn * 2;
            else if(name == 2) cout << "enter lyle:\n";
            cin >> lyle;
            cout << lyle * 2;
            
            
        cin.get();
        cin.get();
        return 0;
    I never got to finish the if else if ladder, because my compiler already gives an
    incorrect syntax.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    if else if syntax for c++ is:

    Code:
    if (condition)
    {
      //execute this code
    }
    else if (condition)
    {
      //exectue this code
    }
    else
    {
     //execute this code
    }

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Yes, you're missing the braces around the clauses:
      { and }

      The compiler can't tell how to compile it otherwise.

      Comment

      • lyle777
        New Member
        • Apr 2010
        • 8

        #4
        Originally posted by RedSon
        if else if syntax for c++ is:

        Code:
        if (condition)
        {
          //execute this code
        }
        else if (condition)
        {
          //exectue this code
        }
        else
        {
         //execute this code
        }
        thanks a lot

        Comment

        Working...