ah08
ah08

Reputation: 11

cin.get() works but NOT cin.getline(). What am I doing wrong or misunderstanding about cin.getline()?

I'm a beginning C++ student studying for finals. I wrote a program 2 ways. The first code uses cin.getline() and doesn't work properly. The second code uses cin.get() and cin >> and does everything correctly.

What am I missing? Why is the situation in Example 1 skipping the rest of the input prompts, and then entering useless numbers?

Isn't cin.getline(ARRAYNAME,ARRAYSIZE) supposed to essentially do the job of setw(n), cin.get(ARRAYNAME,ARRAYSIZE) and cin.ignore(int,char)? Doesn't the cin.getline(ARRAYNAME,ARRAYSIZE) work by extracting up to ARRAYSIZE-1 characters, placing them in ARRAYNAME, adding a \0 at the end, and skipping everything beyond that until it reaches \n... by default?

EDIT: To give a little more background, this example comes from earlier in my textbook (Chapters 3 & 4). I wanted to follow along with its progression and refresh my memory on some early, easy-to-forget concepts. I'll be reviewing strings, the string library, and the string class later on (Chapter 10).

Thanks for the help!

-- ah08

P.S. The ISBN number is set to hold ISBN-13 (13 numbers, 4 hyphens).

User Input Book Info - Example 1 (Does NOT Work Properly)

/*
In this version, I use "cin.getline(ARRAYNAME,ARRAYSIZE)",
but when I input a string with a length that's larger than the ARRAYSIZE,
weird things happen.

I include the cin.ignore(int,'\n') as a safety measure...
but is it really necessary?
*/

//BEGIN PROGRAM CODE

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    char date[9];
    char ISBN[18];
    char bookTitle[31];
    int quantity;
    double unitPrice;

    cout << "Please enter the following information.\n";

    // Input Date
    cout << "Date (in MM/DD/YY format): ";
    cin.getline(date,9);

    // Display Date
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << date << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Quantity
    cout << "Quantity of Books: ";
    cin >> quantity;
    cin.ignore(512,'\n');

    // Display Quantity
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << quantity << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input ISBN
    cout << "ISBN (including hyphens): ";
    cin.getline(ISBN,18);

    // Display ISBN
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << ISBN << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Title
    cout << "Book Title: ";
    cin.getline(bookTitle,31);

    // Display Title
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << bookTitle << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Price
    cout << "Unit Price: ";
    cin >> unitPrice;
    cin.ignore(512,'\n');

    // Display Price
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << unitPrice << endl;
    cout << "------------------" << endl;
    cout << endl;

    cout << endl;
    system("pause");
    return 0;
}

//END PROGRAM CODE

//BEGIN PROGRAM OUTPUT

/*
Please enter the following information.
Date (in MM/DD/YY format): 12/03/1970

------------------
*** 12/03/19
------------------

Quantity of Books:
------------------
*** 2000596547
------------------

ISBN (including hyphens):
------------------
***
------------------

Book Title:
------------------
***
------------------

Unit Price:
------------------
*** -1.#QNAN
------------------


Press any key to continue . . .
*/

User Input Book Info - Example 2 (Does Work Properly)

/*
In this version, I use "cin >> setw(ARRAYSIZE) >> ARRAYNAME"
or "cin.get(ARRAYNAME, ARRAYSIZE)" and follow either instance
with a "cin.ignore(int,'\n')", then everything works perfectly.
*/

//BEGIN PROGRAM CODE

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    char date[9];
    char ISBN[18];
    char bookTitle[31];
    int quantity;
    double unitPrice;

    cout << "Please enter the following information.\n";

    // Input Date
    cout << "Date (in MM/DD/YY format): ";
    cin >> setw(9) >> date;
    cin.ignore(512,'\n');

    // Display Date
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << date << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Quantity
    cout << "Quantity of Books: ";
    cin >> quantity;
    cin.ignore(512,'\n');

    // Display Quantity
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << quantity << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input ISBN
    cout << "ISBN (including hyphens): ";
    cin >> setw(18) >> ISBN;
    cin.ignore(512,'\n');

    // Display ISBN
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << ISBN << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Title
    cout << "Book Title: ";
    cin.get(bookTitle,31);
    cin.ignore(512,'\n');

    // Display Title
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << bookTitle << endl;
    cout << "------------------" << endl;
    cout << endl;

    // Input Price
    cout << "Unit Price: ";
    cin >> unitPrice;
    cin.ignore(512,'\n');

    // Display Price
    cout << endl;
    cout << "------------------" << endl;
    cout << "*** " << unitPrice << endl;
    cout << "------------------" << endl;
    cout << endl;

    cout << endl;
    system("pause");
    return 0;
}

//END PROGRAM CODE

//BEGIN PROGRAM OUTPUT

/*
Please enter the following information.
Date (in MM/DD/YY format): 12/03/1970

------------------
*** 12/03/19
------------------

Quantity of Books: 200

------------------
*** 200
------------------

ISBN (including hyphens): 0-123-45678-90xxxxxx

------------------
*** 0-123-45678-90xxx
------------------

Book Title: Anthony Goes to Hollywood, Summer 2012 Edition

------------------
*** Anthony Goes to Hollywood, Sum
------------------

Unit Price: 12.00

------------------
*** 12
------------------


Press any key to continue . . .
*/

Upvotes: 1

Views: 2808

Answers (2)

JsDoITao
JsDoITao

Reputation: 114

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. If you don't want this character to be extracted, you can use member get instead.

The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

If the function stops reading because this size is reached, the failbit internal flag is set.

if it reaches buffer size and set failbit flag, i think the process to the left characters till '\n' would be depended on the implements of compiler. Maybe to your compiler if its failbit was set, you need more process to ignore '\n' or using cin.clear().

But suggest to use string instead of char array.

Upvotes: 2

jo_dman
jo_dman

Reputation: 303

have you tried writing smaller string? or you could try to give cin.getline a bigger number of characters.

i believe that your read misses the "\n" at the end of the string, and once you try to write it, there is no null terminator to finish the write.

f.e. cin.getline(date, 10)

Upvotes: 0

Related Questions