简体   繁体   English

C++:如何替换结构成员类型字符串

[英]C++ : How to substitute structure member type string

I want my string to comprised of blanks too.我希望我的字符串也由空格组成。 So I used getline not cin .所以我使用getline而不是cin I want to substitute SearchStd->StdName to NewName.我想将 SearchStd->StdName 替换为 NewName。 So I used strcpy.所以我使用了strcpy。 But I can't do that because string type isn't char array.但我不能这样做,因为字符串类型不是 char 数组。 So I want to know what should I replace strcpy with, to substitute string type of structure member.所以我想知道我应该用什么替换 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;
}
}

You can use operator= to copy std::string .您可以使用operator=复制std::string

(*SearchStd).StdName = NewName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM