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
    2 hours 46 min ago
  • Links working
    1 day 2 hours ago
  • Thanks... I may be able to
    1 day 8 hours ago
  • 3306 by default
    1 day 18 hours ago
  • Is this the right place to
    1 day 19 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 3 hours ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 5 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 0 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

cin.get (arrayname, size)

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

Here is another form of the cin use. This form, cin.get(arrayname, size) is similar to cin.getline(arrayname, size) discussed previously. This form cin.get(arrayname, size) reads an entire line from the keyboard terminated by the 'ENTER' key BUT leaves the newline '\n' character in the input queue. Why it does this I'm not sure and why have such a function which for all intents and purposes is the same as the cin.getline(...) method, I'm not sure either.

Some things to be aware of

If there is an existing newline in the input buffer prior to calling cin.get(arrayname,20);, this form will not accept it, remember it leaves the newline character in the input buffer. The point here is that an error state is created in the cin object; the failbit flag is set to true. No further cin operations are permitted whilst the error flag is set to true. In order to remove the error flag this command cin.clear(); is required. It resets the cin error state back to false.

The above is not applicable if the input buffer contains normal characters ending with a newline character prior to calling the cin.get(arrayname, 20);. What occurs in this instance is that the errant characters or characters already in the input buffer as a result of a previous cin operation are assigned or allocated to the character array arrayname. The newline character is left in the input buffer.

Another situation arises whereby the user types in more characters than is specified by the size parameter of the cin function. No error state is recorded and the character array arrayname only receives the number of specified characters. The remaining characters are left in the input buffer.

The following code snippet should explain the various workings of this topic. The code, as is, will create an error state, simply uncomment the line after cin.get(line, 20);.

// File : testcin3.cpp
// Author : Steven Taylor
// Date : 23 Apr 2008
#include <iostream>
using namespace std;
int main()
{
    char getdata;
    cout << "Enter one character: ";
    cin >> getdata;
    cin.get();   // clear out very next character (newline)
   
    char line[20];
    cout << "\nEnter a line of text 20 characters max: ";
    cin.get(line,20);
    cout << "\nLine of text entered : " << line;
    //cin.get();      // flush out the newline character
   
    char line2[20];
    cout << "\nEnter another line of text 20 characters max: ";
    cin.get(line2,20);
    cout << "\nSecond line of text entered : " << line2;
   
    cout << "\n\nNow at the end of the program" << endl;
    cout << "First character of line[20]  : " << line[0] << " character code is : " << (int)line[0] << endl;
    cout << "First character of line2[20] : " << line2[0] << " character code is : " << (int)line2[0]<< endl;
   
    if (cin.eof())
        cout << "\n cin.eof() == true";
    else
        cout << "\n cin.eof() == false";
    if (cin.fail())
        cout << "\n cin.fail() == true";
    else
        cout << "\n cin.fail() == false";
   
    // exit routine
    cin.clear(); // only use this if there is potential for an error state.
    while (cin.get() != '\n')
        continue;
    cin.get();
    return 0;
}

As an aside, if there is no error state, issuing the command cin.clear(); has no side effects, that is, errors are not reported simply because no error exists to start with. It's harmless.

Play with the program. You will note that if you input more than a single character for the first input, that it adversely affects the following cin methods. I'll deal with flushing the input stream/buffer/queue in a separate article but in the interim the exit routine should be a clue as to how to flush the input buffer.

‹ cin.getline (arrayname, size); up Short Programs ›
  • Printer-friendly version
  • Login or register to post comments
  • 234 reads

 Subscribe in a reader

free hit counter


RoopleTheme