By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,812 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,812 IT Pros & Developers. It's quick & easy.

cin.getline() problem

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

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

P: n/a
"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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.