tags:

views:

150

answers:

3

hello okay, so i made a DOS program however my game always crashes on my second time running to the cin function.

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

//call functions
int create_enemyHP (int a);
int create_enemyAtk (int a);
int find_Enemy(int a);
int create_enemyDef (int a);

// user information
int userHP = 100;
int userAtk = 10;
int userDef = 5;
string userName;

//enemy Information
int enemyHP;
int enemyAtk;
int enemyDef;
string enemies[] = {"Raider", "Bandit", "Mugger"};
int sizeOfEnemies = sizeof(enemies) / sizeof(int);
string currentEnemy;
int chooseEnemy;

// ACTIONS 
int journey;

int test;

int main()
{
    // main menu 
    cout << "welcome brave knight, what is your name? " ;
    cin >> userName;
    cout << "welcome " << userName << " to Darland" << endl;

    //TRAVELING 

MENU:

    cout << "where would you like to travel? " << endl;
    cout << endl << " 1.> Theives Pass " << endl;
    cout << " 2.> Humble Town " << endl;
    cout << " 3.> Mission HQ " << endl;
    cin >> journey;

    if (journey == 1) 
    {

        // action variable;
        string c_action;

        cout << "beware your journey grows dangerous " << endl;

        //begins battle

        // Creating the enemy, HP ATK DEF AND TYPE. ;

        srand(time(0));

        enemyHP = create_enemyHP(userHP);

        enemyAtk = create_enemyAtk(userAtk);

        enemyDef = create_enemyDef(userDef);

        chooseEnemy = find_Enemy(sizeOfEnemies);

        currentEnemy = enemies[chooseEnemy];

        cout << " Here comes a " << currentEnemy << endl;
        cout << "stats: " << endl;
        cout << "HP :" << enemyHP << endl;
        cout << "Attack : " << enemyAtk << endl;
        cout << "Defense : " << enemyDef << endl;
ACTIONS:            
        cout << "Attack <A> | Defend <D> | Items <I>";
        cin >> c_action;

        //if ATTACK/DEFEND/ITEMS choice

        if (c_action == "A" || c_action == "a"){

            enemyHP = enemyHP - userAtk;
            cout << " you attack the enemy reducing his health to " << enemyHP << endl;
            userHP = userHP - enemyAtk;
            cout << "however he lashes back causing you to have " << userHP << "health left " << endl;
            //end of ATTACK ACTION
        }

the last line "cin >> c_action crashes. i use two other pages. they just create the functions. is it a complier issue. also why does my complier always shutdown after it runs he app. is there a way to stop it?

+1  A: 

A few hints: I never use forward declarations of functions ( such as "int create_enemyHP (int a);" ) if I can avoid them. If you do this then there are two places in your code that must be correct for your program to work. It makes life easier if there is always a "single source of truth"

Have you run this code through the debugger? It will help you find problems much more quickly.

Jay
+1 ; Prototypes should be used in class interfaces, not in such simple cases.
CFP
A: 

If your c_action variable is only intended to be a char, I'd suggest to use a char variable, rather than a string.
You might want to try this way, and if you're still faced with an error, you might give

scanf("%c", &c_action); //assuming you used a char.
CFP
A: 

I didn't understand if the program crashes before you type the "action" or after. Because if it crashes before, then I think your problems are caused by white spaces characters in the input buffer.

mp