【问题标题】:cin.get() works but NOT cin.getline(). What am I doing wrong or misunderstanding about cin.getline()?cin.get() 有效,但 cin.getline() 无效。我对 cin.getline() 做错了什么或误解了什么?
【发布时间】:2012-06-03 06:55:26
【问题描述】:

我是一名正在准备期末考试的 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 中,在末尾添加\0 并跳过除此之外的所有内容直到达到\n... 默认情况下工作吗?

编辑:为了提供更多背景知识,这个例子来自我教科书的前面部分(第 3 章和第 4 章)。我想跟随它的进展,并在一些早期的、容易忘记的概念上刷新我的记忆。稍后(第 10 章)我将回顾字符串、string 库和 string 类。

感谢您的帮助!

--啊08

附: 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 . . .
*/

【问题讨论】:

    标签: c++ user-input getline cin


    【解决方案1】:

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

    如果找到分隔符,则将其提取并丢弃,即它是 不存储,下一个输入操作将在它之后开始。如果你 不想提取这个字符,可以使用 member get 而是。

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

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

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

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

    【讨论】:

    • 使用字符串而不是 char 数组似乎是普遍的共识。由于您的评论,我编辑了原始帖子以提供更多背景信息。谢谢!
    【解决方案2】:

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

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

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

    【讨论】:

    • 我只是尝试使用较小的字符串,一切正常。这意味着唯一的问题是如果我输入的字符串比 cin.getline(name,size) 长。
    猜你喜欢
    • 2013-05-02
    • 2013-12-02
    • 2019-07-26
    • 2022-07-31
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多