Hello all,

what do you think the answer following question is?

The cin.getline function:

a) reads a fixed number of characters as a string into a character array.

b) reads a line as a string into a character array.

c) reads a string up to the first blank space.

d) reads a character into an array

Answer:

Dani AI

Generated

Short answer: (b). cin.getline is the member that reads a line into a character buffer — it stops at the delimiter (newline by default) or when the buffer limit is reached, and always null-terminates the stored characters. That makes (a) misleading: the numeric argument is a maximum buffer size, not a fixed count that will always be read. ’s reply was correct; ’s choice of (A) is understandable but imprecise. basic_istream::getline reference. (en.cppreference.com)

About the argument list: the correct description is array name (pointer to char) and array size. The member signature is effectively getline(char_type* s, std::streamsize count, char_type delim = '\n'), so you pass the buffer and its capacity. This is why the second multiple-choice (array name and size) is the right idea. (en.cppreference.com)

Don’t confuse cin.getline with the extraction operator >> or with the free std::getline that fills a std::string. operator>> skips leading whitespace and stops at the next whitespace (that behaviour matches option (c)), while std::getline(std::cin, std::string) reads a whole line into a string object — often the safer choice in modern code. Example:

#include <iostream>
#include <string>

std::string line;
std::getline(std::cin, line);
std::cout << line << '\n';

See the std::getline page for details. (en.cppreference.com)

Practical tips: if you mix >> and line-oriented reads, clear the leftover newline first (use std::ws or cin.ignore(...)). Check stream state after cin.getline: if the buffer fills before the delimiter, failbit will be set and gcount() reports how many characters were put in the buffer. Prefer std::getline with std::string to avoid manual buffer-size bugs in new code. (cppreference.patrickfasano.com)

Recommended Answers

All 6 Replies

Answer:
Use Google to find a .

Answer:
Use Google to find a .

I already had but still a bit confused about the questions.
Ok, I think the answer is a), but why not b)?

Choice ( A ) is the most accurate.There are more funtions getline can peform.

I have another question if you dont mind me asking - (last one).

The arguments to the cin.getline function should be

a. format string and the buffer size.

b. character array and the array size. :?:

If (b) was -array name and the array size- it would be accurate(?)
I think is (a) even if I dont quite understand why.

I dont write these questions.
Thanks for the replies...

It should be answer (b)
Here's an example:

#include <iostream.h>
main()
{
   char s[100];
   cin.getline(s,100);
   cout<<s; 
}

So, cin.getline(char_array,max_lenght_of_array); :lol:

It should be answer (b)
Here's an example:

#include <iostream.h>
main()
{
   char s[100];
   cin.getline(s,100);
   cout<<s; 
}

So, cin.getline(char_array,max_lenght_of_array); :lol:

Thanks.
You have solved my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.