-2

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 <iostream>
#include <string>

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] = enemy.y[1] + 1;
    enemy.y[3] = enemy.y[2] + 1;
    enemy.y[4] = enemy.y[3] + 1;

    cout << "enemy ship coordinates:" << endl;
    cout << enemy.y[0]<< "*" << endl;
    cout << enemy.y[1]<< endl;
    cout << enemy.y[2]<< endl;
    cout << enemy.y[3]<< endl;
    cout << enemy.y[4]<< endl;

    int distance=0;
    distance = good.x[1] - enemy.y[1];

    cout << "The distance between good ship and enemy ship is: " << distance << endl; 

    system("pause");
    return 0;
}
Crystal W
  • 19
  • 2
  • `Type identifier[size]` creates an array with `size` elements, ranging from `0` to `size - 1`. If an array has size 4, `array[4]` is out of bounds. – Weak to Enuma Elish Nov 10 '15 at 05:38
  • Same as I've replied to yzt, I just modified that before checking the forum, thanks though! There's still an error... not sure what's going on – Crystal W Nov 10 '15 at 05:47
  • Can you update the code to reflect the changes? – Weak to Enuma Elish Nov 10 '15 at 05:52
  • Thanks to everyone who helped me with this code. I turned this in and my teacher said I began coding three chapters ahead of everyone with this assignment (basically most of what I did here, just from the idea before posting for help on here was considered advance to my instructor) Thanks! – Crystal W Nov 14 '15 at 05:41

3 Answers3

5

The error probably comes from having only 4 coordinates in each struct, not 5. When you declare an array with int x[4];, it will only have 4 elements, namely x[0] to x[3].

There are a number of other problems:

  1. You do not need two structs for two ships. Use just one. That's the whole point of structs/classes: to represents classes of objects. Use only one struct (named e.g. Ship) and declare both your ships good and enemy to have that type.

  2. Don't be afraid of both the enemy ship and the good ship having x coordinates. The compiler and the computer won't get confused at that, and neither should you.

  3. Learn to use loops. Even if you get confused at first, remember that loops are one of the most (if not the most) important tools at a programmers disposal. Think what would happen if you had 100 ships, each with 100 coordinates...

  4. Remember, again, that the first element of an array is at index 0, not index 1. (And the last element is at index N-1.)

  5. Calculating the distance is a little more complex than you've written. Can the distance between two objects ever be negative? What happens if the enemy ship's coordinate is greater than the friendly ship? What's the actual formula for one-dimensional distances?

  6. Remove unused code. What's the use of that region variable? Have you used it anywhere?

UPDATE: (For anyone reading in the future, remember that OP has updated and modified their question and code, to the point that some of my point would not apply or would apply differently.)

  1. Do you REALLY need both xs and ys in Ship?
yzt
  • 8,873
  • 1
  • 35
  • 44
  • Just modified that before checking the forum, thanks though! There's still an error... not sure what's going on. – Crystal W Nov 10 '15 at 05:46
  • Update your question then, with the new symptoms. Does it give you a runtime error? Is the answer wrong? – yzt Nov 10 '15 at 05:53
  • I could compile and run your first set of code (except for making `int x[4]` `int x[5]`) without issues – CJCombrink Nov 10 '15 at 06:02
  • My 2cents on point 5: I do not think getting the correct values should be the focus, rather get the code to work correctly (there is a difference). If it is a maths course then yes, the distance must be correct, but for logic and design the code is more important than the correct formula. In my days many students in my class spent more time on getting the correct answer than actually producing valid code, and it costed them... It depends on what the lecturer wants though – CJCombrink Nov 10 '15 at 06:07
  • @The Badger Your 2cents is exactly one of the points of the assignment – Crystal W Nov 10 '15 at 06:20
  • @TheBadger: While I see your point, I can't agree. I do understand that learning to program and writing a program are processes, and need time and many many steps. But any 10 year old is expected to know how to calculate 1D distances. And a *way* more important lesson in life in-general (and not only programming) is learning how to apply what you already know. If you can't use what you already know, what good is learning anything new? – yzt Nov 11 '15 at 04:59
0

Not sure if the use of system("PAUSE") is something your instructor taught, but that's definitely something you could improve on, too. Explained here

Community
  • 1
  • 1
  • 1
    This is absolutely immaterial. Talking about merits and deficiencies of `system("pause")` when the programmer asking the question has problems with array indexing and loops is irrelevant, and frankly, a waste of time. – yzt Nov 10 '15 at 06:00
  • Updated the code. No errors at all! Thank you all for the help, I really appreciate it. It's helping me learn from my mistakes. I removed region. Still figuring that one out. My instructor said we could choose the size but still unsure of how to use this. I'm still fumbling the textbook and my notes for an answer to that. As for loops and range check, my teacher hasn't covered them yet so I don't think he expects us to use them. As for pause, he just told us to leave it there if I remember correctly. – Crystal W Nov 10 '15 at 06:13
  • I'd like if someone could "list improvements" ... is what I was responding to. New here, and new to the language, but it was the one thing I could offer help with so I did. No offense intended, yzt. – Jay Bennett Nov 10 '15 at 17:11
  • Thanks to everyone who helped me with this code. I turned this in and my teacher said I began coding three chapters ahead of everyone with this assignment (basically most of what I did here, just from the idea before posting for help on here was considered advance to my instructor) Thanks! – Crystal W Nov 14 '15 at 05:40
0

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

CJCombrink
  • 3,738
  • 1
  • 22
  • 38
  • Then look at each point that @yzt listed and try to address them, use loops, do range checking (what if the user inputs a coordinate of 200...), etc. – CJCombrink Nov 10 '15 at 06:00