Thread: What is wrong with my code????

  1. #1
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408

    What is wrong with my code????

    When i compile this i don't get any error messages but when i run the prog and it gets to the attack or flee option it prints out a bunch of lines and quits!!!! I think it said something about "Call frame traceback"...

    And i know that the code isn't too good and that some stuff isn't being used yet.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>
    #include <dos.h>
    #include <fstream.h>
    
    ifstream infile;
    ofstream outfile;
    
    struct CHARACTER {
    char name[50];
    char weapon[20];
    int hp;
    int lvl;
    int damage;
    int damBonus;
    int exp;
    int nextlvlexp;
    int skill;
    };
    
    struct MONSTER {
    char* name;
    int hp;
    int damage;
    int skill;
    int defeatexp;
    
    };
    
    CHARACTER chr;
    MONSTER m;
    int battleChoice;
    int tempDam;
    int monsternr;
    int tempskill;
    
    int getMonster() {
    monsternr = rand() % 3;
    switch(chr.lvl) {
    case 1:
    switch(monsternr) {
    case 0:
    m.name = "Rat";
    m.hp = 4;
    m.damage = 2;
    m.defeatexp = 25;
    case 1:
    m.name = "Goblin";
    m.hp = 6;
    m.damage = 3;
    m.defeatexp = 40;
    case 2:
    m.name = "Giant Worm";
    m.defeatexp = 45;
    m.hp = 7;
    m.damage = 2;
    return 0;
    }
    }
    }
    
    void writeHP(int hp) {
    for (int i = 0;i<hp;i++) {
    cout << "#";
    }
    }
    
    void p() {
    while(!kbhit());
    getch();
    }
    
    int paintFight() {
    clrscr();
    cout << "\n\nYour HP: ";
    writeHP(chr.hp);
    cout << "\n\n";
    cout << "Enemy's HP: ";
    writeHP(m.hp);
    cout << "\n\n";
    }
    
    void createChar() {
    cin >> chr.name;
    chr.hp = 13;
    chr.lvl = 1;
    chr.damage = rand() % 2 +1;
    }
    void charAttack() {
    tempskill = rand() % chr.skill;
    if(tempskill > m.skill/2) {
    tempDam = rand() % chr.damage;
    m.hp = m.hp - tempDam + chr.damBonus;
    cout << "\n\nYou hit the enemy!\n\n";
    delay(2000);
    cout << "The hit does " << tempDam + chr.damBonus << "\n\n";
    delay(2000);
    } else {
    cout << "\n\nYou miss!\n\n";
    delay(2000);
    }
    }
    int mtempskill;
    void mAttack() {
    mtempskill = rand() % m.skill;
    if(tempskill > chr.skill/2) {
    chr.hp = chr.hp - m.damage;
    }
    }
    
    
    int main() {
    createChar();
    getMonster();
    cout << "You stumble upon a " << m.name;
    delay(2000);
    while(m.hp>0) {
    paintFight();
    cout << "1. Attack\n";
    cout << "2. Flee\n\n";
    cin >> battleChoice;
    switch(battleChoice) {
    case 1:
    charAttack();
    mAttack();
    continue;
    case 2:
    cout << "You flee";
    break;
    }
    }
    cout << "\n\nWhat do you want to do now?\n\n";
    cout << "1. Find another monster";
    cout << "2. Save";
    cout << "3. Quit";
    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Havent much time to look, but

    Code:
    m.name = "Rat";
    You have created a monster when you declared it glabally, therefore you nee to use strcpy() to assign a string....or even better, you could use the string class instead of char

  3. #3
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87
    0.3 FIX ALL MY PROBLEMS

    We see a lot of posts like "here is my 5 gazillion page program please fix it if there are any problems" (To quote robwhit). We (I speak at least for myself but probably others as well) would love to solve the world's problems. Unfortunately time usually has something to say about that. If you have a massive program, please do not post the entire thing on the board. It would be best to just post the small section that you believe causes the problem. Or if all the code is suspect (uh oh) then you may want to ask someone to check your code via e-mail. Pages of code are very hard to read on the board. No indentation (...at the time of this FAQ. Oh please let that change very soon! [Note from webmaster: No guarantees, but I'm working on it])

  4. #4
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    ... and also... what errors do the compiler give you?

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    Re: What is wrong with my code????

    Originally posted by ErionD
    When i compile this i don't get any error messages
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >therefore you nee to use strcpy() to assign

    The name of the monster is a pointer which is being assigned the address of a string literal so this bit should be ok. Divison by zero is crashing the program in the line

    tempskill = rand() % chr.skill;

    in charAttack(). As your character has zero skill, you are attempting to find the remainder when dividing by zero, and you've declared paintFight() as returning an int and don't bother returning a value from it.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Sorensen
    >therefore you nee to use strcpy() to assign

    The name of the monster is a pointer which is being assigned the address of a string literal so this bit should be ok.
    Sorry, you are correct...my mistake.....

    I thought the "name" was a char array as it was in CHARACTER.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM