gpt4 book ai didi

c++ - cin.get() 有效但 cin.getline() 无效。我对 cin.getline() 做错了什么或误解了什么?

转载 作者:行者123 更新时间:2023-11-30 04:27:32 28 4
gpt4 key购买 nike

我是一名正在准备期末考试的 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... 默认情况下?

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).

感谢您的帮助!

-- 啊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 . . .
*/

最佳答案

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

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

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

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

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

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

关于c++ - cin.get() 有效但 cin.getline() 无效。我对 cin.getline() 做错了什么或误解了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10868479/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com