1

我是一名正在准备期末考试的 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 . . .
*/
4

2 回答 2

2

提取字符直到提取 (n - 1) 个字符或找到分隔符(如果指定了此参数,则为 delim,否则为 '\n')。如果在输入序列中到达文件末尾或在输入操作期间发生错误,提取也会停止。

如果找到分隔符,则将其提取并丢弃,即不存储,然后开始下一个输入操作。如果您不想提取此字符,则可以使用 member get 代替。

表示 c 字符串结束的结束空字符会自动附加到 s 提取的数据之后。

如果函数因为达到此大小而停止读取,则设置故障位内部标志。

如果它达到缓冲区大小并设置故障位标志,我认为直到 '\n' 的左侧字符的过程将取决于编译器的实现。如果你的编译器设置了它的失败位,你可能需要更多的过程来忽略'\ n'或使用cin.clear()。

但建议使用字符串而不是字符数组。

于 2012-06-03T09:26:42.197 回答
0

你试过写更小的字符串吗?或者你可以尝试给 cin.getline 更多的字符。

我相信您的读取错过了字符串末尾的“\n”,并且一旦您尝试写入它,就没有空终止符来完成写入。

cin.getline(date, 10)

于 2012-06-03T09:14:30.423 回答