Problem with cin.getline()

B

bintom

I have a program to read a string (using cin.getline()) followed by a
number, 3 times in a loop. The program compiles properly. However, the
runtime behaviour of the program is not what is expected. A sample
output is also given:


int main()
{ char name[3][21], dummy[21];
int i, mark[3];

for(i=0; i<3; i++)
{ cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter mark: "; cin >> mark;
// cin.getline(dummy, 21);
}

for(i=0; i<3; i++)
cout << name << '\t' << mark << '\n';
}


A typical run would produce the following results:

Enter name: ABC
Enter mark: 56
Enter name: Enter mark: 78
Enter name: Enter mark: 27

ABC 56
78
27

After much heart-burn, I was able to get the expected output, provided
a dummy string is accepted (see the commented line in the program).
Can somebody tell me why this problem arises?

Thanks in advance,
Bintom
 
A

Alf P. Steinbach

* bintom, on 29.05.2010 10:42:
I have a program to read a string (using cin.getline()) followed by a
number, 3 times in a loop. The program compiles properly. However, the
runtime behaviour of the program is not what is expected. A sample
output is also given:


int main()
{ char name[3][21], dummy[21];
int i, mark[3];

for(i=0; i<3; i++)
{ cout<< "Enter name: "; cin.getline(name, 21);
cout<< "Enter mark: "; cin>> mark;
// cin.getline(dummy, 21);
}

for(i=0; i<3; i++)
cout<< name<< '\t'<< mark<< '\n';
}


A typical run would produce the following results:

Enter name: ABC
Enter mark: 56
Enter name: Enter mark: 78
Enter name: Enter mark: 27

ABC 56
78
27

After much heart-burn, I was able to get the expected output, provided
a dummy string is accepted (see the commented line in the program).
Can somebody tell me why this problem arises?


The

cin >> mark

does not consume the terminating newline character in the input.

This newline character is then interpreted as an empty line of input for the
next name input.

'getline' is one way to consume the rest of the line of input. Another way is to
use 'ignore'.

By the way, you can simplify the handling of names, and also make that code more
robust, by using std::string instead of raw character arrays.


Cheers & hth.,

- Alf
 
B

bintom

I am working with Turbo C++ and so must work with C-strings. Does that
mean that the dummy getline() is the only way to overcome the problem?

Thnx,
Bintom
 
K

Kaar

I am working with Turbo C++ and so must work with C-strings. Does that
mean that the dummy getline() is the only way to overcome the problem?

Thnx,
Bintom

Not at all. But your problem is that your semantics are not
consistent. Why are you using getline() once and the >> operator
another time. Settle on one of them.

Doing this should work

cin.width(21);
cin >> name;

cout << "Enter mark: ";
cin >> mark;

and it can work without the .width, but then if the user enters
something bigger than 21 characters, the output will be unpredictable
and it is always smart to pass array length along to operators when
you are working with characters.

Always study your functions before you use them. Documentation is
necessary to understand the exact semantics of each command you are
using. Get used to it, it is quite a useful habit!

http://www.cplusplus.com/reference/iostream/istream/getline/
http://www.cplusplus.com/reference/iostream/istream/operator>>/
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top