2

Hello I am trying to make a game somewhat like Pokemon but I'm stuck on printing stats.
I am trying to output a level with it showing what level the animal reached.

Any help on the fact I'm not getting expected results?

My code:

visualanimal.h (not used yet):

#include <string>

#ifndef VISUALANIMAL_H
    #define VISUALANIMAL_H

using namespace std;
using namespace Animals;

/*** Visual based on original class
@author Adam Petla
***/
class VisualAnimal : Animal {
public:
    string imageFileURL;
    int size;
    VisualAnimal() {
        this->imageFileURL = "";
        this->size = 0;
    }
};
#endif

animal.h :

#include <string>
#include <stdio.h>
#include <iostream>

#ifndef ANIMAL_H
    #define ANIMAL_H

using namespace std;
namespace Animals {
    class Animal {
    public:
        string name;
        int minlevel;
        int level;
        int maxlevel;
        int baseattack;
        int baseattackraise;
        int attack;
        int basedefense;
        int basedefenseraise;
        int defense;
        int basespeed;
        int basesppedraise;
        int speed;
        int basespecial;
        int basespecialraise;
        int special;
        char type;
        Animal() {
            name = "DOG";
            minlevel = 0;
            level = 1;
            maxlevel = 100;
            baseattack = 1;
            baseattackraise = 1;
            basedefense = 1;
            basedefenseraise = 1;
            basespecial = 1;
            basespecialraise = 1;
            basespeed = 1;
            basesppedraise = 1;
        };
    private:
        void  printstats() {
            //cout << "Attack : " << this->
        };
        void raiseattack() {
            this->attack += this->baseattackraise;
        }
        void raisedefense() {
            this->defense += this->basedefenseraise ;
        }
        void raisespeed() {
            this->speed += this->basesppedraise;
        }
        void raisespecial() {
            this->special += this->basespecialraise;
        }
        void raisestats() {
            raiseattack();
            raisedefense();
            raisespeed();
            raisespecial();
        }
        void updatestats(char  type) {
            switch (type) {
            case 'l':
                raisestats();
            }
        }
       public :
            void raiselevel() {
                   this->level++ ;
                   this->type = 'l';
                   updatestats(this->type);
                   string output = "Level Up!Your " +  string(this->name) + string("has reached level");
                    cout << output;
                    cout << this->level + ".Its stats are now";
                    printstats();
            };
        };
    }
    #endif

animal.cpp :

// Animals.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include "animal.h"
#include "visualanimal.h"

using namespace std;
int main()

{

    using namespace`` Animals;
    Animal test;
    test.raiselevel();
    cout << "Hello World!\n";
    return -1;
}

My expected results: DOG reached level 2.
My actual results DOG reached level

Anybody know an answer It doesn't show any level in output anybody know why?

Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 3
    `this->level + ".Its stats are now";` does not do what you think it does, pretty sure there is a duplicate somewhere. To keep your code clean just chain the `<<` like so: `cout << this->level << ".Its stats are now";` – Lukas-T Jul 25 '20 at 11:27

2 Answers2

3

Change the following:

    cout << this->level + ".Its stats are now";

To:

    cout << this->level << ".Its stats are now";

You were getting incorrect output because you are trying to concatenate a string and an int.

The correct way to concatenate a string to an int is using std::to_string():

int val = 2;
std::string x = "hello";
std::string output = std::to_string(val) + x;
Waqar
  • 8,558
  • 4
  • 35
  • 43
1

+ has a higher precedence than <<, so the line cout << this->level + ".Its stats are now"; is evaluated as cout << (this->level + ".Its stats are now"); - the value of level is used as an offset into a string literal ".Its stats are now", which is not what you want.

Change it to

    cout << this->level << ".Its stats are now";

Or see here how to concatenate an int with a string.

rustyx
  • 80,671
  • 25
  • 200
  • 267