Like Tree1Likes
  • 1 Post By Elysia

Needing help to reduce the damge_bar of the ship (Beginner)

This is a discussion on Needing help to reduce the damge_bar of the ship (Beginner) within the C++ Programming forums, part of the General Programming Boards category; I'm needing help with this code. The idea is that I has a ship and if I hit it with ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    92

    Question Needing help to reduce the damge_bar of the ship (Beginner)

    I'm needing help with this code. The idea is that I has a ship and if I hit it with a shoot the health bar will go from 100 and down until it is 0 then the ship doesn't exists anymore. But I have problem while saying to the program that if it was a hit do "health_bar -= 10" I know what the problem are but not how to fix it. Is it anyone that has another idea of doing this or how to do this?. Please help!.

    Code:
    #include <iostream>
    #include <cstddef>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    class Ships
    {
        public:
            int health_bar;
            string name;
    };
    
    
    Ships* getNewShip()
    {
        Ships *p_ship = new Ships;
        p_ship->name = " ";
        p_ship->health_bar = 0;
        return p_ship;
    }
    
    
    void Ship_1()
    {
        Ships *p_ship;
        p_ship->name = "Xtr-30";
        p_ship->health_bar += 100;
        cout << "Name: " << p_ship->name << endl;
        cout << "Health: " << p_ship->health_bar << endl;
    }
    
    
    int main()
    {
    bool menu = false;
    
    
    while (menu == false)
    {
        system("CLS");
        int input;
    
    
        cout << "Attack ship: ";
        cin >> input;
    
    
        if (input == 1)
        {
            Ships* getShip = getNewShip();
            Ship_1(getShip);
            system("PAUSE");
        }
    }
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Where is your attempt for the action you want to perform, captain?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    The code won't even compile.
    Also,
    >>Ships *p_ship = new Ships;
    Just don't do that. Just do
    Ships p_ship;
    then work with that. Return it if you want, even.

    Allocating with new is discouraged unless you absolutely have to (and I see no reason for doing so).
    See dynamic memory management if you want to learn about this topic (it can be quite advanced, so avoid it unless necessary; simplicity is best).
    stahta01 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    92
    Thanks got it work now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needing Help making change
    By jp1978 in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2012, 04:17 PM
  2. Bison/Yacc shift/reduce reduce/reduce
    By AnthonyGould in forum Tech Board
    Replies: 0
    Last Post: 11-20-2009, 07:15 AM
  3. Beginner Needing Help
    By MadProfessor in forum C Programming
    Replies: 4
    Last Post: 08-08-2005, 04:31 PM
  4. Yacc: reduce/reduce
    By alphaoide in forum Tech Board
    Replies: 2
    Last Post: 04-27-2005, 08:03 PM
  5. Beginner needing help in C language
    By chauncey005 in forum C Programming
    Replies: 2
    Last Post: 09-26-2003, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21