cin and cin.getline()

Discussion in 'C++' started by Aleander, Mar 6, 2005.

  1. Aleander

    Aleander Guest

    First of all, greatings from Italy to everyone, and sorry for my bad
    english!!

    I have this problem with the instruction cin.getline that follow a simple
    cin instruction:

    Example:

    #include <iostream>

    typedef char String20[20];
    using namespace std;
    int main(){

    int times;
    String20 name;
    cout << "How many times do you want to repet the loop?";

    cin >> times; // this "cin" make the problem!!

    cout << endl;
    cout << "Insert your Name";
    cin.getline(name, 20);
    /* At this point the program don't stand by the input
    but it jumps to next instruction. Why ??
    What's the right way to resolve the problem?
    */

    system("PAUSE");

    return 0;
    }

    Thanks a lot!!
     
    Aleander, Mar 6, 2005
    #1
    1. Advertising

  2. Aleander wrote:
    > First of all, greatings from Italy to everyone, and sorry
    > for my bad english!!
    >
    > I have this problem with the instruction cin.getline that
    > follow a simple cin instruction:
    >
    > Example:
    >
    > #include <iostream>
    >
    > typedef char String20[20];
    > using namespace std;
    > int main(){
    >
    > int times;
    > String20 name;
    > cout << "How many times do you want to repet the loop?";
    >
    > cin >> times; // this "cin" make the problem!!
    >
    > cout << endl;
    > cout << "Insert your Name";
    > cin.getline(name, 20);
    > /* At this point the program don't stand by the input
    > but it jumps to next instruction. Why ??
    > What's the right way to resolve the problem?
    > */
    >
    > system("PAUSE");
    >
    > return 0;
    > }
    >
    > Thanks a lot!!



    You must #include<ios> and add this ugly line right after the first use of cin:

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

    or, since you have 'using namespace std;', you can add this

    cin.ignore(numeric_limits<streamsize>::max(),'\n');
     
    Martijn Mulder, Mar 6, 2005
    #2
    1. Advertising

  3. Aleander

    Aleander Guest

    "Martijn Mulder" <i@m> ha scritto nel messaggio
    news:422ae27b$0$99034$...
    > Aleander wrote:
    >> I have this problem with the instruction cin.getline that
    >> follow a simple cin instruction:

    >
    > You must #include<ios> and add this ugly line right after the first use of
    > cin:
    >
    >
    > cin.ignore(numeric_limits<streamsize>::max(),'\n');
    >


    Can you explain me this, too?
     
    Aleander, Mar 6, 2005
    #3
  4. Aleander wrote:
    > "Martijn Mulder" <i@m> ha scritto nel messaggio
    > news:422ae27b$0$99034$...
    >> Aleander wrote:
    >>> I have this problem with the instruction cin.getline
    >>> that follow a simple cin instruction:

    >>
    >> You must #include<ios> and add this ugly line right
    >> after the first use of cin:
    >>
    >>
    >> cin.ignore(numeric_limits<streamsize>::max(),'\n');
    >>

    >
    > Can you explain me this, too?



    cin is the standard input stream object
    ignore is a method from cin. It reads characters from cin and throws them away
    numeric_limits is a template to retrieve implementation-defined values from a
    type
    streamsize is a type that represents the size of I/O buffers
    max is a method from numeric_limits. In this case, it returns the size of an I/O
    buffer
    '\n' means the same as endl, and is used here to indicate that cin must ignore
    the newline too

    numeric_limits<streamsize>::max() can be output like this:

    cout<<numeric_limits<streamsize>::max();

    its size on my machine is 2147483647 (wow)

    It is advisable to also include a call to cin.clear() in your code, to reset the
    input flags:

    cin>>temp;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
     
    Martijn Mulder, Mar 6, 2005
    #4
  5. Aleander

    Alex Vinokur Guest

    "Aleander" <aleander[NOSPAM]@email.it> wrote in message news:uRDWd.64619$...
    >
    > "Martijn Mulder" <i@m> ha scritto nel messaggio
    > news:422ae27b$0$99034$...
    > > Aleander wrote:
    > >> I have this problem with the instruction cin.getline that
    > >> follow a simple cin instruction:

    > >
    > > You must #include<ios> and add this ugly line right after the first use of
    > > cin:
    > >
    > >
    > > cin.ignore(numeric_limits<streamsize>::max(),'\n');
    > >

    >
    > Can you explain me this, too?
    >
    >


    ------ foo.cpp ------
    #include <iostream>
    #include <ios>
    using namespace std;

    typedef char String20[20];

    int main()
    {
    int times;
    String20 name;

    cout << "How many times do you want to repeat the loop?: ";

    // -----------------
    // The user should enter: some integer value + "Enter",
    if (!(cin >> times))
    {
    cerr << "Illegal times" << endl;
    return 1;
    }
    // ..................
    // Sample-1
    // For instance, the user enters: "357" + "Enter".
    // 'times' is of int type.
    // So, in this case cin reads until non-digit in the input, i.e., cin read '3', '5', '7'.
    // After that cin sees "Enter" (i.e., '\n') and stops.
    // Important! Now input stream contains '\n'.
    // ..................
    // Sample-2
    // For instance, the user enters: "169abc" + "Enter".
    // 'times' is of int type.
    // So, in this case cin reads until non-digit in the input, i.e., cin read '1', '6', '9'.
    // After that cin sees 'a' and stops.
    // Important! Now input stream contains 'abc\n'
    // -----------------

    -------------------
    // http://www.cppreference.com/cppio/ignore.html
    // istream &ignore( streamsize num=1, int delim=EOF );
    // The ignore() function is used with input streams.
    // It reads and throws away characters until num characters
    // have been read (where num defaults to 1) or until
    // the character delim is read (where delim defaults to EOF).
    -------------------
    cin.ignore(numeric_limits<streamsize>::max(),'\n');

    cout << endl;
    cout << "Insert your Name: ";
    cin.getline(name, 20);

    // ---------------------------
    // Situation analysis
    //
    // -------------------------
    // Sample-1: "357" + "Enter"
    // -------------------------
    // Case-1. Without "cin.ignore(numeric_limits<streamsize>::max(),'\n');" above.
    // Our input stream already contains '\n'.
    // So, cin.getline() reads 0 characters into name and that why it is not waiting for our entering name.
    // In this case name = "". (Note. '\n' is not put into name.)
    // Case-2. With "cin.ignore(numeric_limits<streamsize>::max(),'\n');" above.
    // '\n' after "357" has been ignored.
    // Our input stream is empty.
    // So, cin.getline() is waiting for entering name.
    //
    // -------------------------
    // Sample-1: "169abc" + "Enter"
    // -------------------------
    // Case-1. Without "cin.ignore(numeric_limits<streamsize>::max(),'\n');" above.
    // Our input stream already contains 'abc\n'.
    // So, cin.getline() reads 'abc' into name and that why it is not waiting for our entering name.
    // In this case name = "abc". (Note. '\n' is not put into name.)
    // Case-2. With "cin.ignore(numeric_limits<streamsize>::max(),'\n');" above.
    // 'abc\n' after "169" has been ignored.
    // Our input stream is empty.
    // So, cin.getline() is waiting for entering name.
    // ---------------------------

    cout << "name = " << name << endl;

    return 0;
    }
    ---------------------


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Mar 6, 2005
    #5
  6. Aleander

    Alex Vinokur Guest

    Alex Vinokur, Mar 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Poof
    Replies:
    1
    Views:
    385
  2. Fred H
    Replies:
    6
    Views:
    7,407
    Kevin Goodsell
    Jan 23, 2004
  3. Simon Gibson

    cin.getline problems in case

    Simon Gibson, Feb 26, 2004, in forum: C++
    Replies:
    6
    Views:
    589
    Simon Gibson
    Feb 26, 2004
  4. bgeneto
    Replies:
    5
    Views:
    9,458
  5. Devendra_Vidhale

    newbie problem with cin and getline

    Devendra_Vidhale, Jul 17, 2007, in forum: C++
    Replies:
    20
    Views:
    1,153
    James Kanze
    Jul 19, 2007
Loading...

Share This Page