简体   繁体   中英

Assigning 2 string::Array in 2 different a struct

this is the first struct and contain first member

#include <iostream>
#include <algorithm>
#include <cstring>
struct Student_Data {
    string Assignment[200];
};

this is the 2nd struct and contain one member

struct Courseassign { 
    string createassig[200];
};

//here is declare function 
Doc(Student_Data ,Courseassign );
//the main function to declare 2 structures and call the Doc() function 
int main()
{
    Courseassign phase1 ; // declare 1st struct 
    Student_Data Student_Details ; // declare 1st struct 
    Doc(  phase1  , Student_Details); // Call Doc() function to run it
}

here is our function to make the mission i have tried to use copy() but it wasn't working too and i know that cpy should be char not string so i just want to make member equal to another memeber in another struct while using string Array

void Doc(Courseassign phase1, Student_Data Student_Details )
{
    for(int i=0;i<200;i++) {
        cout<< "create assign " << endl ; 
        cin >> phase1.createassig[i]  ; // here to enter member value 
        // here it's just a try to make them equal each other but it failed
        Student_Details.Assignment[i] = phase1.createassig[i];
        // here it should be Array of char but i need to Array of strings so what should i use
        cpy( Student_Details.Assignment[i],phase1.createassig[i]); 
    }
}

What error do you get when you try: Student_Details.Assignment[i] = phase1.createassig[i];

strcpy as you mentioned is for char* s, if you would like to assign one string value to another string variable, you can also use string::assign

For example in your case it would be:

Student_Details.Assignment[i].assign( phase1.createassig[i] );

Finally, as mentioned in comments by @Peter, you are passing your structs by value, so any changes you make in them will not be propogated to the actual ones passed to the function. The changes will be made to them locally, and destroyed when the function terminates. You can try passing the structs by reference, ie

void Doc(Courseassign&, Student_Data&);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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