(C++)1 dimensional battleship game help? advice for improvement

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Unfortunately I do not have the Instructor to aid me with this assignment over the weekend and I am stuck. I'm just learning C++ and I've taken a Logic and Design class for Programming but like I said I'm very new to C++. I'm having a hard time catching up to the rest of the students.

I'd like if someone could list improvements and maybe clarify if I've done anything wrong in comparison to the assignment statement. I do really appreciate the help!

My code is repetitive and I'm sure I could go another way into displaying the array values without all that code. An error also pops up after use of the application that says:

"Run-Time Check Failure #2 - Stack around the variable 'enemy' was corrupted.

If there is a handler for this exception, the program may be safely continued."

The assignment is:

"Create a Battleship struct containing 5 one-dimensional integer coordinates representing its location within a region (of any size). Instantiate 2 copies of the struct and have the user enter a single coordinate for each Battleship. Design your code to take this single coordinate and use it to populate the remaining 4 coordinates for each ship. Do this for both ship structs. Then, have your code calculate the numeric distance between the 2 ships based on their respective coordinates. Finally, display the resulting distance to the user with an English language sentence."

My code as for right now is :

#include 
#include 

using namespace std;

struct Ship
{
    int x[5];
    int y[5];
};

int main()
{
    Ship good;
    Ship enemy;

    good.x[0] = 0;
    enemy.y[0] = 0;

    cout << "Enter a coordinate (out of 100) for good ship: "<< endl;
    cin >> good.x[0];

    good.x[1] = good.x[0] + 1;
    good.x[2] = good.x[1] + 1;
    good.x[3] = good.x[2] + 1;
    good.x[4] = good.x[3] + 1;

    cout << "Good ship coordinates:" << endl;
    cout << good.x[0]<< "*" << endl;
    cout << good.x[1]<< endl;
    cout << good.x[2]<< endl;
    cout << good.x[3]<< endl;
    cout << good.x[4]<< endl;

    cout << "Enter a coordinate (out of 100) for enemy ship: "<< endl;
    cin >> enemy.y[0];

    enemy.y[1] = enemy.y[0] + 1;
    enemy.y[2
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

So starting with

Create a Battleship struct containing 5 one-dimensional integer coordinates representing its location within a region (of any size). Instantiate 2 copies of the struct and have the user enter a single coordinate for each Battleship

You need a single struct:

struct Ship
{
    int x[5];
};

Now make 2 copies

int main()
{
    Ship good;
    Ship bad;
    ...

Then the rest looks good, it compiles and runs without any issues on my computer. You can add a function to populate the ship to reduce the number of code

Ship createShip(int startPos) {
    Ship newShip;
    newShip[0] = startPos;
    // ... <- rest of your code that you have to populate
    return newShip;
}

int main() 
{ 
    int pos;
    cout << "Enter a coordinate (out of 100) for good ship: "<< endl;
    cin >> pos;
    Ship good = createShip(pos);
    //... 
    //... <- Get pos of bad ship
    Ship bad = createShip(pos); 
 }

Then you can also create a simular function that prints the location of the ship


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.