我是一名正在准备期末考试的 C++ 初学者。我用两种方式编写了一个程序。第一个代码使用cin.getline()
并且无法正常工作。第二个代码使用cin.get()
并cin >>
正确执行所有操作。
我错过了什么?为什么示例 1 中的情况会跳过其余的输入提示,然后输入无用的数字?
本质上不cin.getline(ARRAYNAME,ARRAYSIZE)
应该做setw(n)
,cin.get(ARRAYNAME,ARRAYSIZE)
和的工作cin.ignore(int,char)
吗?通过 cin.getline(ARRAYNAME,ARRAYSIZE)
提取ARRAYSIZE-1
字符,将它们放入ARRAYNAME
,在末尾添加 a\0
并跳过除此之外的所有内容,直到\n
默认情况下...
编辑:为了提供更多背景知识,这个例子来自我教科书的前面部分(第 3 章和第 4 章)。我想跟随它的进展,并在一些早期的、容易忘记的概念上刷新我的记忆。稍后我将回顾字符串、
string
库和string
类(第 10 章)。
谢谢您的帮助!
——啊08
PS ISBN 编号设置为保存 ISBN-13(13 个数字,4 个连字符)。
用户输入书籍信息 - 示例 1(无法正常工作)
/*
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 . . .
*/
用户输入书籍信息 - 示例 2(工作正常)
/*
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 . . .
*/