this is doing my head in!!

This is a discussion on this is doing my head in!! within the C++ Programming forums, part of the General Programming Boards category; i have written the start of my new text game, and it compiles fine and I got no warnings either, ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    this is doing my head in!!

    i have written the start of my new text game, and it compiles fine and I got no warnings either, but, when I run it, if you pressed D to go to drakes inn with 1 item, it is locked, which is fine, but, after it goes back to the sign post function, and you press D again, it skips back to the end credits!!! I am sure it should not do that, but i can't see where I am going wrong, as if there was a problem with my program in functions, the compiler would of told me, I spoke to my dad who works for IBM, and he said he can't see a reason either, do you goes know how to fix it? can i put a BREAK in the function perhaps?

    Sorry to bleat on, heres the code:

    Code:
    #include<iostream>
    #include<string>
    #include<conio.h>
    #include<algorithm>
    #include<vector>
    #include<cctype>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    using namespace std;
    
    void intro();
    void game();    // functions defined
    void status();
    void townsign();
    void gameover();
    void endcredits();
    
    
    
    char pname[64];
    char attack;
    char choice;
    int gold = 0;   // player varibles
    int level = 1;
    int Exp = 0;
    int health = 100, MinHealth = 0, MaxHealth = 200; // player health
    int items = 0;
    
    char enemy[64];
    int Ehealth;
    int Edamage;
    int Emindamage;  // enemy varibles
    int Emaxdamage;
    bool Ealive;
    bool Edead;
    
    int size; // used for text scroll
    int x;
    
    int chapter; // level varible name
    
    int main()
    {
    intro();
    game();
    status();
    townsign();
    gameover();
    endcredits();
           
    } // end main()
    
    void intro()
    {
    
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("\t\t|=================================|\n"
           "\t\t|   D A I S Y 'S   Q U E S T      |\n"
           "\t\t|                                 |\n"
           "\t\t|   Press P to Play               |\n"
           "\t\t|                                 |\n"
           "\t\t|   Press L to Load Game          |\n"
           "\t\t|                                 |\n"
           "\t\t|   Press Q to Quit               |\n"
           "\t\t|=================================|\n");
    
        Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n"); 
       Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n");
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\t\tProgramed By Peter Watts\n");
        Sleep(100);
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        cout << "Selection: " << endl;
        cin >> choice;
        
        if (choice == 'p')
        {
        game();
        }
        
        else if (choice == 'q')
        {
        system("cls");
        printf("Goodbye, Daisy hopes to see you soon...\n");
        Sleep(3000);
        exit(0);
        }
        
        else if (choice == 'l')
        {
        system("cls");
        printf("Sorry, the LOAD feature is not yet avalible...\n");
        Sleep(3000);
        intro();
        }
        } // ends intro()
    
    void game()
    {
    /* SET ENEMY VARIBLES FOR FOLLOWING GAME FUNCTION*/
    
    if (chapter == 1)
    {
    {
    char enemy[64]="small dog";
    Ehealth = 20;
    Edamage = 10;
    Emaxdamage = 10;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="disfigured girl";
    Ehealth = 40;
    Edamage = 15;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="small bat";
    Ehealth = 10;
    Edamage = 5;
    Emaxdamage = 5;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="Villager Boss";
    Ehealth = 80;
    Edamage = 10;
    Emaxdamage = 15;
    Emindamage = 0;
    Ealive = true;
    }
    }
    
    else if (chapter == 2)
    {
    {
    char enemy[64]="sewer spider";
    Ehealth = 20;
    Edamage = 10;
    Emaxdamage = 15;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="sewer worker";
    Ehealth = 30;
    Edamage = 15;
    Emaxdamage = 15;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="mutant snail";
    Ehealth = 40;
    Edamage = 20;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="caged worm demon Boss";
    Ehealth = 100;
    Edamage = 15;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    }
    
    else if (chapter == 3)
    {
    {
    char enemy[64]="spirit sailor";
    Ehealth = 0;
    Edamage = 0;
    Emaxdamage = 0;
    Emindamage = 0;
    Ealive = true;
    Edead = true;
    }
    {
    char enemy[64]="haunted book";
    Ehealth = 25;
    Edamage = 10;
    Emaxdamage = 10;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="phamtom of the Oprea";
    Ehealth = 40;
    Edamage = 30;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="evil fairy Boss";
    Ehealth = 150;
    Edamage = 40;
    Emaxdamage = 25;
    Emindamage = 0;
    Ealive = true;
    }
    }
    
    else if (chapter == 4)
    {
    {
    char enemy[64]="castle soldier";
    Ehealth = 30;
    Edamage = 15;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="resident monk";
    Ehealth = 30;
    Edamage = 15;
    Emaxdamage = 15;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="spider hunter";
    Ehealth = 40;
    Edamage = 20;
    Emaxdamage = 25;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="dark servent";
    Ehealth = 50;
    Edamage = 30;
    Emaxdamage = 30;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="drucia Final Boss";
    Ehealth = 300;
    Edamage = 50;
    Emaxdamage = 25;
    Emindamage = 10;
    Ealive = true;
    }
    }
    
    /* SET PLAYER UPGRADE VARIBLES FOR GAME()*/
    
    if (Exp == 6)
    {
    cout << "You have reached Level 2!\n" << endl;
    level=+1;
    }
    else if (Exp == 12)
    {
    cout << "You have reached Level 3!\n" << endl;
    level=+1;
    }
    else if (Exp == 21)
    {
    cout << "You have reached Level 4!\n" << endl;
    level=+1;
    }
    else if (Exp == 33)
    {
    cout << "You have reached Level 5!\n" << endl;
    level=+1;
    }
    else if (Exp == 48)
    {
    cout << "You have reached Level 6!\n" << endl;
    level=+1;
    }
    else if (Exp == 69)
    {
    cout << "You have reached Level 7!\n" << endl;
    level=+1;
    }
    else if (Exp == 92)
    {
    cout << "You have reached Level 8!\n" << endl;
    level=+1;
    }
    
    /* GAME OVER*/
    
    if (health <=0) // if player health drops below or equal to zero, then gameover function is called
    {
    gameover();
    }
    
    
    
    
    
    system("cls");
    
    char senta[]="Hello there!\nWould you like to do a poor Princess a big favour?\n"
                 "I hope you will help me, as my land is in terrible danger...\n"
                 "We have been over-run by Drucila's evil army of demons.\n"
                 "I have tried my hand at fighting back, but it has come to no\n"
                 "avail...\n"
                 "\n\n"
                 "So, will you help me?\n"
                 "\n\n";
                 
                size=strlen(senta);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",senta[x]);
                }    
                cout << "Will you help? (Y/N) " << endl;
                cin >> choice;
                
                if (choice == 'n')
                {
                system("cls");
                
    char sentwh[]="Oh well, I suppose it was worth me asking....\n"
                  "It's a shame though, you could of been my ideal\n"
                  "adventurer...\n"
                  "\n\n";
                  
                size=strlen(sentwh);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentwh[x]);
                }    
                Sleep(2000);
                gameover();
                }
                
                else if (choice == 'y')
                {
                system("cls");
                
                items=+1;
                gold=+100;
                Exp=+1;
                
    char sentb[]="Oh thank you so much adventurer!\nHold on, I am a Princess! I don't\n"
                 "think I should be addressing you as adventurer now should I?\n"
                 "So, brave traveller, what do I call you? ";
                 
                 size=strlen(sentb);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentb[x]);
                }  
                
                cin >> pname; 
                
                system("cls");
                
    char sentc[]="I do appoligise ";
    
                size=strlen(sentc);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentc[x]);
                }  
                
               printf("%s!",pname); 
    
    char sentd[]="\n\n"
                 "It was my poor mannors.  So then, as you have been so very kind\n"
                 "in assisting me defeat Drucila, I give you some GOLD and a LETTER\n"
                 "Please take care of the letter, it was a sort of family airloom.\n"
                 "Well, enough of all this chat, lets get going....\n"
                 "\n\n";
                 
                 size=strlen(sentd);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentd[x]);
                }  
                Sleep(2000);     
                } 
                
                system("cls");
                
                cout << "\t\tMake a Selection...\n" << endl;
                cout << "\tPress E to explore the area\n" << endl;
                cout << "\tPress S to view your stats\n" << endl;
                cout << "\n\n";
                cin >> choice;
                 
                if (choice == 's')
                {
                status();
                }
                
                else if (choice == 'e')
                {
                system("cls");
                
    char sente[]="You walk around for a bit and find yourself inside a large\n"
                 "town square.  Before you is a sign post, you look up and\n"
                 "discover that it has a few directions displayed on it..\n"
                 "\n\n";
                 
                 size=strlen(sente);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sente[x]);
                } 
                Sleep(2000); 
                townsign();
                }
                
    
                
                if (choice == 'd' && items == 1)
                {
                system("cls");
                
    char sentil[]="The inn seems to be locked, maybe the inn keeper has gone out\n"
                  "somwhere....\n";
                  
                   size=strlen(sentil);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentil[x]);
                }  
                Sleep(2000);
                townsign();
                }
                
                else if (choice == 'd' && items == 2)
                {
                system("cls");
                
    char sentdi[]="\t\tDRAKES INN\n"
                  "\n\n"
                  "You enter the cosy inn, and a petite young lady is stood\n"
                  "behind the counter smiling at you.\n"
                  "\n\n"
                  "She asks you how she can help you, then offers a selection of\n"
                  "choices...\n"
                  "\n\n"
                  "Spend the night -  20 gold  (Press S)\n"
                  "\n\n"
                  "Eat a hearty meal - 10 gold  (Press E)\n"
                  "\n\n"
                  "Nothing really - FREE!!  (Press N)\n"
                  "\n\n";
                  
                   size=strlen(sentdi);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentdi[x]);
                }  
                cout << "Selection: " << endl;
                cin >> choice;
                }           
                                                   
                                       
                 
                
    endcredits();            
    } // ends game(), hence, the whole program
    
    
    
    void status()
    {
    system("cls");
    
    cout << "\t\tS T A T U S\n" << endl;
    cout << "\n\n";
    cout << "Player Name: " << pname;
    cout << "\n\n";
    cout << "Items Found: " << items;
    cout << "\n\n";
    cout << "Player life: " << health;
    cout << "\n\n";
    cout << "Total Gold:  " << gold;
    cout << "\n\n";
    cout << "Ezperience:  " << Exp;
    cout << "\n\n";
    cout << "Player Levl: " << level;
    cout << "\n\n";
    cout << "Hit a key!\n" << endl;
    getch();
    system("cls");
    
    } // end of status()
    
    void townsign()    
    {        
    system("cls");
    
    char sentf[]="\t\tFaucet Town Square\n"
                 "\n\n"
                 "Press D for drakes inn\n"
                 "\n\n"
                 "Press M for Faucet market\n"
                 "\n\n"
                 "Press C for the old church\n"
                 "\n\n"; 
                 
                  size=strlen(sentf);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentf[x]);
                }
                
                cout << "Selection: " << endl;
                cin >> choice;
                }
    
    void gameover()
    {
    system("cls");
    
    char sentgo[]="You fall to floor, all your life force\n"
                  "now extinguished.  Daisy's Kingdom is now doomed..\n"
                  "\n\n"
                  "\n\n\n\n\n\t\tG     A     M     E     O     V     E     R . . .\n"
                  "\n\n";
                  
                  size=strlen(sentgo);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentgo[x]);
                }    
                Sleep(3000);
                exit(0);
                } // end of gameover()
                
    void endcredits()
    {
    system("cls");
    
    char sentec[]="\t\tD A I S Y'S   Q U E S T\n"
                  "\n\n"
                  "\tProgramed by:   Peter Watts\n"
                  "\n\n"
                  "\tMusic and Fx:   Peter Watts\n"
                  "\n\n"
                  "\tGraphic Design: Alan Pink\n"
                  "\n\n"
                  "\tI would likw to thank a few people, as if it\n"
                  "\n\n"
                  "\twas not for their hard work, this project would\n"
                  "\n\n"
                  "\tnot of been finished.... Firstly myself for putting\n"
                  "\n\n"
                  "\tup with so many stupid little errors in syntax,\n"
                  "\n\n"
                  "\tsecond to alan for his skilled hand on 'PAINT', and\n"
                  "\n\n"
                  "\tfinally, I would like to thank you, YES YOU... because\n"
                  "\n\n"
                  "\twithout you playing it, then it would only mean a waste of\n"
                  "\n\n"
                  "\ttime for me...\n"
                  "\n\n"
                  "\n\n"
                  "\n\n"
                  "\t\t\t\tF    I    N\n"
                  "\n\n"
                  "\n\n"
                  "\t\tAs you leave the kingdom, you see a shadow of a recent foe\n"
                  "\n\n"
                  "\t\tstanding over you....\n"
                  "\n\n";
                  
                  size=strlen(sentec);
                for(x=0;x<size;x++)
                {   
                Sleep(40); 
                printf("%c",sentec[x]);
                }    
                Sleep(3000);
                exit(0);
                } // end of gamecredits()

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    After you accept user input into your char variable, you should ignore any trailing characters left in your input stream:

    Code:
    #include<limits>
    
    
    cin >> choice;
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n"); 
       Sleep(100);
        printf("\n"); 
        Sleep(100);
        printf("\n");
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\t\tProgramed By Peter Watts\n");
        Sleep(100);
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
        printf("\n");   
        Sleep(100);
    Ever use a for-loop? If you find yourself typing a bunch of lines over and over and a feeling of drudgery overtakes you, that feeling should be trained to alert you that you are doing something wrong.

    int chapter; // level varible name
    Ever hear the admonition: "always initialize your variables" ? Since chapter equals some junk value, none of the chapter if statements in the first part of the game() function are true, and therefore none of them are executed. Since you also have this to start:

    int Exp = 0;

    none of the Exp if statements are executed either.
    ------


    Code:
    if (chapter == 1)
    {
    {
    Why do you have two opening braces there?

    Suppose chapter is equal to 1, what will the value of Ehealth be after the code below executes? What about Edamage?
    Code:
    if (chapter == 1)
    {
    {
    char enemy[64]="small dog";
    Ehealth = 20;
    Edamage = 10;
    Emaxdamage = 10;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="disfigured girl";
    Ehealth = 40;
    Edamage = 15;
    Emaxdamage = 20;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="small bat";
    Ehealth = 10;
    Edamage = 5;
    Emaxdamage = 5;
    Emindamage = 0;
    Ealive = true;
    }
    {
    char enemy[64]="Villager Boss";
    Ehealth = 80;
    Edamage = 10;
    Emaxdamage = 15;
    Emindamage = 0;
    Ealive = true;
    }
    }
    --------

    Code:
    level=+1;
    You have a syntax error. Look at the following code and try to guess what level is equal to when it is displayed:
    Code:
    int level = 0;
    level=+1;
    level=+1;
    level=+1;
    
    cout<<level<<endl;
    Now, run the code and see if you were correct.

    You have so many errors it's hard to list them all. Here is a hint: when you are writing programs write one function, then compile your program and test the function to make sure it works correctly. Once it's working correctly, move on to the next function.
    Last edited by 7stud; 06-07-2005 at 03:00 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Nit... the prefered versions of these headers add a "c" to the beginning and drop the ".h" extension:

    Code:
    #include <cstdio>  // No .h extension
    #include <cstdlib> // No .h extension
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. Cant head insert on doubly linked list
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 09-18-2005, 11:31 AM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 07:33 AM
  5. Linked list, is this correct?
    By scrappy in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 11:06 PM

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