-2

Please answer in low level English. I have a question about a piece of code, which doesn't work and I don't know why. I want to give out a field of 5x5 Points (.), it works. Then I want to save a name on a coordinate (for example 3,4) and the compiler doesn't recognizes a fault, but Visual Studio always ends the program after cout << "Name: "; and doesn't notice the command cin.get(Feld[xWert][yWert].Feldname, 19); and also doesn't notice the condition of the do-while loop. I don't know why. You can copy the piece of code 1:1 in your compiler. It would please me, if you can help me. I translated the few German words into English here:

(Feld=field ; Felddaten =data_of_the_field ; Feldbesetzt = occupied_field ; Feldname = name_of_the_field; Auswahl=choice ; Hoehe =height ; Breite= width; Wert=value)

#include <iostream>

using namespace std;

int main() {
  struct Felddaten {
    bool Feldbesetzt;
    char Feldname[20];
  };

  Felddaten Feld[5][5];
  int Auswahl;
  int Hoehe = 5;
  int Breite = 5;

  for (int x = 0; x < Hoehe; x++) {
    for (int y = 0; y < Hoehe; y++) {
      Feld[x][y].Feldbesetzt = false;
    }
  }

  for (int x = 0; x < Hoehe; x++) {
    for (int y = 0; y < Hoehe; y++) {
      if (Feld[x][y].Feldbesetzt == false)
        cout << ".";
    }
    cout << endl;
  }

  int xWert;
  int yWert;

  do {
    cout << "XPosition";
    cin >> xWert;
    cout << "YPosition";
    cin >> yWert;
    cout << "Name: ";
    cin.get(Feld[xWert][yWert].Feldname, 19);
  } while ((xWert > 4) && (yWert > 4) && (xWert < 0) && (yWert < 0));

  return 0;
}

Wyck
  • 8,140
  • 4
  • 39
  • 51
  • 3
    read the condition carefully. What value of `xWert` can be `>4` and `<0` ? Checking that condition is the last thing the code does before `main` returns – 463035818_is_not_a_number May 09 '22 at 18:01
  • if `(xWert > 4)` then `(xWert < 0)` can never be true (same thing with `yWert`) – ChrisMM May 09 '22 at 18:03
  • 2
    Also related: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/). Even though you are not using `std::getline()`, the same issue affects `cin.get()`, too. – Remy Lebeau May 09 '22 at 18:05
  • Ok thank you, but if i leave out the do while loop, the compiler also doesnt let me write a name. it stops at cout << "Name: "; like before – DanielZ May 09 '22 at 18:06
  • @DanielZ See my earlier comment about that. As for the `do..while` loop, your condition is wrong. You are allowing the user to enter an invalid coordinate and you will access `Feld` out of bounds before validating it. Change the `while` condition to `while (true)` and then use an `if` check inside the loop after reading in the coordinates, eg: `do { cout << "XPosition"; cin >> xWert; cout << "YPosition"; cin >> yWert; if (xWert < 0 || xWert > 4 || yWert < 0 || yWert > 4) { break; /* or continue, if needed */ } cout << "Name: "; cin.get(Feld[xWert][yWert].Feldname, 20); } while (true);` – Remy Lebeau May 09 '22 at 18:09
  • Hey thank you to all, with the help of remy lebeau i could fix my main problem. I needed cin.ignore(); in front of cout << "Name: "; cin.get(Feld[xWert][yWert].Feldname, 19); And about the comments of Chris and 463..., yes i made a mistake withe the do while loop too. Thank you ;) – DanielZ May 09 '22 at 18:30
  • Tactical note: Don't put `ignore` before an operation to removed unwanted characters. Sooner or later you'll find a case where you reach the ignore with no garbage in the stream and discard data you needed to process. Instead place the `ignore` after the operation that left the garbage in the stream. Sometimes this winds up being the same place, but not always. – user4581301 May 09 '22 at 18:33

0 Answers0