Wörteer aus dateiStrings sortieren,array,



  • Ich lese Wortweise aus einer Datei Wörter ein, bearbeite sie, und speichere sie in einem Array!

    Dann sollen diese sortiert werden:
    [cpp]#pragma argsused
    int main(int argc, char* argv[])
    {
    string wort;
    ifstream datei("test.txt");
    string array[50];
    int i=0;
    bool sortiert;
    while(!datei.eof())
    {
    datei>>wort;
    cout<<wort<<endl;
    for(int k=0;k<wort.length();k++)
    {
    if(wort[k]==')'||wort[k]=='(')
    wort.erase(k,k);
    }
    array[i]=wort;
    i++;
    cout<<wort<<endl;
    }
    do
    {
    for(int j=0;j<i;j++)
    {
    sortiert=true;
    if(array[j]>array[j+1])
    {
    sortiert=false;
    if((array[j+1])==" "){}
    else
    {
    string temp=array[j];
    array[j]=array[j+1];
    array[j+1]=temp;
    }
    }
    }
    }
    while(!sortiert);
    cout<<endl;
    cout<<"Sortierte Arrayausgabe"<<endl;
    cout<<"----------------------"<<endl;
    for(int j=0;j<=i;j++)
    cout<<array[j]<<endl;
    getch();
    return 0;
    }
    [cpp]

    Textdatei:
    hallo d(as ist ein text

    Ausgabe:

    das
    ein
    hallo
    
    ist 
    text
    

    Wieso ist da ein leerzeichen?



  • SORRY FÜR DOPPELPOST:

    Noch mal mit Codetags

    #pragma argsused
    int main(int argc, char* argv[])
    {
    string wort;
    ifstream datei("test.txt");
    string array[50];
    int i=0;
    bool sortiert;
    while(!datei.eof())
    {
    datei>>wort;
    cout<<wort<<endl;
    for(int k=0;k<wort.length();k++)
    {
    if(wort[k]==')'||wort[k]=='(')
    wort.erase(k,k);
    }
    array[i]=wort;
    i++;
    cout<<wort<<endl;
    }
    do
    {
    for(int j=0;j<i;j++)
    {
    sortiert=true;
    if(array[j]>array[j+1])
    {
    sortiert=false;
    if((array[j+1])==" "){}
    else
    {
    string temp=array[j];
    array[j]=array[j+1];
    array[j+1]=temp;
    }
    }
    }
    }
    while(!sortiert);
    cout<<endl;
    cout<<"Sortierte Arrayausgabe"<<endl;
    cout<<"----------------------"<<endl;
    for(int j=0;j<=i;j++)
    cout<<array[j]<<endl;
    getch();
    return 0;
    }
    

    Textdatei:

    hallo d(as ist ein text
    

    Ausgabe:
    Code:
    das
    ein
    hallo

    ist
    text
    Code:
    das
    ein
    hallo

    ist
    text
    Code:
    das
    ein
    hallo

    ist
    text



  • Da schleicht sich ne Leerstelle ein. Laut meinen Debugger sieht das Array hinterher so aus:

    "das"
    "ein"
    "hallo"
    ""
    "ist"
    "text"
    

    Warum hab ich beim überfliegen jetzt nicht gefunden.

    /edit:
    Nochmal kurz drüber geguckt und den Fehler gefunden.
    Der Knackpunkt liegt genau hier:

    do{
       for(int j=0;j<i;j++){
        sortiert=true;
        if(array[j]>array[j+1]){
         sortiert=false;
    	 if((array[j+1])!=" "){
          string temp=array[j];
          array[j]=array[j+1];
          array[j+1]=temp;
         }
        }
       }
      }while(!sortiert);
    

    Und zwar in der ersten If-Anweisung. Wenn j = i ist, dann zeigt j+1 auf einen leeren String, der ja garnicht mit sortiert werden soll.
    Ergo:

    do{
       for(int j=0;j<i-1;j++){
        sortiert=true;
        if(array[j]>array[j+1]){
         sortiert=false;
    	 if((array[j+1])!=" "){
          string temp=array[j];
          array[j]=array[j+1];
          array[j+1]=temp;
         }
        }
       }
      }while(!sortiert);
    


  • schade das ich mich net so auskenne (weil im ALphabet gibbed kein Leerzeichen) da, aber wo müsste ein leerzeichen liegen?
    zwischen h und i?

    wieso sortiert er es net raus?

    denn ansonsten kann ich erstmal gröberes nicht erkennen



  • Achja, und es sind so ein paar Denkfehler im Code, würde das aufm Papier oder für die Faulen, im Debugger durchgehen.


Anmelden zum Antworten