Problem mit schleifen Ausgabe
-
ja also abstürze gabs noch keine
und ja könnte ich schon tun nur durch was? vorschlag dann setz ich das mal um.
Aber jetzt hab ich nen anders problem, wo wir schon bei abstürzen sind wenn ich einen wert über 8 eingeb dann stürzt das program ab sobald ich die letzte zahl eingegeben hab.
ja stimmt beginnen ja mit 0
hmmm
danke
-
Soll ich es etwas größer schreiben? Dein Versuch eines VLA ist unsinnig (und wie du gesehen hast, ist es nur eine Frage der Zeit, bis es kracht).
In C++ verwendet man für solche Aufgaben vector<>en:
vector<int>array; ... x = panzahl * 3; cout << "\nJetzt bitte " << x << " " << polydata << " eingeben"; array.resize(x); for (i = 0; i < x ; i++) { cout << "\n"; cin >> array[i]; }
-
aha
ok ich werds mal so probiern und das ganze Programm mal überarbeiten.
Hast schon recht
ja auf jedenfall ists ja so versuchs programm und auf jedenfall hat es viele verbesserungen nötig.
mal sehn wie ich jetzt weiter machen werde aber dein tipp werde ich jetzt berücksichtigen.
-
Hi,
spontan fällt mir bei den "if-else-if"'s auf, dass sie seeeeehr fehleranfällig sind. Ist zwar eigentlich nur eine Frage der Formatierung, aber mehr als nur "Geschmackssache".
Mal Fragen:
1.) Weißt Du, dass nur EINE Anweisung hinter einem if ("if(bedingung) anweisung;") dem if zugeordnet wird ?
Also:if (BINARY == 1) ASCII = ("ASCII"); // wird nur ausgeführt, wenn BINARY==1 ist something = 2; // wird IMMER ausgeführt else // Compiler-error: "misplaced else" something = 3;Das hast Du zwar (noch) nicht falsch gemacht, aber Dein Code ist seeeeehr anfällig für diesen Fehler.
2.) kennst Du die switch-case-Konstruktion ?
Also:switch(BINARY) { case 1: ASCII = ("ASCII"); break; case 2: ASCII = ("BINARY"); break; default: // sonst return 0; }Beides könnte Dir weiterhelfen.
Gruß,
Simon2.
-
Und bei dem Aussehen der if-Kaskaden könnte man das auch noch kompakter lösen (aber das fällt schon unter "fortgeschrittene Programmiertechniken"):
//aus: if (polyZ == 1) polydata = ("POINTS"); else if (polyZ == 2) polydata = ("VERTICES"); else if (polyZ == 3) polydata = ("LINES"); else if (polyZ == 4) polydata = ("POLYGONS"); else if (polyZ == 5) polydata = ("TRIANGLE_STRIPS"); else if (polyZ > 5) return 0; //wird: string polytypes[]={"POINTS","VERTICES","LINES","TRIANGLE_STRIPS"}; if(polyZ<=5) polydata = polytypes[polyZ-1]; else return 0;
-
ja ich kenn die switch-case-Konstruktion
und da ich den code komplett überarbeiten werde werde ich das mit einbinden
und auch dir danke für die hinweise und hilfe
-
hab jetzt bisserl rumprobiert und mach grad des mit den vektoren
das klapt auch gut Danke nochmal guter tipp auch das andere
#include<iostream> #include <fstream> #include <string> #include<vector> using namespace std; string space = " "; int main() { vector<int> Array; cout << "Bitte Werte eingeben\n"; int ArrayWert; do{ cout << "Wert (999 = Ende der Eingabe):"; cin >> ArrayWert; if(ArrayWert != 999) Array.push_back(ArrayWert); } while(ArrayWert != 999); cout << "Es wurden die folgenden Werte eingegeben:\n"; for(int i = 0; i < Array.size(); i++) cout << i << ". Wert : " << Array[i] << endl; int PointAnzahl; ofstream myFile; myFile.open ("C:/Test.vtk"); if (myFile) { for (int l=0; l< PointAnzahl; l++) { for (int j = 0; j<=3; j++) { myFile << Array[l*3+j] << space; } myFile << endl; } myFile.close(); system("pause"); } }würd mich über genau so gute hilfe wie vorher freun
-
Du hast vergessen, PointAnzahl zu initialisieren.
-
hupps stimmt danke
mai mai bin ich doof
aber find ich voll gut das forum großes lob
Und dir CStoll danke für die ganze hilfe

PS:
War auch der Falsche Code ich poste ihn oben nochmal
Kommt davon wenn man im compiler und in Notepad rumarbeitet und dann des falsche kopiert
-
also hier mal ne abgeänderte version (nicht fertig nicht komplett und so)
#include <fstream> #include <iostream> #include <string> #include<vector> using namespace std; //Strings string space = " "; string enter = "\n"; string vtkversion = "# vtk DataFile Version"; string null = ".0"; string ASCIIBINARY = ""; string dataset = "DATASET"; string EingabeName; string Dataseteingabe; string polydata; string Pointart; string pfad; string ProjektName; string VTK = ".vtk"; int version; int main ( ) { //Variablen int i; int array [i]; int version; int PointAnzahl; int Dateieingabe; int PolydataZahl; int ASCIIBINARYzahl; //Name der Datei unter dem sie dann abgespeichert wird cout << " \n Bitte Name der Datei eingeben \n"; cout << " "; cin >> EingabeName; //Pfad angeben wo datei dann gespeichert wird cout << " \n Bitte Pfad eingeben \n"; cout << " "; cin >> pfad; //Wahl der VTK Version 1, 2 oder 3. cout << " \n Bitte VTK Versionsnummer eingeben \n"; cout << " "; cin >> version; //Name in der Datei z.B. kurze beschreibung (ohne Leerzeichen)! cout << " \n Bitte Projekt Namen der Datei eingeben \n"; cout << " "; cin >> ProjektName; // Wahl zwischen ASCII und BINARY cout << " \n Bitte zwischen ASCII | BINARY wählen \n"; cout << " \n 1. ASCII "; cout << " \n 2. BINARY "; cout << "\n\n "; cin >> ASCIIBINARYzahl; switch(ASCIIBINARYzahl) { case 1: ASCIIBINARY = ("ASCII"); break; case 2: ASCIIBINARY = ("BINARY"); break; default: // sonst return 0; } //DATASET type wählen cout << " \n Bitte Datei Art wählen "; cout << " \n 1. STRUCTURED_POINTS "; cout << " \n 2. STRUCTURED_GRID "; cout << " \n 3. UNSTRUCTURED_GRID "; cout << " \n 4. POLYDATA "; cout << " \n 5. RECTILINEAR_GRID "; cout << " \n 6. FIELD "; cout << "\n\n "; cin >> Dateieingabe; /*Dataset attributes. The number of data items n of each type must match the number of points or cells in the dataset. (If type is FIELD, point and cell data should be omitted. */ string Datasetattributes[]={"STRUCTURED_POINTS","STRUCTURED_GRID","UNSTRUCTURED_GRID","POLYDATA","RECTILINEAR_GRID","FIELD"}; if(Dateieingabe<=6) Dataseteingabe = Datasetattributes[Dateieingabe-1]; else return 0; if (Dateieingabe == 4) cout << " \n Bitte POLYDATA wählen "; cout << " \n 1. POINTS "; cout << " \n 2. VERTICES "; cout << " \n 3. LINES "; cout << " \n 4. POLYGONS "; cout << " \n 5. TRIANGLE_STRIPS "; cout << "\n\n "; cin >> PolydataZahl; string polytypes[]={"POINTS","VERTICES","LINES","TRIANGLE_STRIPS"}; if(PolydataZahl<=5) polydata = polytypes[PolydataZahl-1]; else return 0; cout << " \n Bitte Anzahl der Points eingeben \n"; cout << " \n "; cin >> PointAnzahl; cout << " \n Bitte Pointart eingeben \n"; cout << "\n "; cin >> Pointart; int x; x = PointAnzahl * 3; cout << "\nJetzt bitte " << x << " " << polydata << " eingeben"; for (i = 0; i <= x ; i++) { cout << "\n"; cin >> array[i]; } string pfad2 = pfad + EingabeName + VTK; pfad2.c_str(); //steht dann für char { ofstream myFile; myFile.open (pfad2.c_str()); if (myFile) { myFile << vtkversion << space << version << null << enter << ProjektName << enter << ASCIIBINARY << enter << dataset << space << Dataseteingabe << enter << polydata << space << PointAnzahl << space << Pointart << endl; for (i=0; i< PointAnzahl; i++) { for (int j = 1; j<=3; j++) { myFile << array[i*3+j] << space; } myFile << endl; } myFile.close(); for (i = 1; i <= x ; i++) { cout << i; } } system("pause"); } }Und das beste es funktioniert ^^ ohne fehler
Hier ne ausgabe als beispiel
# vtk DataFile Version 2.0
Test
ASCII
DATASET POLYDATA
POINTS 8 float
<---- Ok das noch falsch aber des weis ich ja wos liegt
0 0 1
0 0 1
1 0 0
1 0 0
0 1 1
0 1 1
1 1 0
1 1 0_______________________________________________
So da wollt ich jetzt noch fragen da ich ja schon so viele gut tolle tipps gekriegt hab noch mehr ideen? wie die vektoren und die switch und so?
gerne auch "fortgeschrittene Programmiertechniken" komm ich schon mit klaralso würd mich drüber freun
-
Erstmal solltest du die gesamten globalen Strings entsorgen (und die Hilfskonstanten wie space und Co. direkt als String-Literale einsetzen, wo sie gebraucht werden). Außerdem kannst du die Variablendefinitionen auch in der Funktion verteilen (bis zu der Stelle, wo sie wirklich gebraucht werden).
PS: Und da du die Ausgabedaten sowieso von oben nach unten zusammenstellst, kannst du die Daten auch direkt in die Datei schreiben, wenn du sie ermittelt hast

-
hmmm das ne idee ja
wenn ich sie immer dann rein schreib wenn ichs hab dann hab ich auch kein problem mehr mit den abständen und so weil ich einfach eingabe mach (in file schreiben und jeh nach dem leer oder enter) das gut
und variablen erst definiern wenn ich sie brauch?
also zum ersten mal benutze
gut gut mal schaunfrisch ans werk
Danke führ diese schon sehr früher Hilfestellung
-
Habs jetzt abgeändert
#include <fstream> #include <iostream> #include <string> #include<vector> using namespace std; int main ( ) { //Name der Datei unter dem sie dann abgespeichert wird string EingabeName; cout << " \n Bitte Name der Datei eingeben \n"; cout << " "; cin >> EingabeName; //Pfad angeben wo datei dann gespeichert wird string pfad; cout << " \n Bitte Pfad eingeben \n"; cout << " "; cin >> pfad; string VTK = ".vtk"; string pfad2 = pfad + EingabeName + VTK; pfad2.c_str(); //steht dann für char ofstream myFile; myFile.open (pfad2.c_str()); if (myFile) { string vtkversion = "# vtk DataFile Version"; string null = ".0"; string space = " "; string enter = "\n"; //Wahl der VTK Version 1, 2 oder 3. int version; cout << " \n Bitte VTK Versionsnummer eingeben \n"; cout << " "; cin >> version; myFile << vtkversion << space << version << null << enter; //Name in der Datei z.B. kurze beschreibung (ohne Leerzeichen)! string ProjektName; cout << " \n Bitte Projekt Namen der Datei eingeben \n"; cout << " "; cin >> ProjektName; myFile << ProjektName << enter; // Wahl zwischen ASCII und BINARY string ASCIIBINARY = ""; int ASCIIBINARYzahl; cout << " \n Bitte zwischen ASCII | BINARY wählen \n"; cout << " \n 1. ASCII "; cout << " \n 2. BINARY "; cout << "\n\n "; cin >> ASCIIBINARYzahl; switch(ASCIIBINARYzahl) { case 1: ASCIIBINARY = ("ASCII"); break; case 2: ASCIIBINARY = ("BINARY"); break; default: // sonst return 0; } myFile << ASCIIBINARY << enter; string dataset = "DATASET"; //DATASET type wählen int Dateieingabe; cout << " \n Bitte Datei Art wählen "; cout << " \n 1. STRUCTURED_POINTS "; cout << " \n 2. STRUCTURED_GRID "; cout << " \n 3. UNSTRUCTURED_GRID "; cout << " \n 4. POLYDATA "; cout << " \n 5. RECTILINEAR_GRID "; cout << " \n 6. FIELD "; cout << "\n\n "; cin >> Dateieingabe; /*Dataset attributes. The number of data items n of each type must match the number of points or cells in the dataset. (If type is FIELD, point and cell data should be omitted. */ string Dataseteingabe; string Datasetattributes[]={"STRUCTURED_POINTS","STRUCTURED_GRID","UNSTRUCTURED_GRID","POLYDATA","RECTILINEAR_GRID","FIELD"}; if(Dateieingabe<=6) Dataseteingabe = Datasetattributes[Dateieingabe-1]; else return 0; myFile << dataset << space << Dataseteingabe << enter; int PolydataZahl; if (Dateieingabe == 4) cout << " \n Bitte POLYDATA wählen "; cout << " \n 1. POINTS "; cout << " \n 2. VERTICES "; cout << " \n 3. LINES "; cout << " \n 4. POLYGONS "; cout << " \n 5. TRIANGLE_STRIPS "; cout << "\n\n "; cin >> PolydataZahl; string polydata; string polytypes[]={"POINTS","VERTICES","LINES","TRIANGLE_STRIPS"}; if(PolydataZahl<=5) polydata = polytypes[PolydataZahl-1]; else return 0; int PointAnzahl; string Pointart; cout << " \n Bitte Anzahl der Points eingeben \n"; cout << " \n "; cin >> PointAnzahl; cout << " \n Bitte Pointart eingeben \n"; cout << "\n "; cin >> Pointart; myFile << polydata << space << PointAnzahl << space << Pointart << enter; /* int x; int i; int array[i]; x = PointAnzahl * 3; cout << "\nJetzt bitte " << x << " " << polydata << " eingeben"; for (i = 0; i <= x ; i++) { cout << "\n"; cin >> array[i]; } for (i=0; i< PointAnzahl; i++) { for (int j = 1; j<=3; j++) { myFile << array[i*3+j] << space; } myFile << endl; } */ myFile.close(); } system("pause"); }der letzte auskommentierte teil der mit der eingabe von array[i] der funktioniert nicht wenns bei der ausführung angelangt istdann stürtzt das programm ab (aber ohne hinweis auf fehler)
nur weis ich nicht warum
-
Da ist ja schon wieder deine VLA-Konstruktion (die immer noch genauso falsch ist wie am Anfang). Und eine Variable anzulegen, nur um sie dann EINMAL auszugeben, halte ich für Verschwendung:
if (myFile) { //Wahl der VTK Version 1, 2 oder 3. int version; cout << " \n Bitte VTK Versionsnummer eingeben \n"; cout << " "; cin >> version; myFile << "# vtk DataFile Version " << version << ".0\n"; ... //DATASET type wählen int Dateieingabe; cout << " \n Bitte Datei Art wählen "; cout << " \n 1. STRUCTURED_POINTS "; cout << " \n 2. STRUCTURED_GRID "; cout << " \n 3. UNSTRUCTURED_GRID "; cout << " \n 4. POLYDATA "; cout << " \n 5. RECTILINEAR_GRID "; cout << " \n 6. FIELD "; cout << "\n\n "; cin >> Dateieingabe; string Datasetattributes[]={"STRUCTURED_POINTS","STRUCTURED_GRID","UNSTRUCTURED_GRID","POLYDATA","RECTILINEAR_GRID","FIELD"}; if(Dateieingabe<=6) myFile<<"DATASET "<<Datasetattributes[Dateieingabe-1]<<"\n"; else return 0; ...(btw, nur eine Kleinigkeit: Gewöhn dir einen vernünftigen Einrückungsstil an, das erleichtert die Lesbarkeit deines Codes)
-
ja ok ich muss mir halt noch einen guten style aneignen naja hoffe das es bisdahin noch geht
und das erste Problem werde ich noch berichtigen aber ich meine das das problem ja hier liegt
int x; int i; int array[i]; x = PointAnzahl * 3; cout << "\nJetzt bitte " << x << " " << polydata << " eingeben"; for (i = 0; i <= x ; i++) { cout << "\n"; cin >> array[i]; } for (i=0; i< PointAnzahl; i++) { for (int j = 1; j<=3; j++) { myFile << array[i*3+j] << space; } myFile << endl; }weil ja bis zu der stelle geht alles
-
SORRY
ICH NEHMS ZURÜCK
ich verbessers
-
GT schrieb:
j...
int i; int array[i];...
Gib's zu: Du willst CStoll in den Wahnsinn treiben....

Ach ja: Als Mitglied kannst Du Deine Posts auch editieren und musst nicht immer neue aufmachen...
Gruß,
Simon2.
-
So jetzt aber
#include <fstream> #include <iostream> #include <string> #include<vector> using namespace std; int main ( ) { //Name der Datei unter dem sie dann abgespeichert wird string EingabeName; cout << " \n Bitte Name der Datei eingeben \n"; cout << " "; cin >> EingabeName; //Pfad angeben wo datei dann gespeichert wird string pfad; cout << " \n Bitte Pfad eingeben \n"; cout << " "; cin >> pfad; string VTK = ".vtk"; string pfad2 = pfad + EingabeName + VTK; pfad2.c_str(); //steht dann für char ofstream myFile; myFile.open (pfad2.c_str()); if (myFile) { string vtkversion = "# vtk DataFile Version"; string null = ".0"; string space = " "; string enter = "\n"; //Wahl der VTK Version 1, 2 oder 3. int version; cout << " \n Bitte VTK Versionsnummer eingeben \n"; cout << " "; cin >> version; myFile << vtkversion << space << version << null << enter; //Name in der Datei z.B. kurze beschreibung (ohne Leerzeichen)! string ProjektName; cout << " \n Bitte Projekt Namen der Datei eingeben \n"; cout << " "; cin >> ProjektName; myFile << ProjektName << enter; // Wahl zwischen ASCII und BINARY string ASCIIBINARY = ""; int ASCIIBINARYzahl; cout << " \n Bitte zwischen ASCII | BINARY wählen \n"; cout << " \n 1. ASCII "; cout << " \n 2. BINARY "; cout << "\n\n "; cin >> ASCIIBINARYzahl; switch(ASCIIBINARYzahl) { case 1: ASCIIBINARY = ("ASCII"); break; case 2: ASCIIBINARY = ("BINARY"); break; default: // sonst return 0; } myFile << ASCIIBINARY << enter; string dataset = "DATASET"; //DATASET type wählen int Dateieingabe; cout << " \n Bitte Datei Art wählen "; cout << " \n 1. STRUCTURED_POINTS "; cout << " \n 2. STRUCTURED_GRID "; cout << " \n 3. UNSTRUCTURED_GRID "; cout << " \n 4. POLYDATA "; cout << " \n 5. RECTILINEAR_GRID "; cout << " \n 6. FIELD "; cout << "\n\n "; cin >> Dateieingabe; /*Dataset attributes. The number of data items n of each type must match the number of points or cells in the dataset. (If type is FIELD, point and cell data should be omitted. */ string Dataseteingabe; string Datasetattributes[]={"STRUCTURED_POINTS","STRUCTURED_GRID","UNSTRUCTURED_GRID","POLYDATA","RECTILINEAR_GRID","FIELD"}; if(Dateieingabe<=6) Dataseteingabe = Datasetattributes[Dateieingabe-1]; else return 0; myFile << dataset << space << Dataseteingabe << enter; int PolydataZahl; if (Dateieingabe == 4) cout << " \n Bitte POLYDATA wählen "; cout << " \n 1. POINTS "; cout << " \n 2. VERTICES "; cout << " \n 3. LINES "; cout << " \n 4. POLYGONS "; cout << " \n 5. TRIANGLE_STRIPS "; cout << "\n\n "; cin >> PolydataZahl; string polydata; string polytypes[]={"POINTS","VERTICES","LINES","TRIANGLE_STRIPS"}; if(PolydataZahl<=5) polydata = polytypes[PolydataZahl-1]; else return 0; int PointAnzahl; string Pointart; cout << " \n Bitte Anzahl der Points eingeben \n"; cout << " \n "; cin >> PointAnzahl; cout << " \n Bitte Pointart eingeben \n"; cout << "\n "; cin >> Pointart; myFile << polydata << space << PointAnzahl << space << Pointart << enter; vector<int> Array; cout << "Bitte Werte eingeben\n"; int ArrayWert; do{ cout << "Wert (999 = Ende der Eingabe):"; cin >> ArrayWert; if(ArrayWert != 999) Array.push_back(ArrayWert); } while(ArrayWert != 999); for (int l=0; l< PointAnzahl; l++) { for (int j = 1; j<=3; j++) { myFile << Array[l*3+j] << space; } myFile << endl; } myFile.close(); } system("pause"); }Aber jetzt wenn ich über einen wert komme dann krieg ich wieder das raus
# vtk DataFile Version 2.0
TEST
ASCII
DATASET POLYDATA
POINTS 8 float
1 1 1
1 1 1
1 1 0
0 0 0
0 0 0
0 0 0
0 1 1
0 0 1702130553aber bei geringen werten nicht
-
ja also nicht wirklich ich bin ihm ja für seine hilfe sehr dankbar
ich habs nur voll verplant das ich des ja schon wie bei dem beispiel jetzt geändert hatte
aber jetzt wie eins drüber das problem hmmmm
-
Die Ausgabeschleife erwartet, daß (mindestens) 3*PointAnzahl Werte in deinem vector stehen - wenn du dich also bei der Eingabe verzählst, erhältst du Müll-Daten (oder einen Zugriffsfehler). Und außerdem hast du da noch einen eins-daneben-Fehler in der Ausgabe - ändere die innere Schleife mal auf 'for(j=0;j<3;++j)'.