【问题标题】:C++ : How to substitute structure member type stringC++:如何替换结构成员类型字符串
【发布时间】:2021-07-10 19:33:39
【问题描述】:

我希望我的字符串也包含空格。所以我使用了getline 而不是cin。 我想将 SearchStd->StdName 替换为 NewName。所以我使用了strcpy。 但我不能这样做,因为字符串类型不是 char 数组。 所以我想知道我应该用什么替换 strcpy 来替换结构成员的字符串类型。

struct Subject {    //과목 정보 
string SubName; //과목 이름 
int Hakjum;         //과목 학점 
char Grade[10];     //과목 평점 
float GPA;          // 과목 평점
};

struct Student {    //학생 정보 
string StdName; //학생 이름 
int Hakbun;         //학번 
Subject *Sub;       //과목 
int SubNum;         //과목 수 
float AveGPA;       //교과목 평균
};

void ModifyStdInfo(Student *pSt, int StudentNum){           //학생 정보 수정
Student* SearchStd;
SearchStd = StdSearch(pSt, StudentNum);
if(SearchStd != NULL){
    string NewName;
    int NewHakbun=0;
    cout<<"* ("<<SearchStd->StdName<<", "<<SearchStd->Hakbun<<")의 정보를 수정하세요\n";
    cout<<"이름 :";
    getline(cin, NewName);
    cout<<NewName<<endl;
    strcpy((*SearchStd).StdName, NewName);
    cout<<"학번 : ";
    cin>>NewHakbun;
    SearchStd->Hakbun=NewHakbun;
}
}

【问题讨论】:

    标签: c++ arrays string char


    【解决方案1】:

    您可以使用operator=复制std::string

    (*SearchStd).StdName = NewName;
    

    【讨论】:

    • 不妨用-&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2021-05-22
    相关资源
    最近更新 更多