我是一名C ++学生,正在攻读总决赛。我用2种方式写了一个程序。第一个代码使用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
,并跳过除此之外的所有字符,直到达到string
{1}} ...默认情况下?
编辑:为了给出更多背景知识,这个例子来自我的教科书(第3章和第4章)。我想跟随它的进展,并在一些早期的,容易忘记的概念上重温我的记忆。我将在稍后(第10章)查看字符串,
string
库和/* 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 . . . */
类。
感谢您的帮助!
- 啊08
P.S。 ISBN编号设置为保留ISBN-13(13个数字,4个连字符)。
/*
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 . . .
*/
{{1}}
答案 0 :(得分:2)
提取字符,直到(n-1)个字符为止 提取或找到分隔字符(如果这样,则为delim 指定参数,否则指定'\ n')。提取也停止了 如果在输入序列中到达文件末尾或是否有错误 在输入操作期间发生。
如果找到分隔符,则将其提取并丢弃,即它 没有存储,下一个输入操作将在它之后开始。如果你 不希望提取此字符,可以使用member get 代替。
表示c-string结尾的结束空字符是 在提取数据后自动附加到s。
如果函数因达到此大小而停止读取,则会设置failbit内部标志。
如果达到缓冲区大小并设置failbit标志,我认为左边的字符到'\ n'的过程将取决于编译器的实现。对于你的编译器,如果设置了failbit,你需要更多的进程来忽略'\ n'或使用cin.clear()。
但建议使用字符串而不是char数组。
答案 1 :(得分:0)
我认为你的读取错过了字符串末尾的“\ n”,一旦你尝试编写它,就没有空终止符来完成写入。
f.e。 cin.getline(date, 10)