Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cin and cin.getline()

Reply
Thread Tools

cin and cin.getline()

 
 
Aleander
Guest
Posts: n/a
 
      03-06-2005
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!!


 
Reply With Quote
 
 
 
 
Martijn Mulder
Guest
Posts: n/a
 
      03-06-2005
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::streamsiz e>::max(),'\n');

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

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



 
Reply With Quote
 
 
 
 
Aleander
Guest
Posts: n/a
 
      03-06-2005

"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?


 
Reply With Quote
 
Martijn Mulder
Guest
Posts: n/a
 
      03-06-2005
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') ;


 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      03-06-2005

"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





 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      03-06-2005

"Alex Vinokur" <> wrote in message news:d0f917$4ar$...
[snip]
> // -------------------------
> // Sample-1: "169abc" + "Enter"

// Should be
// Sample-2: "169abc" + "Enter"
> // -------------------------



--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cin.get() and cin.eof() ends up in an endless loop Fernando C++ 4 11-16-2011 07:19 PM
std::cin.ignore() and std::cin.clear() Chris Mantoulidis C++ 5 01-06-2004 11:08 PM
gets and cin Bartek C++ 8 12-11-2003 05:18 PM
cin and cin.clear() problem TaiwanNoWhere C++ 8 10-17-2003 05:53 PM
cin and spaces Samuele Armondi C++ 2 06-23-2003 10:28 PM



Advertisments