cin.getline() problem

M

Markus

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
 
R

Rolf Magnus

Markus said:
Hi,

I want to get an integer from the user.
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.
 
K

Kai-Uwe Bux

Markus said:
Hi,

I want to get an integer from the user.
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
 
D

David Harmon

On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"
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.
 
J

Jim Langston

David Harmon said:
On 1 Sep 2006 02:42:54 -0700 in comp.lang.c++, "Markus"


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

Markus

Rolf said:
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
 
J

Jim Langston

Markus said:
Thank you for your answers.

Another solution seems to be:

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:

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.
 
S

ShaoVie

Markus said:
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
 
M

Markus

Jim said:
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
 
J

Jerry Coffin

[ ... ]
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;
}
 
S

ShaoVie

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 !
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top