hilfe bei struct programm
-
huhu leute, so siehts aus:
#include <iostream> #include <conio> #include <string> using namespace std; int j=0; //////////////////////////////////////////////////////////////////////////////////////////////////// struct adresse { string Vorname; string Name; string Strasse; string Postleit; string Ort; }addy[j]; //////////////////////////////////////////////////////////////////////////////////////////////////// void eingabe() { for(j=0;j<=j;j++) { j+1; cout << "Geben Sie den Vornamen ein: "; cin >> addy[j].Vorname; cout << "Geben Sie den Namen ein: "; cin >> addy[j].Name; cout << "Geben Sie die Strasse an: "; cin >> addy[j].Strasse; cout << "Geben Sie die Postleitzahl an: "; cin >> addy[j].Postleit; cout << "Geben Sie den Ort an: "; cin >> addy[j].Ort; clrscr(); } } //////////////////////////////////////////////////////////////////////////////////////////////////// void ausgabe() { for(j=0;j<=j;j++) { cout << endl; j+1; cout << addy[j].Vorname <<" " ; cout << addy[j].Name <<" " ; cout << addy[j].Strasse <<" " ; cout << addy[j].Postleit <<" "; cout << addy[j].Ort << " "; } } //////////////////////////////////////////////////////////////////////////////////////////////////// int main() { eingabe(); ausgabe(); getchar(); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////
Der meckert bei addy [j] rum, ich will eine eingabe machen von j und diese zahl soll halt [j] annehmen, doch das klappt nich so, hoffe ihr könnt mir helfen.
-
@henke11,
du meinst sich bei diesem Teil oder:
struct adresse { string Vorname; string Name; string Strasse; string Postleit; string Ort; } addy[j];
j wird erst zu Laufzeit gesetzt.
Du musst in diesem Fall einen konstanten Wert
verwenden. Folglich bleibt dir nichts anderes
übrig als neue Strukturen bei Bedarf zu erstellen.typedef struct structADDY { ... ... } ADDY; int iAnzahl = 20; /* Feste anzahl von Adressen erstellen */ ADDY *pAddy = malloc( iAnzahl *sizeof( ADDY ) ); ... /* Wenn du nun mehr brauchst */ iAnzahl++; pAddy = realloc( pAddy, iAnzahl *sizeof( ADDY ) );
Bye Peter.
-
Es gilt immer: j == j
mfg
v R
-
malloc, realloc und dann auch noch free vergessen? Brrr - Finger weg. Wenn du ein variables Array brauchst, nimm std::vector.