Problem mit Programm, Schleife wird nicht durchlaufen



  • Wieso hast Du denn Deine Klasse "Word" über Bord geworfen? Die steht doch extra in der Aufgabe drin, und kann Dir die Arbeit mit den C-Strings ordentlich vereinfachen.

    So koennte das Interface z.B. aussehen:
    word.h

    class Word
    {
    public:
        Word(char* w = NULL);   //Std-Ctor
        Word(const Word& word); //Copy-Ctor
        ~Word(); //Dtor
        char charAt(size_t idx) const; //char at ;)
        size_t length() const;  //Wortlaenge
        Word& operator=(const Word& w); //zuweisen
        bool operator<(const Word& w) const; //Vergleich
        bool operator>(const Word& w) const; //Vergleich
        bool operator==(const Word& w) const; //Vergleich
        operator const char*() const; //cast
        const char* findSubStr(const char* str, size_t startIdx = 0) const; //Substring
    private:
        void assign(const char* cStr);
        char* word;
    };
    


  • Danke schonmal,

    habe es mittlerweile geschafft, hänge aber im Moment an der letzten Funktion ein Wort zu bearbeiten

    void WordChart::editWord() 
    {
      int index;
      char input[256];
      for (int i=0; i<wordList.size(); i++)
      {
    	cout << "(" << i << ") "; wordList[i].printWord();
      }
      cin.clear();
      cin.sync();
      cout << "\nWelches wort?";
      cin >> index;
      cout << "\nBitte das neue Wort eingeben: ";
      cin.clear();
      cin.sync();								//previous inputs deprecated
      cin.getline(input, 256, '\n');
      wordList[index] = Word(input);
    }
    

    Die Auswahl klappt, die neue Eingabe klappt auch, doch wenn ich die dann abschicke, passiert einfach gar nichts mehr. Was mache ich falsch?



  • Hat sich erledigt, konnte das Problem wie folgt lösen

    void WordChart::editWord() 
    {
      int index;
      for (int i=0; i<wordList.size(); i++)
      {
    	cout << "(" << i << ") "; wordList[i].printWord();
      }
      char userInput;
      Word currentWord;
      cout << "Bitte den Index eingeben: ";
      cin.clear(); cin.sync();  
      cin >> index;
      cin.clear(); cin.sync();
      cout << "Bitte neues Wort eingeben: ";
      do 
      {
        userInput = cin.get(); 
    	if (cin.fail())  
    	{
          cin.clear(); cin.sync();
    	  return;
    	}
    	switch (userInput) 
    	{
          case ' ':
    	  case '\n':
    	  case '\t': wordList[index] = currentWord; currentWord.clear(); break; 
    	  default: currentWord.appendChar(userInput); break; 
    	}
      } while (userInput != '\n');
      lettStat.update(wordList);
    }
    

Anmelden zum Antworten