mercredi 30 janvier 2019

How to use a 2-D array as strings for each row so that they may be passes through functions in c++?

I am writing a program in c++ that stores a list of names from a file, stores the list into a 2-D array (size predetermined), and sorts the names. I am able to get a 2-D array from the names, but I am not quite sure how to approach using individual rows as seperate strings so I may pass them through functions that have 1-D char arrays as parameters, here is the relevent code.`

int main(){
cout << "Please enter the names of input and output files\n Enter file name 1.";
string filename1, filename2;
cin >> filename1;
cout << "\nEnter file name 2.";
cin >> filename2;
ifstream file1;
ofstream file2;
file1.open(filename1.c_str());
file2.open(filename2.c_str());
char names[10][9] = {'\0'};
char temp;                      // Now the files have been opened and the array created with a temp variable to store the characters being read from "file1"
for (int i = 0; i < 9; i++){
    for( int j = 0; j < 8; j++){
temp = file1.get();
file2 << temp;
names[i][j] = temp; //the 2-D array is created with a list of names
}
}

cout << "\n";

for (int i=0; i<9 ; i++){
for (int j=0; j<8 ; j++){   //printing out the names
cout << names[i][j];}
}


cout << "\n\n";

char string1[10];            //Here is where I am confused on how to do this part?? how do I use just one ROW of the array?
for (int i=0;i<9;i++){
temp = file1.get();
string1[i] << temp;
string1[i] = temp;
}
cout << string1;           
cout << myStringLength(string1);

return 0;
}

Aucun commentaire:

Enregistrer un commentaire