getline() für DataStructure mit Array wird ignoriert - flush funktioniert nicht



  • Servus,
    Ich fang grad erst an n bisschen C++ zu lernen, und hab mich für dieses Tutorial entschieden: http://www.cplusplus.com/doc/tutorial/

    Jetz hab ich mich durch die ersten Seiten gearbeitet und n paar kleine Beispielprogramme geschrieben, bis ich hier hängengeblieben bin: http://www.cplusplus.com/doc/tutorial/structures.html

    An sich versteh ich alles was da steht, und das Problem hat nicht direkt mit structures zu tun, sondern eher mit std::getline() ... glaub ich 🙂

    Hier mein kleines Anfänger Programm (is ziemlich gleich dem Beispiel in dem Tutorial, nur dass ich noch ne Abfrage hinzugefügt hab wieviele "games" gespeichert werden sollen.)

    //Data Structure test - save some stuff in an array
    
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    //Data structure
    struct games {
           string title;
           string dev;
           int year;
           };
    
    //Prototype function
    void printgame (games game);
    
    int main ()
    {
        string inp;
        int g;
        cout << "How many games would you like to save? ";
        cin >> g;
        games gamearray[g];
    
        cout << "Now you will be able to enter the Title,\n";
        cout << "Developer and Release Year of each game.\n";
        for (int m=0; m<g; m++)
        {
            cout << "Enter title: ";
    //Hier is das Stück Code das beim ersten Schleifendurchlauf ignoriert wird...
            getline (cin,gamearray[m].title);
            cout << "Enter Developer: ";
            getline (cin,gamearray[m].dev);
            cout << "Enter Release Year: ";
            getline (cin,inp);
            stringstream(inp) >> gamearray[m].year;
            }
    
        cout << "\nYou have saved the following games:\n";
        for (int i=0; i<g; i++)
        {
            printgame (gamearray[i]);
            }
    
    system("PAUSE");
    return(0);
    }
    
    void printgame (games game)
    {
              cout << "\nTitle: " << game.title << endl;
              cout << "Developer: " << game.dev << endl;
              cout << "Release Year: " << game.year << endl;
              }
    

    Da wo der Comment is, also beim erstem

    getline (cin,gamearray[m].title);
    

    ... da is das Problem.

    Wenn ich das Ding laufen lass, dann ignoriert er dieses getline, schreibt mir hin
    "Enter Title: Enter Developer: "
    und ab da kann ich dann so speichern wie es gedacht war.

    Beim nächsten durchlauf der Schleife funktioniert alles, da nimmt er mir das erste getline() auch, und ich kann den Title speichern.

    Hab hier im Forum rugmesucht, gegoogled und in ner C++ Reference nachgeschaut (http://www.cplusplus.com/reference/iostream/istream/getline.html). Habs mal mit nem

    cout << "Enter Title: " << flush;
    

    probiert, klappt auch net.
    Kann sonst einfach nix finden .. sorry 😞

    Kann mir jemand helfen ?

    edit: achja ... ich benutz Bloodshed Dev-C++ als Compiler
    edit2: die ganzen anderen edits waren schönheitsfehler :p



  • http://www.cppreference.com/cppio/ignore.html

    schreibe hinter der Zeile 24 die ignore( ... ) funktion



  • Hm, muss ich dafür <fstream> einbinden ?
    Und selbst wenn ich das mach, funktionierts nich 😕

    //Data Structure test program - save some games in an array
    
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    using namespace std;
    
    //[...]
    
    int main ()
    {
        string inp;
        int g;
        cout << "How many games would you like to save? ";
        cin >> g;
        ignore();
        games gamearray[g];
    

    Compile-Error: "26 C:\C++\DataStructure-Save games.cpp `ignore' undeclared (first use this function) "

    Trotzdem schonma danke für die Hilfe.

    Jo, ich bin n noob 🙂



  • cin.ignore( ... );



  • Yay ! Danke 🙂


Anmelden zum Antworten