CIN FRAGE



  • hi ...

    habe folgendes schwerwiegendes Problem mit cin bei meinem Code ...

    drückt der Benutzer bei der Eingabeaufforderung den Buchstaben "d" wird die methode checkInputD() aufgerufen ... in dieser dann die methode definition_labyrinth() ...

    und da beginnen dann unsere Probleme ...

    der code wie er hier abgebildet ist funktioniert ... es werden in der methode definition_labyrinth() 2 uint32 Variablen angelegt und denen der Wert 10 zugewiesen ... dann wird eine char matrix mit 10 Spalten und 10 Zeilen erzeugt ... danach wird diese wieder gelöscht ...

    wir wollen aber das der benutzer werte eingeben kann ... daher die auskommentierte Zeile ...

    //cin >> row >> line;

    der Benutzer soll also z.B. folgendes eingeben ...

    5 3(enter)

    diese werte werden doch in die variablen row und line gespeichert ... das sagt uns zumindestens unser cout ...

    kurz gesagt ...
    lässt man das cin >> row >> line; also im code kommt es zu irgendeinem problem und das ganze haut nimmer hin ...

    auch die erstellte Matrix wird laut memusage nicht mehr gelöscht ...

    bitte um dringende Hilfe

    #include <iostream> 
    #include <cstdlib> 
    #include <cstdio> 
    #include <cstring> 
    
    #define MAX_LINE_LENGTH 128 
    #define ILLEGAL_ARGUMENTS -1 
    
    using std::cout; 
    using std::cin; 
    using std::cerr; 
    using std::endl; 
    
    typedef unsigned int uint32; 
    
    class InputHandler 
    { 
    public: 
      InputHandler(); 
      ~InputHandler(); 
    
      void dispatch(char *input_line); 
      void checkInputD(char *input); 
      void run(); 
      void definition_labyrinth(); 
      void quit_labyrinth(); 
    }; 
    
    int main() 
    { 
      InputHandler inputhandler_test; 
      inputhandler_test.run(); 
          system("PAUSE"); 
          return 0; 
    } 
    
    InputHandler::InputHandler() 
    { 
    } 
    //------------------------------------------------------------ 
    
    InputHandler::~InputHandler() 
    { 
    } 
    
    //------------------------------------------------------------ 
    
    // The main run-loop that accepts the keyboard input 
    
    void InputHandler::run() 
    { 
      char input_line[MAX_LINE_LENGTH + 1]; 
    
       for(;;) 
      { 
        cout << "labyrinth> "; 
        cin.clear(); 
        cin.getline(input_line, MAX_LINE_LENGTH); 
        dispatch(input_line); 
      } 
    } 
    //------------------------------------------------------------ 
    
    // dispatches the keyboard input 
    
    void InputHandler::dispatch(char *input_line) 
    { 
      char *input = 0; 
      input = new char[strlen(input_line + 1)]; 
      strcpy(input,input_line); 
    
      char command[2]; 
      strncpy(command, input, 1); 
    
      switch(command[0]) 
      { 
        case 'd': 
            checkInputD(input); 
            delete[] input; 
            break; 
    
        case 'q': 
            delete[] input; 
            quit_labyrinth(); 
            break; 
    
        default: 
           cout << "error: unrecognized command" << endl; 
           delete[] input; 
           break; 
      } 
    } 
    //------------------------------------------------------------ 
    void InputHandler::checkInputD(char *input) 
    { 
    if (strlen(input) > 1) 
        { 
          cerr << "error: unrecognized command" << endl; 
        } 
        definition_labyrinth(); 
    } 
    //------------------------------------------------------------ 
    
    void InputHandler::quit_labyrinth() 
    { 
      exit(1); 
    } 
    
    //------------------------------------------------------------ 
    
    void InputHandler::definition_labyrinth() 
    { 
    
      char **matrix_; 
      uint32 row = 10; 
      uint32 line = 10; 
    
      //cin >> row >> line; 
    
      cout << "Spalte: " << row << endl; 
      cout << "Zeile: " << line << endl; 
    
       matrix_ = new char*[row]; 
      for (uint32 j = 0; j < row; j++) 
         matrix_[j] = new char[line]; 
    
      for (uint32 j = 0; j < row; j++) 
        delete[] matrix_[j]; 
      delete[] matrix_; 
    }
    


  • also ich habe es unter mingw 3.2 und msvc 7.1 getestet und bei beidem liefs einwandfrei, wenn ich folgende zeile aus der mf run ()

    cin.clear ();
    

    in

    cin.sync ();
    

    aendere. vielleicht sagst du mal welchen compiler du benutzt. ansonsten kann ich dir im moment nicht weiterhelfen

    was auch immer


Anmelden zum Antworten