4 Replies - 153 Views - Last Post: 01 March 2018 - 09:38 AM Rate Topic: -----

#1 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Program Crashes When I Press 'w' (C++)

Posted 01 March 2018 - 03:28 AM

I made this console game in c++
player is 'O' and is supposed to shoot out 'o' when 'w' is pressed
player moves fine , but when I try to shoot Program crashes;
Pls look at my code (if you have time) and tell me what is wrong;

I have attached the file
ThankYou (if you decide to help) :gun_bandana:

If there is no file attached .

#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;
////////////
bool gameover = false;
enum shotflag {STOP = 0, UP};
class player;
class bullet;
class screen;               //forward decl.
////////////
class bullet
{                                                                   //BULLET CLASS

    int bulletX;
    int bulletY;
    shotflag movebullet;
    bool trigger;
public:
    bullet ()
    {
    trigger = false;
    }


    friend void display(const screen , const player,const bullet* );

    bool & shoot()
    {                                                                   //TriggerFunc.
        return trigger;
    }

    friend void BulletDisplay(bullet *, player &,const screen);
    friend void BulletCreation(player &,bullet *);
    friend void BulletDestroy(player &,bullet *);

};
class   screen                                   //SCREEN CLASS
{
    int length;
    int breadth;
public:
    screen ()
    {
        length = 15;
        breadth = 20;
    }                                   //display func.

    friend void display(const screen , const player,const bullet*);
    friend void collider(const screen , player &);
    friend void InputFunc(const screen,player &);
    friend void BulletDisplay(bullet *, player &,const screen);

};

class player                                            //BASE PLAYER CLASS
{
    int posx , posy;
    int LMB;                                           //LMB = no.  of shots fired(left mouse button)
public:
    player ()
    {
        posx = 7;
        posy = 14;
    }
    void LMBset()
    {
        LMB = 0;
    }
    friend void display(const screen,const player,const bullet*);
    friend void InputFunc(const screen,player &);
    friend void collider(const screen ,  player &);
    friend void BulletDisplay(bullet *, player&,const screen);
    friend void BulletCreation(player &,bullet *);
    friend void BulletDestroy(player &,bullet *);

};

void BulletCreation(player & p,bullet *B)/>                       //BulletCreation
{
    b = new bullet[p.LMB];

    for(int i=0;i<p.LMB;i++)
    {
            b[i].movebullet = STOP;
            b[i].bulletX = p.posx;
    }

     for(int i=0;i<p.LMB;i++)
    {
        b[i].movebullet = UP;
    }

     for(int i=0;i<p.LMB;i++)
        {
            if(b[i].movebullet != STOP )
               b[i].bulletX = b[i].bulletX;

        }



}

void BulletDisplay(bullet * b, player & p,const screen s)               //BulletMovementDisplay
{
    for(int i=0;i<p.LMB;i++)
    {
            if (b[i].movebullet != STOP)
           b[i].bulletY--;

    }


}

void BulletDestroy(player & p, bullet * B)/>                      //DestroyBullet
{
    for(int i=0;i<p.LMB;i++)
    {
        if(b[i].bulletY = 0)
        p.LMB--;
        else if(b[p.LMB-1].bulletY = 0)
        {   delete b;
        }

    }
}

void collider(const screen s,player & p)                    //collider func.
{
    if(19 ==p.posx )
        p.posx = 1;
    else if(p.posx == 0)
        p.posx = 18;
        else
        p.posy = p.posy;
}                                                          //Collider Func.

void InputFunc(const screen s,player & p)
                                                                //Movement Func.
{
    if (_kbhit())
        switch (getch())
    {

    case 'a':
        p.posx--;
        break;
    case 'd':
        p.posx++;
        break;
    case 'w':
        p.LMB++;
        break;
    default:
        break;

    }



}                                                       //Movement Func.


    void display(const screen s,const player p,const bullet*B)/>                 //Display Func.
{
        for(float i=0;i<s.breadth;i++)                    //Top Border
            cout<<".";
        for(int i=0;i<s.length;i++)
        {
            cout<<endl;
            for(int j=0;j<s.breadth;j++)
            {
                if(j == 0 || j == s.breadth-1)          //Right & Left Border
                    cout<<".";
                    else if (p.posx == j && p.posy == i )
                        cout<<"O";

                    else if(p.LMB >=1)
                        for(int i1=0;i<p.LMB;i++)
                    {
                       if(b[i1].bulletX == j && b[i1].bulletY == i)
                           cout<<"o";
                            else
                                cout<<" ";
                    }


                else
                    cout<<" ";
            }
        }
        cout<<endl;
        for(int i=0;i<s.breadth;i++)                    //Down Border
            cout<<".";

    }

int main()
{
    screen S;
    player P1;
    P1.LMBset();
    bullet *B;
    while (!gameover)
{   system ("cls");


    display(S,P1,B)/>;
    InputFunc(S,P1);
    collider(S,P1);

    BulletCreation(P1,B)/>;
    BulletDisplay(B,P1,S);
    BulletDestroy(P1,B)/>;

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Program Crashes When I Press 'w' (C++)

#2 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 366
  • Joined: 09-September 16

Re: Program Crashes When I Press 'w' (C++)

Posted 01 March 2018 - 03:36 AM

In your display function you try to use an uninitialized pointer of type bullet.
I would suggest getting rid of all these raw pointers. In modern C++ you should not use them.
Much better and easier to use vectors, value sematics and pass by reference.
If really really necessary use smart pointers.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Program Crashes When I Press 'w' (C++)

Posted 01 March 2018 - 05:36 AM

View PostBihar Shooter, on 01 March 2018 - 05:28 AM, said:

player moves fine , but when I try to shoot Program crashes;


I find this highly unlikely. The code offered can't even hope to run. Why are you using pointers at all?

Not only is bullet *b; never initialized, you don't seem grasp how arrays work:
void BulletCreation(player & p, bullet * b ) {
    // do you think this will return an array?
    // it won't
    b = new bullet[p.LMB]; 
    /* 
    all of this
    for(int i=0;i<p.LMB;i++)
    {
            b[i].movebullet = STOP;
            b[i].bulletX = p.posx;
    }

     for(int i=0;i<p.LMB;i++)
    {
        b[i].movebullet = UP;
    }

     for(int i=0;i<p.LMB;i++)
        {
            if(b[i].movebullet != STOP )
               b[i].bulletX = b[i].bulletX;

        }
    is equivalent to
    */
    for(int i=0;i<p.LMB;i++) {
        b[i].movebullet = STOP;
        b[i].bulletX = p.posx;
        b[i].movebullet = UP;  // so, that = STOP is pointless
        // this will always be true
        if(b[i].movebullet != STOP ) {
            // this will always be pointless
            b[i].bulletX = b[i].bulletX;
        }
    }
}



I appreciate that you're trying to do something you think is fun. Good, do that. However, you first need to learn some basics. Practice the simple stuff for a while.
Was This Post Helpful? 1
  • +
  • -

#4 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Re: Program Crashes When I Press 'w' (C++)

Posted 01 March 2018 - 08:53 AM

View Postbaavgai, on 01 March 2018 - 05:36 AM, said:

View PostBihar Shooter, on 01 March 2018 - 05:28 AM, said:

player moves fine , but when I try to shoot Program crashes;


However, you first need to learn some basics. Practice the simple stuff for a while.


Can you pls tell me what basics
I should clear
And what simple stuff i should practice? :stupid:

BTW my player moves fine
It's only when I press 'w' the program crashes
Compiler shows no error
I use codeblocks
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Program Crashes When I Press 'w' (C++)

Posted 01 March 2018 - 09:38 AM

View PostBihar Shooter, on 01 March 2018 - 10:53 AM, said:

Can you pls tell me what basics

A book? A website? The basics should be the same regardless of where you go. The fact that you're trying to learn C++ and are even thinking of using pointers and dynamic arrays at this point is a bad sign.

Any book will cover variables, variable types, conditionals, functions, pass by reference, loops, strings, STL, collections, classes, arrays, dynamic allocation, more on classes, etc. In roughly that order.

You don't understand classes, pass by reference, and seem ignorant of STL.

View PostBihar Shooter, on 01 March 2018 - 10:53 AM, said:

BTW my player moves fine
It's only when I press 'w' the program crashes
Compiler shows no error
I use codeblocks

Well, then, turn up the warning level on that compiler. Any decent IDE should have warned you of the unassigned pointer.

Posted Image

The only thing really saving you is that *B is just not doing what you think it is. No values are being passed back.

So, quick lesson.

This is what you're doing:
#include <iostream>

void fill(int *a, int size) {
    a = new int[size];
    for (int i = 0; i < size; i++) { a[i] = i * 2; }
}

void show(int *a, int size) {
    for (int i = 0; i < size; i++) { std::cout << a[i] << ' '; }
    std::cout << std::endl;
}

int main() {
    int n = 5;
    int *a;
    fill(a, n);
    show(a, n);
    delete[] a;
    return 0;
}


This should crash horribly in several places, if it even compiles.

This is how to do it properly:
#include <iostream>

// you need to pass a pointer to a pointer that is the array, so it can be passed back
void fill(int **pa, int size) {
    *pa = new int[size];
    int *a = *pa; // point to the pointer pointer, for easier access
    for (int i = 0; i < size; i++) { a[i] = i * 2; }
}

void show(int *a, int size) {
    for (int i = 0; i < size; i++) { std::cout << a[i] << ' '; }
    std::cout << std::endl;
}

int main() {
    int n = 5;
    int *a;
    fill(&a, n);
    show(a, n);
    delete[] a;
    return 0;
}




Here's how to do it in normal C++:
#include <iostream>
#include <vector>

void fill(std::vector<int> &v, int size) {
    for (int i = 0; i < size; i++) { 
        v.push_back(i * 2);
    }
}

void show(const std::vector<int> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << std::endl;
}

int main() {
    std::vector<int> v;
    fill(v, 5);
    show(v);
    return 0;
}



If any of that looks confusing to you, good. Now you have something to discover. Good luck.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1