else if problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: else if problem

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    else if problem

    Hi

    Im fairly new to c++ and ive got stuck with else if statements. I keep getting"error: expected primary-expression before "else""
    this is the code, i know its probably really simple i just cant work it out

    if (xpgo==1)
    xp--;
    hp++;
    cout<<"You hae added 1 xp to your HP!\n";


    else if(xpgo==2)
    xp--;
    cout<<"You hae added 1 xp to your ATTACK!\n";
    at++;

    else if(xpgo==3)
    xp--;
    cout<<"You hae added 1 xp to your DEFENCE!\n";
    def++;


    else
    cout<<"You have input and invalid number please try again\n";


    thanks

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: else if problem

    You need to brace compound statements.
    Code:
    if (condition )
       statement
    else
       statement
    is fine for single statements but when you need compound statements you should do
    Code:
    if (condition)
    {
       statement
       statement
       statement
    }
    else
    {
       statement
       statement
    }
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Aug 2009
    Posts
    2

    Re: else if problem

    ahhh thankyou very much i knew itd be simple.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center