cin and cin.getline()

A

Aleander

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!!
 
M

Martijn Mulder

Aleander said:
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');
 
A

Aleander

Martijn Mulder said:
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?
 
M

Martijn Mulder

Aleander said:
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');
 
A

Alex Vinokur

Aleander said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top