C++ 为什么我能';t从struct向char数组中输入数据?

C++ 为什么我能';t从struct向char数组中输入数据?,c++,C++,我的代码或编译器有问题…我不知道…当我在struct char数组中输入一些字符时,在输出中,一些数组有其他不应该存在的数据。如果你能帮助我,那就太好了。谢谢大家! 如果仔细查看年份行(粗体):它写在www.tedbrown.com上,不应该在那里,它应该只输出年份 输出: 姓名:特德 姓:布朗 电话:123456 电邮:tedb@gmail.com 工作:机械师 日期:129 月份:9 年份:2019www.tedbrown.com 网址:www.tedbrown.com 我的代码: #inc

我的代码或编译器有问题…我不知道…当我在struct char数组中输入一些字符时,在输出中,一些数组有其他不应该存在的数据。如果你能帮助我,那就太好了。谢谢大家!

如果仔细查看年份行(粗体):它写在www.tedbrown.com上,不应该在那里,它应该只输出年份

输出: 姓名:特德

姓:布朗

电话:123456

电邮:tedb@gmail.com

工作:机械师

日期:129

月份:9

年份:2019www.tedbrown.com

网址:www.tedbrown.com

我的代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

struct s_data
{
    char Day[2];
    char Month[2];
    char Year[4];
    //8
};
//75+8=83
struct s_contact
{
    char name[10];
    char surname[10];
    char number[10];
    char email[15];
    char job[15];
    s_data data;
    char web[15];
    //75
};

void input_contact(s_contact &temp)
{
    string tempS;
    fflush(stdin);
    cout<<"Enter name:"<<endl;
    cin>>tempS;
    strcpy(temp.name, tempS.c_str());
    cout<<"Enter surname:"<<endl;
    cin>>tempS;
    strcpy(temp.surname, tempS.c_str());
    cout<<"Enter number:"<<endl;
    cin>>tempS;
    strcpy(temp.number, tempS.c_str());
    cout<<"Enter email:"<<endl;
    cin>>tempS;
    strcpy(temp.email, tempS.c_str());
    cout<<"Enter job:"<<endl;
    cin>>tempS;
    strcpy(temp.job, tempS.c_str());
    cout<<"Enter Day:"<<endl;
    cin>>tempS;
    strcpy(temp.data.Day, tempS.c_str());
    cout<<"Enter Month:"<<endl;
    cin>>tempS;
    strcpy(temp.data.Month, tempS.c_str());
    cout<<"Enter Year:"<<endl;
    cin>>tempS;
    strcpy(temp.data.Year, tempS.c_str());
    cout<<"Enter web:"<<endl;
    cin>>tempS;
    strcpy(temp.web, tempS.c_str());
}
int main()
{
    s_contact temp;
    input_contact(temp);
    cout<<endl<<endl<<endl;
    cout<<"OUTPUT:"<<endl;
    cout<<"name: "<<temp.name<<endl;
    cout<<"surname: "<<temp.surname<<endl;
    cout<<"number: "<<temp.number<<endl;
    cout<<"Email: "<<temp.email<<endl;
    cout<<"job: "<<temp.job<<endl;
    cout<<"Day: "<<temp.data.Day<<endl;
    cout<<"Month: "<<temp.data.Month<<endl;
    cout<<"Year: "<<temp.data.Year<<endl;
    cout<<"Web: "<<temp.web<<endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构s_数据
{
查日[2];
半个月[2];
焦年[4];
//8
};
//75+8=83
结构s_触点
{
字符名[10];
查氏[10];
字符数[10];
char电子邮件[15];
charjob[15];
s_数据;
炭网[15];
//75
};
无效输入触点(s触点和温度)
{
弦乐节拍;
fflush(stdin);

不能只需更改以下代码:

struct s_contact
{
char name[10];
char surname[10];
char number[10];
char email[15];
char job[15];
s_data data;
char web[15];
//75
}

这是因为内存分配技术


我希望代码现在可以正常运行。您的问题在于此定义之间的交互:

struct s_data
{
    char Day[2];
    char Month[2];
    char Year[4];
    //8
};
这个输出:

 cout<<"Year: "<<temp.data.Year<<endl;
在s_数据中,您没有为字符串终止符(标记文本结尾的0字节)提供空间。您的输出是基于字符串的,这意味着它们将吐出所有数据,直到命中0终止符

日输出为“日:129”,因为日和月之间没有0终止符;因为您的数据是一位数字,所以月与月之间正好有一个终止符。该终止符同时结束日和月字符串输出。年也会发生同样的情况,但紧跟在“2019”之后的是什么正好是一个网址,这是更引人注目的

这可以通过将s_数据中的大小增加到:

struct s_data
{
    char Day[3];
    char Month[3];
    char Year[5];
};
或者,如果s_数据是必须使用的格式,则可能需要进行一些特殊处理,以确保只输出所需的值


旁注:如果您以与声明不同的顺序填写数据字段,您可能会得到一些非常奇怪的效果——您现在看到的是零终止符被良好的数据覆盖,但初始化顺序不正确,您将看到零终止符擦除现有数据。(例如:设置网址&然后设置年份——您将丢失整个网址,因为当您填写4位数的年份时,会在其第一个字节上加0).

对不起,我忘了发布输入信息……这里是:输入姓名:ted输入姓氏:布朗输入号码:123456输入电子邮件:tedb@gmail.com输入作业:机械输入日:12输入月份:9输入年:2019进入Web:www. TEDBRONN.COMTIVE是C++。尽一切可能避免使用C风格的字符缓冲器,如<代码>字符名称[10 ]。
而是使用
std::string name
。您不想与字符缓冲区溢出问题作斗争,也不想使用
strcpy
和手动内存管理。事实上,您绝对不应该使用
strcpy
。如果必须使用C字符串,请始终使用带有缓冲区大小限制的字符串。
char Year[4]这样的老师绝对是最差的。尽你所能在这门课上做下去,但是记住,这是C++代码应该如何写的完全相反的。提示:使用<代码> char *< /Cl>和 StutUp >填充你的结构。这将总是正确地缓冲缓冲区。OU总是可以通过为各种结构实现简单的流运算符来序列化C++字符串,这意味着可以从“二进制”读写。文件。如果你花点时间考虑你正在处理的数据类型,这实际上非常简单。伙计们,你真的救了我…问题是年份数组的大小太小,我忘记了“\0”…我很感激你的所有想法,非常感谢!不,这不是导致OP出现问题的原因。是的,我试过了-而且如果你读过上面的评论,很明显这不是问题的原因。这是一个只使用代码的答案,最终只使用代码的答案只会提升你的复制粘贴技能。为了挽救这个答案,请解释问题所在(“不应该在这里”是不充分的解释)以及你提出的解决方案是如何解决问题的。一旦你这样做了,你可能会发现你只是用一个可见和已知的问题来交换一个隐藏的问题。这什么也解决不了,只是把问题扫到地毯下面,这样它就不会那么明显了。旁注:如果允许的话,你更喜欢
char
数组。
string
makes sure数据适合(正确使用时)并具有令人印象深刻的支持函数库。
struct s_contact
{
...
    s_data data;
    char web[15];
...
};
struct s_data
{
    char Day[3];
    char Month[3];
    char Year[5];
};