簡體   English   中英

為什么我不能從 struct 將數據輸入到 char 數組中?

[英]Why I can't input data into char array from struct?

我的代碼或編譯器有問題...我不知道...當我在 output 的 struct char 數組中輸入一些字符時,一些 arrays 有其他不應該存在的數據。 如果你能幫助我,那就太好了。 謝謝!

如果您仔細查看年份行(粗體):它寫為 www.tedbrown.com 並且它不應該在那里,它應該只是 output 年份。

OUTPUT:名稱:泰德

姓氏:棕色

號碼:123456

Email:tedb@gmail.com

工作:機械師

天數:129

月:9

年份: 2019www.tedbrown.com

Web: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;
}

您的問題是此定義之間的交互:

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

而這個 output:

 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”之后的恰好是 web 地址,這個地址更加明顯。

這可以通過將 s_data 中的大小增加到:

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

或者,或者,如果 s_data 是您必須使用的格式,您可能需要進行一些特殊處理以確保您僅 output 為您想要的值。

旁注:如果您以與聲明不同的順序填寫數據字段,您會得到一些非常奇怪的效果——您現在看到的是零終止符被好的數據覆蓋,但是初始化亂序,您將參見 0 終結符清除現有數據。 (例如:設置 web 地址和年份 - 您將丟失整個 web 地址,因為當您填寫 4 位數年份時,您將在其第一個字節上放置一個 0)。

只需更改以下代碼:

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
}

這是因為 memory 分配技術。

您可以通過此 go 獲取一些參考。

我希望代碼現在應該可以正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM