为什么我无法从struct将数据输入到char数组中?

时间:2019-11-19 19:13:41

标签: c++

我的代码或编译器有问题...我不知道...当我将一些字符输入到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;
}

2 个答案:

答案 0 :(得分:1)

您的问题是这个定义之间的相互作用:

struct s_data
{
    char Day[2];
    char Month[2];
    char Year[4];
    //8
};

此输出:

 cout<<"Year: "<<temp.data.Year<<endl;

此命令有副作用:

struct s_contact
{
...
    s_data data;
    char web[15];
...
};

在s_data中,您没有为字符串终止符(标记文本结尾的0字节)提供空间。您的输出基于字符串,这意味着它们将吐出所有数据,直到命中0终止符为止。

日输出为“日:129”,因为日与月之间没有0终止符;碰巧一个月又一个月,只是因为您的数据是个位数。该终止符将同时终止Day和Month字符串输出。 Year也有同样的情况,但紧接在“ 2019”之后的是一个网址,更引人注目。

可以通过将s_data中的大小增加为以下内容来解决此问题:

struct s_data
{
    char Day[3];
    char Month[3];
    char Year[5];
};

或者,或者,如果s_data是必须使用的格式,则可能需要执行一些特殊处理以确保仅输出所需的值。

附带说明:如果您以不同于声明的顺序填写数据字段,则会得到一些非常奇怪的效果-现在看到的是零终止符被良好的数据覆盖,但初始化顺序不正确,您会看到0终止符会清除现有数据。 (例如:设置网址,然后输入年份-您会丢失整个网址,因为填写4位数字的年份时,它会在第一个字节的开头加上0。)

答案 1 :(得分:-4)

只需从更改以下代码:

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_contact
{
char name[10];
char surname[10];
char number[10];
char email[15];
char job[15];
//s_data data; *It shouldnot be here.*
char web[15];
s_data data; //*It should be here*
//75
}

这是因为内存分配技术。

U can go through this for some references.

我希望代码现在可以正常运行。