I tried writing 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 doing right in Example 2 that's missing in Example 1? Why does the situation in Example 1 skip the rest of the input prompts, and then enter 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?
A little contextual background: This example comes from earlier in my textbook (Starting Out With C++: From Control Structures through Objects 6th Ed., Tony Gaddis, Chapters 3 & 4). I wanted to follow along with its progression and refresh my memory on some early, easy-to-forget concepts as I study for finals. I'll be reviewing strings, the string library, and the string class later on (ibid, 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/*
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?
*/
#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;
}
|
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/*
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.
*/
#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;
}
|
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 . . . |