469,626 Members | 1,615 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

cin and cin.getline()

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!!
Jul 23 '05 #1
5 5901
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') ;

Jul 23 '05 #2

"Martijn Mulder" <i@m> ha scritto nel messaggio
news:42***********************@news.wanadoo.nl...
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?
Jul 23 '05 #3
Aleander wrote:
"Martijn Mulder" <i@m> ha scritto nel messaggio
news:42***********************@news.wanadoo.nl...
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') ;
Jul 23 '05 #4

"Aleander" <aleander[NOSPAM]@email.it> wrote in message news:uR*********************@twister2.libero.it...

"Martijn Mulder" <i@m> ha scritto nel messaggio
news:42***********************@news.wanadoo.nl...
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

Jul 23 '05 #5

"Alex Vinokur" <al****@big-foot.com> wrote in message news:d0**********@news.wplus.net...
[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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vikram | last post: by
5 posts views Thread by vknid | last post: by
1 post views Thread by ma740988 | last post: by
10 posts views Thread by Skywise | last post: by
14 posts views Thread by KL | last post: by
18 posts views Thread by Amadeus W. M. | last post: by
5 posts views Thread by allspamgoeshere3 | last post: by
33 posts views Thread by Chen shuSheng | last post: by
3 posts views Thread by JackC | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.