0

I was trying to make a game with C++ and noticed that there is Process Killed in the 4th line of the output. I am just curious about what is it.

My Code

#include <iostream>
#include <string>
#include <ctime>
#include <unistd.h>
using namespace std;

class Health {
    public:
    int Nhealth = 1000;
    int Mhealth = 1000;
};

class AttackMagicPower : public Health { // You can ignore this function
    public:
    int MagicAttackCommand;
    
    void AttackMagicPowerPlan() {
        cin >> MagicAttackCommand;
        if (MagicAttackCommand = 1) {
            Mhealth = Mhealth - 200;
        }
    }
};

class Enemy {
    public:
    void coutx() {
        srand(time(0) + time(0));
        string a[3];
        a[0] = "Monster! - PUNCH";
        a[1] = "Monster! - KICK";
        a[2] = "Monster! - COMBO";
        int randomcout = (rand() % 3);
        cout << a[randomcout] << endl;
        cout << "#########################" << endl;
    }
};

class Character {
    public:
    int startSTOPmatch;
    void coutxy() {
        string coutt;
        cout << "Enter Your Command" << endl;
        cin >> coutt;
        if (coutt == "KICK" || coutt == "PUNCH" || coutt == "COMBO") {
            cout << "#########################" << endl;
            cout << "Ninja! - " << coutt << endl;
            startSTOPmatch = 1;
        }
        else {
            cout << "Your Command Was Wrong...You Lost The Match" << endl;
            cout << "You May Retry!" << endl;
            startSTOPmatch = 0;
        }
    }
};

class mainx : public Health, public Character {
    public:
    mainx() {
        int random = (rand() % 10);
        Enemy e;
        cout << "Ladies and Gentlemen! Welcome to the fight of a Ninja and a Monstor\n";
        sleep(1.0);
        cout << "Hope You Will Enjoy!\n";
        sleep(0.5);
        cout << "__________________________________________________________";
        cout << "_____________\n\n" << endl;
        cout << "You and the monster can perform the following actions - \nKICK, ";
        cout << "PUNCH, COMBO" << endl;
        sleep(2.0);
        cout << "_____________________________" << endl;
        cout << "Monster Health - 1000" << endl;
        cout << "Ninja Health - 1000" << endl;
        cout << "_____________________________" << endl;
        cout << " " << endl;
        sleep(1.0);
            do {
                
                srand(time(0));
                int randomHealthLowNormal = (rand() % 101);
                srand(time(0) + random);
                int xandomHealthLowNormal = (rand() % 101);
                Mhealth = Mhealth - randomHealthLowNormal;
                Nhealth = Nhealth - xandomHealthLowNormal;
                coutxy();
                if (startSTOPmatch = 1) {
                    e.coutx();
                    sleep(1.0);
                    cout << "_____________________________" << endl;
                    cout << "Monster Health - " << Mhealth << endl;
                    cout << "Ninja Health - " << Nhealth << endl;
                    cout << "_____________________________" << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    cout << " " << endl;
                    sleep(3.0);
                } else {
                    cout << " " << endl;
                }
           } while (Mhealth > 0 || Nhealth > 0);
        if (Nhealth < 0 || Nhealth == 0 || startSTOPmatch == 0) {
                cout << "Monster Won The Match!" << endl;
            }
            else if (Mhealth < 0 || Mhealth == 0) {
                cout << "Ninja Won The Match!" << endl;
            }
    }
};

int main() {
    mainx m;
    return 0;
}

My Output

+ g++ -w -Wall -std=c++14 -O2 -o main.out main.cpp -lm


Process Killed.


+ ./main.out

>>>Ladies and Gentlemen! Welcome to the fight of a Ninja and a Monster

>>>Hope You Will Enjoy!
_______________________________________________________________________


>>>
You and the monster can perform the following actions - 
KICK, PUNCH, COMBO

>>>_____________________________
Monster Health - 1000
Ninja Health - 1000
_____________________________
 

>>>Enter Your Command
>>>
Naitik
  • 55
  • 1
  • 8
  • 2
    It might be caused by the host not having enough RAM for g++ to work properly. – Ali Tavakol Dec 04 '20 at 17:24
  • Have you tried the `g++ -w -Wall -std=c++14 -O2 -o main.out main.cpp -lm` command again? Is there something in your `dmesg` like OOM_Killer? – drescherjm Dec 04 '20 at 17:24
  • 1
    Probably not related, but you have a couple of cases where you use `=` where I think you mean `==`. – Fred Larson Dec 04 '20 at 17:26
  • `srand(time(0) + time(0));` I see that you prevent it from being called more than 1 time per second however I would just seed the random number generator 1 time in `int main()` instead of reseeding mostly for each random number. – drescherjm Dec 04 '20 at 17:28
  • 1
    `if (x = 1)` sets x to 1, then since non-zero is *truthy*, it results in `true` expression for the if predicate. `srand` should only be called once, probably near the beginning of the `main` block. The `mainx` constructor is doing way too much. A constructor should just do construction — move that to a `void run_loop()` method of `mainx`. – Eljay Dec 04 '20 at 17:42

1 Answers1

3

What Is The Meaning Of “Process Killed” In C++?

It is probably a message given by your operating system since processes are unknown to C++. I guess that if your OS is Unix-like or POSIX (e.g. MacOSX, Linux), that message was output by your Linux or Unix shell (perhaps GNU bash or zsh). You need to understand what process was killed. On Linux, see signal(7) and ps(1), strace(1), top(1).

I recommend reading a good textbook on operating systems. Perhaps start writing your own OS, see OSDEV for more.

I also recommend reading some good C++ programming book, and the documentation of your C++ compiler, e.g. GCC or Clang/LLVM.

g++ -w -Wall -std=c++14 -O2 -o main.out main.cpp -lm

Try at least g++ -Wall -Wextra -std=c++14 -O2 -g -o main.out main.cpp -lm then use gdb(1). You could even be interested in the -fanalyzer option to recent GCC.

Once you have read a good C++ programming book, consider looking inside this C++ reference website, and read the documentation of your debugger, e.g. GDB.

Later read some C++ draft standard, perhaps n4849

Study also for inspiration the source code of existing C++ open source programs or libraries (e.g. on github or gitlab), such as Fish, Qt, RefPerSys, FLTK and of course both GCC and Clang/LLVM.

Consider perhaps writing your own GCC plugin to analyze (and detect some bugs at compile-time) your C++ code. Or consider using the Clang static analyzer or Frama-C (or perhaps in 2021 my Bismon described in this draft report funded by CHARIOT & DECODER projects)

Be however aware of theoretical limitations such as Rice's theorem.

See also this answer. It might interest you.

(Note: It is my first game project so if there are any game developers here, please suggest me how to get started with pixel game development in the comments)

Use some libraries, such as libSFML or Qt or libSDL. You might want to also use Wt or sqlite. If on Linux, RefPerSys might be interesting too. BTW, you'll have more fun in joining some existing open source project than in working alone on your own one.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    This is how to schedule oneself 20 years of studying computer science :D – bloody Dec 04 '20 at 17:37
  • 2
    I would rather say 40 years. I start studying computer science in 1975, and my late father was himself a computer scientist (the author of the first commercial French compiler, [PAF](http://www.feb-patrimoine.com/english/paf_starynkevitcha.htm)) – Basile Starynkevitch Dec 04 '20 at 17:38
  • @BasileStarynkevitch interesting. Thanks for sharing – drescherjm Dec 04 '20 at 17:52
  • The funny thing is that `PAF` was very `BASIC`-like, but appeared in 1959. BASIC appeared in 1984. I was lucky to code on a [CAB 500](https://en.wikipedia.org/wiki/CAB500) computer (in a Paris Museum) in PAF. That CAB 500 thing was as old as I am, and my own father coded its compiler. – Basile Starynkevitch Dec 04 '20 at 17:55