Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Basic cin operations


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
    • Basic cin operations
      • cin >> getdata;
      • cin.getline (arrayname, size);
      • cin.get (arrayname, size)
    • Short Programs
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Got it solved This page here
    1 hour 38 min ago
  • Links working
    1 day 1 hour ago
  • Thanks... I may be able to
    1 day 7 hours ago
  • 3306 by default
    1 day 17 hours ago
  • Is this the right place to
    1 day 17 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 2 hours ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 4 days ago

New forum topics

  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
  • index.php?
more

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

cin.getline (arrayname, size);

  • View
  • Revisions
Submitted by Steve on Wed, 23 Apr, 2008 - 00:29
  • C++
  • cin
  • get()
  • getline()

So you want to read an entire line from the keyboard and not be bothered with cin >> getdata; only reading the first word. Then this form of the cin object is for you.

cin.getline(arrayname, size);

This form accepts an entire line entered from the keyboard terminated by the 'ENTER' key. The newline character created by the 'ENTER' key is replaced by a null '\0' character (which is the terminating character required for character arrays). As you can guess from the code snippet, the line of text is ultimately assigned to arrayname variable, a character array, and only receives the number of characters specified by the second parameter, size.

If the number of characters entered is greater than the number specified by the second parameter size the left-over characters remain in the input queue/stream. Remember, the next cin operation will retrieve either some or all of these characters depending upon the type of cin operation that's applied. If the number of characters is less than the parameter size then the input queue is effectively flushed and will not be a problem for the next cin operation.

cin.getline(arrayname,size) also has a third parameter which isn't used here. Well, to be correct, it is used here but not explicitly expressed. The third parameter is a delimiter character and by default the newline '\n' character is the delimiter if not specified as a parameter. It's possible to speicify a character other than the newline '\n' character as the delimiter.

If you remember from Part 1 of this article there is a possibility that this form may appear not to work. That is, it may appear that the character array is receiving a blank line or incorrect characters. If cin.getline(arrayname, size) is preceded by cin >> getdata code then at the very least a newline '\n' character is left in the input queue. Our getline(...) operation receives characters until a newline is encountered and in this example, it just happens to be the very first character. Is this a problem, well not really, but not understanding it, is. We need to make sure that prior to getline(...) operation occurring that the input queue has been flushed of errant characters.

In order to flush out errant characters or at the very least a newline '\n' character issue this code cin.get(); immediately after the cin >> getdata; line. We'll discuss this form in a separate topic but for now just accept that it clears or flushes the very next character from the input queue.

To better explain the whole process copy and paste the following short code snippet and check for yourself.

#include <iostream>
using namespace std;
int main()
{
    char getdata;
    cout << "Enter one character: ";
    cin >> getdata;
    //cin.get();
    char line[20];
    cout << "\nEnter a line of text 20 characters max: ";
    cin.getline(line,20);
    cout << "\nLine of text entered : " << line;
    cout << "\n\nNow at the end of the program";
    cout << "\nFirst character of line[20] " << (int)line[0] << endl;
    // exit routine
    while (cin.get() != '\n')
        continue;
    cin.get();
    return 0;
}

Firstly, the exit routine will be explained in it's own topic and just accept that it's sole purpose is to pause the program so as you can check your results (usually a problem for Windows users executing the program directly from a compiler).

Running the program as is you will never get a chance to enter a line of text. The reason being is that the newline character entered from the cin >> getdata; line is picked up by the cin.getline(line,20) line and assigned to the character array line. You will note that the first character code of the character array line is 0 (null character).

Uncomment the cin.get() line and the program will behave nearly as intended. The cin.get() will flush the newline '\n' character and thus will allow the cin.getline(line,20) to do it's thing. If you type ABC DEF the cout line towards the end of the program will indicate the character code of the first character of the character array line. In this case being 65.

With the cin.get() line uncommented, now run the program again. This time at the first prompt, rather than enter a single character enter a series of characters ie ABCDEF. You will note that 'A' is correctly assigned to it's variable getdata but you never have a chance to enter a line of text and yet the character array line contains BCDEF. You will also note that the character code of the first character of the array is 66 (code for 'B').

At this point we are therefore still left with the problem of a user entering more than a single character for getdata which adversely affects cin.getline(...). I'll address that soon but in the interim the exit routine should be a clue as to how to address it.

‹ cin >> getdata; up cin.get (arrayname, size) ›
  • Printer-friendly version
  • Login or register to post comments
  • 247 reads

 Subscribe in a reader

free hit counter


RoopleTheme