000
21.04.2009, 05:21
Thereptil
|
dieser code läst sich ausführen stürzt aber dann mit einem zugrifffehler ab hat jemand Ahnung warum
#include <iostream> #include <fstream> #include <string>
using namespace std;
const int breide = 14; const int hoehe = 16; int nx, ny; string Dateiname;
struct Nachbar { int nx,ny;
};
struct shex { int nhoehe; int nboden; Nachbar nNachbar0, nNachbar1, nNachbar2, nNachbar3, nNachbar4, nNachbar5; };
shex hexfeld[breide][hoehe];
int get_hoehe(int nx, int ny) { int nHoehe; nHoehe = hexfeld[nx][ny].nhoehe; return nHoehe; };
int get_boden(int nx, int ny) { int nBoden; nBoden = hexfeld[nx][ny].nboden; return nBoden; };
void lese_datei() { string Dateiname; ifstream dat_ein;
cout << "Geben Sie den Namen der Datei ein: "; cin >> Dateiname; cout << endl;
dat_ein.open(Dateiname.c_str(), ios_base::in);
if(!dat_ein) { cout << "Datei konnte nicht geoeffnet werden!"; cout << endl; }
for (int y=1; y<18; y++) { for (int x=1; x<16; x++) { dat_ein >> hexfeld[x-1][y-1].nhoehe >> hexfeld[x-1][y-1].nboden; hexfeld[x-1][y-1].nNachbar0.ny = y - 1; hexfeld[x-1][y-1].nNachbar3.ny = y + 1; hexfeld[x-1][y-1].nNachbar0.nx = x ; hexfeld[x-1][y-1].nNachbar3.nx = x ; hexfeld[x-1][y-1].nNachbar1.nx = x +1; hexfeld[x-1][y-1].nNachbar2.nx = x +1; hexfeld[x-1][y-1].nNachbar4.nx = x -1; hexfeld[x-1][y-1].nNachbar5.nx = x -1; if (x%2 == false) { hexfeld[x-1][y-1].nNachbar1.ny = y ; hexfeld[x-1][y-1].nNachbar2.ny = y +1; hexfeld[x-1][y-1].nNachbar5.ny = y ; hexfeld[x-1][y-1].nNachbar4.ny = y +1; } else { hexfeld[x-1][y-1].nNachbar1.ny = y -1 ; hexfeld[x-1][y-1].nNachbar2.ny = y ; hexfeld[x-1][y-1].nNachbar5.ny = y -1 ; hexfeld[x-1][y-1].nNachbar4.ny = y ; } } } dat_ein.close(); };
int main() { lese_datei(); cout << get_hoehe(6,6)<< endl << get_boden(6,6) << endl << hexfeld[6][6].nNachbar0.nx << hexfeld[6][6].nNachbar0.ny << endl; cout << hexfeld[6][6].nNachbar1.nx << hexfeld[6][6].nNachbar1.ny << endl; cout << hexfeld[6][6].nNachbar2.nx << hexfeld[6][6].nNachbar2.ny << endl; cout << hexfeld[6][6].nNachbar3.nx << hexfeld[6][6].nNachbar3.ny << endl; cout << hexfeld[6][6].nNachbar4.nx << hexfeld[6][6].nNachbar4.ny << endl; cout << hexfeld[6][6].nNachbar5.nx << hexfeld[6][6].nNachbar5.ny << endl; cin.get(); return 0; }
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
Dieser Beitrag wurde am 21.04.2009 um 05:54 von Thereptil bearbeitet.
|
|
Profil || Suche
|
001
21.04.2009, 08:06
Thereptil
|
wenn ich das auskommentiere beendet sich die Console ohne Fehler
/* if (x%2 == false) { hexfeld[x-1][y-1].nNachbar1.ny = y ; hexfeld[x-1][y-1].nNachbar2.ny = y +1; hexfeld[x-1][y-1].nNachbar5.ny = y ; hexfeld[x-1][y-1].nNachbar4.ny = y +1; } else { hexfeld[x-1][y-1].nNachbar1.ny = y -1 ; hexfeld[x-1][y-1].nNachbar2.ny = y ; hexfeld[x-1][y-1].nNachbar5.ny = y -1 ; hexfeld[x-1][y-1].nNachbar4.ny = y ; }*/
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
Dieser Beitrag wurde am 21.04.2009 um 08:06 von Thereptil bearbeitet.
|
|
Profil || Suche
|
002
21.04.2009, 08:16
hausi
|
Ich hab dir mal die relevanten Code-Zeilen zusammenkopiert:
const int breide = 14; const int hoehe = 16; // ... shex hexfeld[breide][hoehe]; // ... for (int y=1; y<18; y++) // -> y = 1..17 for (int x=1; x<16; x++) hexfeld[x-1][y-1]; // -> y = 0..16 -> Fehler Des weiteren: - Loops durch arrays macht man nicht 1..len, sondern so: for (int x = 0; x < length; x++) - x % 2 == false ist nicht wirklich leserlich und in vielen Sprachen nicht erlaubt
--
|
|
Profil || Suche
|
003
21.04.2009, 08:21
Adrian_Broher
Admin
|
Wieso beginnen die zaehler bei 1? Wieso benutzt du magic numbers als oberes limit, obwohl konstanten fuer die hoehe und breite definiert sind? Der modulooperator gibt hier ein int zurueck, wieso wird nicht gegen 0 abgeglichen? nNachbar<Nummer> ist nichtssagend. nx, ny und Dateiname werden nirgendwo genutzt.
--
There is nothing wrong with high standards. It's your problem that you don't meet them. If you think it's simple, then you have misunderstood the problem. When a customer says "nothing has changed", assume they're lying.
|
|
Profil || Suche
|
004
21.04.2009, 09:08
Thereptil
|
danke für das lesen mal ein paar mehr Infos das ganz soll nur die Koordinaten der Nachbarfelder eines Hexagones in ein Struktur schreiben
hab mal zwei screens gemacht um zu zeigen wie das bei mir aussieht

und wenn ich auf enter drücke (cin.get)

nx,ny und Dateiname hab ich mal rausgelöscht ist für was anderes kommt ja noch mehr ;-)
hexfeld müsste doch von [0...14][0...16] gehen also 15x17 Felder oder ist das falsch?
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
|
|
Profil || Suche
|
005
21.04.2009, 09:12
Adrian_Broher
Admin
|
#003 war eine auflistung von maengeln.
--
There is nothing wrong with high standards. It's your problem that you don't meet them. If you think it's simple, then you have misunderstood the problem. When a customer says "nothing has changed", assume they're lying.
|
|
Profil || Suche
|
006
21.04.2009, 09:13
theDon
|
hexfeld hat 14x16 Felder, [0,13]x[0,15].
--
\o tanz den naziprau! o/
And more than ever, I hope to never fall, Where enough is not the same it was before
|
|
Profil || Suche
|
007
21.04.2009, 09:18
Thereptil
|
supi ich glaub der fehler wahr das zweimal
string Dateiname einmal am Anfang und einmal in der Funktion währe das möglich ?
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
|
|
Profil || Suche
|
008
21.04.2009, 09:34
Thereptil
|
theDon postete hexfeld hat 14x16 Felder, [0,13]x[0,15]. verwirrt mich jetzt nicht ganz aber ein Array in c++ geht doch immer von 0..MAX und hat MAX + 1 Felder also
hexfeld[14]16]
ist ein Array mit 15 spalten und 17 reihen (wenn man mit 1 zu zählen beginnt ;D)
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
Dieser Beitrag wurde am 21.04.2009 um 09:36 von Thereptil bearbeitet.
|
|
Profil || Suche
|
009
21.04.2009, 10:25
hausi
|
Nein. http://tutorial.schornboeck.net/arrays.htm Das ist afaik nur in VB so.
--
Dieser Beitrag wurde am 21.04.2009 um 10:26 von hausi bearbeitet.
|
|
Profil || Suche
|
010
21.04.2009, 11:12
Thereptil
|
testen
--
------------------------------- http://reptil-city.myminicity.com -------------------------------
Dieser Beitrag wurde am 21.04.2009 um 11:34 von Thereptil bearbeitet.
|
|
Profil || Suche
|