C++ c++;使用get的输入被替换为空格、不正确的数字
我正在制作一个程序,要求用户输入出生日期,然后输出。它使用异常类。然而,我似乎遇到的问题是,输入不知何故丢失或被转换成不想要的数字。代码:C++ c++;使用get的输入被替换为空格、不正确的数字,c++,input,get,C++,Input,Get,我正在制作一个程序,要求用户输入出生日期,然后输出。它使用异常类。然而,我似乎遇到的问题是,输入不知何故丢失或被转换成不想要的数字。代码: dob::dob() { cout << "Enter your date of birth in format MM-DD-YYYY" << endl; //cin.getline(monthChar, 10); cin.get(monthChar, 10, '-'); cin.ignore(); cin.get
dob::dob() {
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
//cin.getline(monthChar, 10);
cin.get(monthChar, 10, '-');
cin.ignore();
cin.get(dayChar, 03, '-');
cin.ignore();
cin.get(yearChar, 04);
cout << dayChar[0];
cout << dayChar[1];
cout << monthChar[0];
cout << monthChar[1];
cout << " " << endl;
day = dayChar[0] * 10 + dayChar[1];
month = monthChar[0] * 10 + monthChar[1];
year = yearChar[0] * 1000 + yearChar[1] * 100 + yearChar[2] * 10 + yearChar[3];
cout << month << day << year;}
dob::dob(){
您可以使用非常奇怪的方法将数字(typechar
)转换为数字(typeint
)
我理解你的想法
day = dayChar[0] * 10 + dayChar[1];
从两个字符dayChar[0]
和dayChar[1]
为int day
赋值
这是错误的,而且一定是这样
day = (dayChar[0] - '0') * 10 + (dayChar[1] - '0');
让我们查看详细信息。字符串“12”
有两个字符,分别为49
和50
,只需检查:
cout << (int)dayChar[0] << endl;
cout << (int)dayChar[1] << endl;
给予
因此,必须使用字符代码'0'
(它是48
)和表达式进行更正
dayChar[0] * 10 + dayChar[1]
(dayChar[0] - '0') * 10 + (dayChar[1] - '0')
将给予
(49 - 48) * 10 + (50 - 48) = 1 * 10 + 2 = 12
正如所料
更新:
还有一些需要改进的地方:
考虑使用atoi
或其他标准转换函数从字符串中获取数字,例如day=atoi(dayChar);
小心使用诸如cin.get(yearChar,04);
-04
是八进制中的数字(数字小于8并不重要,但011
等于十进制中的9
)
关于cin.get(yearChar,4);
-value4
表示“字符串大小”,我们知道char*
类型的字符串需要nul终止符(\0
)的一个额外位置,所以要获得“1996”
我们需要大小4+1,即它必须是
cin.get(yearChar, 5);
同样重要的是,数组yearChar
的大小必须等于或大于5
顺便说一下,如果您打算从注释中恢复<代码> CIN(MunthChar,10);,请考虑下面的代码段(其中代码< 11)>代码>而不是<代码> 10 <代码>,<代码> sSCANF用于字符串解析(这是C样式,只是显示可能的选项)):
char dataChar[11];//存储MM-DD-YYYY至少需要11个字符
整数日、月、年;
int rescnt=0;//将显示从文本行中提取的数字数量
而(1){
cout我对您的代码做了一些更改,效果很好:
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
//cin.getline(monthChar, 10);
cin.get(monthChar, 03, '-');
cin.ignore();
cin.get(dayChar, 03, '-');
cin.ignore();
cin.get(yearChar, 05);
cout << dayChar[0];
cout << dayChar[1];
cout << monthChar[0];
cout << monthChar[1];
cout << " " << endl;
int day, month,year;
day =(dayChar[0] - 48 )* 10 +(dayChar[1] -48);
month =( monthChar[0]-48) * 10 + (monthChar[1]-48);
year =( yearChar[0] -48)* 1000 + (yearChar[1]-48) * 100 + (yearChar[2]-48) * 10 + (yearChar[3]-48);
cout << month << day << year;
cout根据,cin.get(char*s,streamsize n,char delim)
将从流中提取字符,直到提取出(n-1)个字符或遇到定界字符。因此,假设输入字符串的格式正确为“MM-DD-YYYY”,即在不进行错误检查的情况下,您可以将代码更新为
std::cin.get(monthChar, 3, '-');
std::cin.ignore();
std::cin.get(dayChar, 3, '-');
std::cin.ignore();
std::cin.get(yearChar, 5);
std::cout << dayChar[0];
std::cout << dayChar[1];
std::cout << monthChar[0];
std::cout << monthChar[1];
std::cout << " " << std::endl;
auto day = (dayChar[0] - '0') * 10 + dayChar[1] - '0';
auto month = (monthChar[0] - '0') * 10 + monthChar[1] - '0';
auto year = (yearChar[0] - '0') * 1000 + (yearChar[1] - '0') * 100 + (yearChar[2] - '0') * 10 + yearChar[3] - '0';
std::cout << month << day << year;
std::cin.get(monthChar,3'-');
std::cin.ignore();
标准:cin.get(dayChar,3'-');
std::cin.ignore();
标准:cin.get(yearChar,5);
std::cout为什么您的streamsize 10和3在cin.get()
call中表示月份和日期?您能否提供新代码(根据答案中的示例进行更新)?只需在问题的末尾添加新代码作为一个片段。我想问题与输入中5
之前的0
有关,所以我坚持使用标准的功能化新代码。无论我输入什么数字,monthChar[0]以空格形式输出。问题最可能的原因是分配给字符串的内存使用不正确。您必须记住,如果读取了2个字符,则必须使用cin.get(monthChar,3'-');
(值为3),在此之前,monthChar
应声明为char monthChar[3];
或值大于3
。第三个字符是\0
(nul终止符)所必需的。否则,将数据写入较小大小的数组将导致违反数组边界,这反过来将导致操作错误(请阅读有关未定义行为和相关运行时错误的详细信息)你错过了一个重要的事实:cin.get(char*s,streamsize n,char delim)
将读取n-1个字符,除非首先找到delim
。我使用了你的建议,现在正确地输出了日期和年份,而不是月份。我在编辑的帖子中有输出。在编辑的帖子中添加了新代码。无论我输入什么,monthChar[0]仍然以空格形式输出。@NicholasA.Randall您在程序中使用的字符数组声明未显示,请检查大小-它们必须至少为3个月和天(char monthChar[3];
char dayChar[3];
)和5个年(char yearChar[5]
;`)将字符数组的声明更改为大小为3是可行的。但是为什么?为什么它在大小为2的情况下可以工作几天,在大小为4的情况下可以工作一年,而在大小为2的情况下不能工作一个月?这是一个关于未定义行为的示例-“有时有效,但有时失败”。您可以测试同样的情况,在更改数组声明的顺序后,行为可能会改变(但可能不是——未定义的行为)。无论如何,现在你记住了这个案例,我希望你以后永远不会犯类似的错误。
char dataChar[11]; // at least 11 chars are required to store MM-DD-YYYY
int day, month, year;
int rescnt = 0; // will show how many numbers are extracted from text line
while(1){
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
cin.getline(dataChar, 11);
int rescnt = sscanf(dataChar, "%d-%d-%d", &month, &day, &year);
if (rescnt != 3)
{
cout << "Imput was wrong!" << endl;
cin.clear(); // reset state of cin
cin.ignore(INT_MAX, '\n'); // clean the input buffer
}
else
{
break;
}
}
// data is ready
cout << month << " " << day << " " << year << endl;
cout << "Enter your date of birth in format MM-DD-YYYY" << endl;
//cin.getline(monthChar, 10);
cin.get(monthChar, 03, '-');
cin.ignore();
cin.get(dayChar, 03, '-');
cin.ignore();
cin.get(yearChar, 05);
cout << dayChar[0];
cout << dayChar[1];
cout << monthChar[0];
cout << monthChar[1];
cout << " " << endl;
int day, month,year;
day =(dayChar[0] - 48 )* 10 +(dayChar[1] -48);
month =( monthChar[0]-48) * 10 + (monthChar[1]-48);
year =( yearChar[0] -48)* 1000 + (yearChar[1]-48) * 100 + (yearChar[2]-48) * 10 + (yearChar[3]-48);
cout << month << day << year;
std::cin.get(monthChar, 3, '-');
std::cin.ignore();
std::cin.get(dayChar, 3, '-');
std::cin.ignore();
std::cin.get(yearChar, 5);
std::cout << dayChar[0];
std::cout << dayChar[1];
std::cout << monthChar[0];
std::cout << monthChar[1];
std::cout << " " << std::endl;
auto day = (dayChar[0] - '0') * 10 + dayChar[1] - '0';
auto month = (monthChar[0] - '0') * 10 + monthChar[1] - '0';
auto year = (yearChar[0] - '0') * 1000 + (yearChar[1] - '0') * 100 + (yearChar[2] - '0') * 10 + yearChar[3] - '0';
std::cout << month << day << year;