473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cin.getline() problem

Hi,

I want to get an integer from the user.
cin >a;
If the user inserts a character instead of an int the program goes
crazy. So I tried something like this:
>int get_value() {
int a;
char b[6];
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}
This program works pretty well. But if I put this code in my main
program the b-array does not get a value.
When I change my code the array does not get a value.
>int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}
What is the problem with the cin > before the cin.getline() ?

Best Regards

Markus

Sep 1 '06 #1
11 3240
Markus wrote:
Hi,

I want to get an integer from the user.
>cin >a;
If the user inserts a character instead of an int the program goes
crazy.
Then you probably forgot to check for the stream state after reading.
So I tried something like this:
>>int get_value() {
int a;
char b[6];
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

This program works pretty well. But if I put this code in my main
program the b-array does not get a value.
When I change my code the array does not get a value.
>>int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

What is the problem with the cin > before the cin.getline() ?
When you type in the value for x, you press return aftewards. The resulting
\n stays in the buffer and gets picked up by your getline call.

Sep 1 '06 #2
Markus wrote:
Hi,

I want to get an integer from the user.
>cin >a;
If the user inserts a character instead of an int the program goes
crazy.
[snip]

You should test for success:

if ( cin >a ) {
// input could be parsed as an int.
} else {
// input was not an int. take error recovery measures.
}

Best

Kai-Uwe Bux
Sep 1 '06 #3
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
>What is the problem with the cin > before the cin.getline() ?
It leaves '\n' in the stream, and getline sees it as an empty line.
Sep 1 '06 #4

"David Harmon" <so****@netcom.comwrote in message
news:45***************@news.west.earthlink.net...
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
>>What is the problem with the cin > before the cin.getline() ?

It leaves '\n' in the stream, and getline sees it as an empty line.
The solution being, of course, just to do another getline and ignore the
results.

Although you'll probably want to check the state of the stream after the int
input, because if the user typed a character instead of a number it'll be
bad and you'll have to clear it.
Sep 1 '06 #5
Jim Langston wrote:
>
"David Harmon" <so****@netcom.comwrote in message
news:45***************@news.west.earthlink.net...
>On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
>>>What is the problem with the cin > before the cin.getline() ?

It leaves '\n' in the stream, and getline sees it as an empty line.

The solution being, of course, just to do another getline and ignore the
results.
Or just use the ignore() member function.

Sep 1 '06 #6

Rolf Magnus wrote:
Jim Langston wrote:

"David Harmon" <so****@netcom.comwrote in message
news:45***************@news.west.earthlink.net...
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
What is the problem with the cin > before the cin.getline() ?

It leaves '\n' in the stream, and getline sees it as an empty line.
The solution being, of course, just to do another getline and ignore the
results.

Or just use the ignore() member function.
Thank you for your answers.

Another solution seems to be:
cin.seekg(0, ios::end);
cin.clear();
But now I have got another problem. If I insert something like 23c3
just 23 is stored in a . Everything that follows a char is ignored
without making any problems. To check each char in the array wheter it
is an integer or not seems to be a little bit to costly.
This source code has exactly the same effect:
int function() {
int a;
if ( cin >a ) {
if (a < 1000000) {
cout << "a = " << a << endl;
} else {
cout << "Wrong input" << endl;
}
} else {
cout << "Wrong input" << endl;
a = 0;
}
cin.seekg(0, ios::end);
cin.clear();
return 0;
}
Is there an elegant way to check that the whole input is not equal with
a if someone inserts something like 23c3 ?

Regards

Markus

Sep 1 '06 #7
"Markus" <de*************@web.dewrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
Rolf Magnus wrote:
>Jim Langston wrote:
>
"David Harmon" <so****@netcom.comwrote in message
news:45***************@news.west.earthlink.net...
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
What is the problem with the cin > before the cin.getline()
?

It leaves '\n' in the stream, and getline sees it as an empty line.

The solution being, of course, just to do another getline and ignore
the
results.

Or just use the ignore() member function.

Thank you for your answers.

Another solution seems to be:
>cin.seekg(0, ios::end);
cin.clear();

But now I have got another problem. If I insert something like 23c3
just 23 is stored in a . Everything that follows a char is ignored
without making any problems. To check each char in the array wheter it
is an integer or not seems to be a little bit to costly.
This source code has exactly the same effect:
>int function() {
int a;
if ( cin >a ) {
if (a < 1000000) {
cout << "a = " << a << endl;
} else {
cout << "Wrong input" << endl;
}
} else {
cout << "Wrong input" << endl;
a = 0;
}
cin.seekg(0, ios::end);
cin.clear();
return 0;
}

Is there an elegant way to check that the whole input is not equal with
a if someone inserts something like 23c3 ?

Regards

Markus
Now you're getting into tricky waters. When you >int cin will return the
number portion. The "c3" should remain waiting to be read. I know that
there's some way to see if anything is waiting, but not sure what it is.
Someone with more knowledge of iostream will probably have the answer.
Sep 2 '06 #8

Markus wrote:
Hi,

I want to get an integer from the user.
cin >a;
If the user inserts a character instead of an int the program goes
crazy. So I tried something like this:
int get_value() {
int a;
char b[6];
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

This program works pretty well. But if I put this code in my main
program the b-array does not get a value.
When I change my code the array does not get a value.
int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cout << "Insert a value " << endl;
cin.ignore (1, '\n'); // it can ignore one
'\n' that you last typed during input "x" // my
english is poor ,sorry
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}

What is the problem with the cin > before the cin.getline() ?

Best Regards

Markus
Sep 3 '06 #9

Jim Langston wrote:
"Markus" <de*************@web.dewrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

Rolf Magnus wrote:
Jim Langston wrote:


"David Harmon" <so****@netcom.comwrote in message
news:45***************@news.west.earthlink.net...
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
<de*************@web.dewrote,
What is the problem with the cin > before the cin.getline()
?

It leaves '\n' in the stream, and getline sees it as an empty line.

The solution being, of course, just to do another getline and ignore
the
results.

Or just use the ignore() member function.
Thank you for your answers.

Another solution seems to be:
cin.seekg(0, ios::end);
cin.clear();
But now I have got another problem. If I insert something like 23c3
just 23 is stored in a . Everything that follows a char is ignored
without making any problems. To check each char in the array wheter it
is an integer or not seems to be a little bit to costly.
This source code has exactly the same effect:
int function() {
int a;
if ( cin >a ) {
if (a < 1000000) {
cout << "a = " << a << endl;
} else {
cout << "Wrong input" << endl;
}
} else {
cout << "Wrong input" << endl;
a = 0;
}
cin.seekg(0, ios::end);
cin.clear();
return 0;
}
Is there an elegant way to check that the whole input is not equal with
a if someone inserts something like 23c3 ?

Regards

Markus

Now you're getting into tricky waters. When you >int cin will return the
number portion. The "c3" should remain waiting to be read. I know that
there's some way to see if anything is waiting, but not sure what it is.
Someone with more knowledge of iostream will probably have the answer.
I had a resembling idea yesterday. If there is still the "c3" in the
stream I know that the input was wrong. If there is a '\n' in the
stream the input was correct.
I am not sure whether this always works but now my source code goes
something like this (the aim is to get an input from the user that
consists of no more than 6 decimals, if it is too long or if a char is
inserted an error message will be displayed and the user has to insert
this value again):

int check_int_input() {
int value = 0;
int decimals = 6;
char check_istream = '\n';

while (1) {
cin.seekg(0, ios::end);
cin.clear();
cin >value;
if (cin.good()) {
if ((cin.get(check_istream)) && (check_istream != '\n')) {
cout << "ERROR. Wrong input. Try again" << endl;
cin.seekg(0, ios::end);
cin.clear();
continue;
}
if (value >= (pow(10, decimals))) {
cout << "ERROR. Wrong input. Try again." << endl;
cin.seekg(0, ios::end);
cin.clear();
continue;
}
// Correct input
break;
} else {
cout << "ERROR. Wrong input. Try again." << endl;
cin.seekg(0, ios::end);
cin.clear();
continue;
}
}
return value;
}

Maybe someone has got a similiar problem.
There is only one problem ... cin.getline() vanished.

Regards

Markus

Sep 3 '06 #10
In article <11**********************@m79g2000cwm.googlegroups .com>,
de*************@web.de says...

[ ... ]
I had a resembling idea yesterday. If there is still the "c3" in the
stream I know that the input was wrong. If there is a '\n' in the
stream the input was correct.
One common way of dealing with this is to start by reading a line into a
string, then attempt to convert the string to an int (or whatever). If
that consumes the entire string, it succeeded, but if there's something
left, it failed.

int getval(int max) {
static string prompts[] = {
"Please enter a value",
"ERROR. Wrong input. Try again"
};

bool error = false;

int value = max+1;
bool extra_junk;

do {
cout << prompts[error] << ": " << flush;
error=true;
string temp;
getline(cin, temp);
stringstream t(temp);
t >value;
extra_junk=!t.eof();
} while (extra_junk || value max);
return value;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 3 '06 #11
int get_value() {
int a;
int x;
char b[6];
cout << "Insert a value" << endl;
cin >x;
cin.ignore (1, '\n');
cout << "Insert a value " << endl;
cin.getline(b, 7, '\n');
a = atoi(b);
cout << "a: " << a << endl;
return 0;
}
why not try it!!

I think that your problem is forget the '\n', then when you run "--
cin.getline(b, 7, '\n'); --", computer
will read the last '\n' first , and read senven chars of you typing!
"getline" function read '\n' first and it stop reading, because it
ending by '\n' char.
"ignore " function , you can look up the MSDN, my english is poor
,so i can't s describe clearly!

and i don't know whether rigtht of my posted; sorry,
i hope that i can help you !

Sep 4 '06 #12

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...
4
by: Joe | last post by:
Hello - I wrote a program that uses ifstream to open an ASCII file and getline() to read in the lines. The problem is when I try to open the same file again later in the code. I used close()...
1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
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...
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...
7
by: Chris | last post by:
Running into a problem on Windows. This code std::string randomStuff; std::getline(std::cin, randomStuff); works on unix, but on windows, it requires the user to hit the enter key *twice*...
6
by: ankit.kumar.agarwal | last post by:
I am facing a problem with getline I am reading a text file with a getline function the lines can have '|' as separator. everything works OK but in case if i have 2 delimitors in file '234||...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.