0

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?

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
TimothyTech
  • 745
  • 2
  • 11
  • 18
  • 4
    Just a hint when writing code: It's never a compiler issue. – Stephen Jun 07 '10 at 20:30
  • What compiler are you using? Can you cut some of the excess code and get this down to a close to minimal example? What do you mean "crashes"? What you you mean by your compiler shutting down after running the app (the compiler will be finished before running it)? Is there any particular reason why you said the second input when the line you said crashes is the third one? – David Thornley Jun 07 '10 at 20:32
  • 3
    Also, I applaud @MSN's reformatting efforts because what was originally there was *bad*, and it could still use some cleanup. – Daniel DiPaolo Jun 07 '10 at 20:32
  • Your code is missing the declaration of c_action. Also, your compiler isn't running your app. Your app is running in teh OS and when it reaches the end it shuts down. If you don't want it to stop, then add some code at the end (to restart it, or wait for user input for it to end, etc.). – Niki Yoshiuchi Jun 07 '10 at 20:35
  • 1
    This is wrong: `int sizeOfEnemies = sizeof(enemies) / sizeof(int);` – Brian Neal Jun 07 '10 at 20:35
  • @Stephen: Except [when it is](http://stackoverflow.com/questions/2722302/can-compiler-optimization-introduce-bugs/2722395#2722395) – BlueRaja - Danny Pflughoeft Jun 07 '10 at 20:41
  • By the way, no need to add semicolons at the end of comments lines ;) – Clément Jun 07 '10 at 20:41
  • @BlueRaja: However, "It's never a compiler issue" is an excellent assumption to make. It isn't always true, but once you start thinking "Maybe it's a compiler problem" you'll have a real hard time figuring out what's wrong. – David Thornley Jun 07 '10 at 20:45
  • @Niki, read more carefully, `c_action` is there :-) Otherwise you are right. – Péter Török Jun 07 '10 at 20:55

3 Answers3

1

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
  • 13,803
  • 4
  • 42
  • 69
0

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.
Clément
  • 12,299
  • 15
  • 75
  • 115
0

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.
  • 521
  • 3
  • 9