473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6047
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.