reading text from cin fails after reading numbers. - Linux

This is a discussion on reading text from cin fails after reading numbers. - Linux ; Hello, I want to use the library iostream with cin to read from standard input. Look at the followin code: #include using namespace std; int main(){ cout uint number; cin>>number; cout char text[81]; // cin.seekg(0L,std::ios::end); // cin.clear(); cin.getline(text,80); cout return ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: reading text from cin fails after reading numbers.

  1. reading text from cin fails after reading numbers.

    Hello,
    I want to use the library iostream with cin to read from standard input.
    Look at the followin code:
    #include
    using namespace std;
    int main(){
    cout<<"Teste jetzt die Eingabe. Bitte Zahl eingeben: ";
    uint number;
    cin>>number;
    cout<<"Jetzt Text eingeben: ";
    char text[81];
    // cin.seekg(0L,std::ios::end);
    // cin.clear();
    cin.getline(text,80);
    cout<<"\n you have typed in: "< return 0;
    }
    It doesn't work neither with nor without the //-lines. What is wrong?



  2. Re: reading text from cin fails after reading numbers.

    Hi.

    It worked okay for me:

    agw@homeesktop$ foo
    Teste jetzt die Eingabe. Bitte Zahl eingeben: 80
    Jetzt Text eingeben:
    you have typed in: 80

    --
    Art Werschulz (agw STRUDEL comcast.net)
    207 Stoughton Ave Cranford NJ 07016
    (908) 272-1146

  3. Re: reading text from cin fails after reading numbers.

    Art Werschulz schrieb:
    > Hi.
    >
    > It worked okay for me:
    >
    > agw@homeesktop$ foo
    > Teste jetzt die Eingabe. Bitte Zahl eingeben: 80
    > Jetzt Text eingeben:
    > you have typed in: 80
    >


    or may be he wanted to input own text. Then he should eat the eol from
    last input, like:

    // cin.getline(text,80);
    string s;
    char eol;
    std::cin.get(eol);
    getline(cin,s);
    cout<<"\n you have typed in: "<

    Teste jetzt die Eingabe. Bitte Zahl eingeben: 123
    Jetzt Text eingeben: hey you

    you have typed in: 123hey you

  4. Re: reading text from cin fails after reading numbers.

    Uwe Bosse wrote:
    > I want to use the library iostream with cin to read from standard input.


    As far as C++ is concerned, it is not a library but rather a builtin part of
    C++.

    > int main(){
    > cout<<"Teste jetzt die Eingabe. Bitte Zahl eingeben: ";
    > uint number;


    What is 'uint'?

    > cin>>number;
    > cout<<"Jetzt Text eingeben: ";
    > char text[81];
    > cin.getline(text,80);


    No, get rid of such strings.

    #include
    ....
    std::string text;
    getline( std::cin, text);

    > What is wrong?


    You shouldn't mix line-based input with field-based input. Rather, read
    std::cin linewise and afterwards parse it (using e.g. std::stringstream).
    Alternatively, you can ignore spaces, newlines and tabs using std::ws or
    discard input using std::istream's ignore() function, in order to skip the
    newline that is still in the input buffer after extracting the number.

    Uli


+ Reply to Thread