Thread: help with my text based game code

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    4

    help with my text based game code

    help with my  text based game code-help-pnghelp with my  text based game code-help2-jpg the error is pointing me at line 55

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you have semi-colons where they should not be, e.g.,
    Code:
    if(cho=='Y');
    should have been:
    Code:
    if(cho=='Y')
    likewise for the else.

    These semi-colons are basically empty statements, i.e., it is as if you wrote:
    Code:
    if
    {
    }
    
    {
        /* the intended body of your if statement */
    }
    instead of:
    Code:
    if
    {
        /* the intended body of your if statement */
    }
    Consequently, the else could not be matched to the if, hence the compile error.

    By the way, it is better for you to post code in [code][/code] tags instead of posting an image of your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please post your code here (using code tags), rather than a screen shot.

    Line 55:

    Code:
    else(cho=='n');
    An "else" shouldn't shouldn't be followed by a conditional expression like that, it defeats the whole purpose of "else".

    Code:
    if ( /* expression */)
        /* do something 1 */
    else
        /* do something 2 */
    The same is true even if you use an "if-else" chain:

    Code:
    if ( /* expression 1 */)
        /* do something 1 */
    else if ( /* expression 2 */)
        /* do something 2 */
    else
        /* do something 3 */


    Lines 60 and 61:

    Code:
    scanf("%lf", &cho2);
    if(cho2=='fight');
    1. "cho2" is a double, you're reading it as a double, so why are you trying to compare it to a word?
    2. If you want to use the word "fight" (which it doesn't seem you do), then it needs to be enclosed in double quotes to be a proper string.



    A couple of other observations (I could probably tell you more if I had actual code instead of a screenshot):

    • "rand()" is a function from stdlib.h, you shouldn't declare your own function with this name
    • Use more descriptive variable names: things like "ack", "pts", "cho", "cho2", etc make it difficult to read your code. Using complete names ("attack", "points", etc) make your code easier to read and more self-documenting.
    • If "cho" and "cho2" are doubles, why are you treating them as characters (e.g. comparing them to 'Y' and 'n')?
    • You have semi-colons after some of your "if()"s - this is wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. First Text based game.
    By skatenjoi922 in forum C Programming
    Replies: 19
    Last Post: 12-13-2012, 06:45 PM
  2. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 04:39 PM
  3. Text-based game in C++
    By Nectarios in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2008, 07:12 PM
  4. Text based game
    By beene in forum Game Programming
    Replies: 10
    Last Post: 05-12-2008, 07:52 PM
  5. Text based game
    By beene in forum Game Programming
    Replies: 7
    Last Post: 07-09-2007, 03:39 AM

Tags for this Thread