0

Structure( sorting by alphabet)

Please, help to find my mistake. How to write toUpper function with char? This is my code: #include <iostream> #include <fstream> #include <vector> using namespace std; // create structure for train struct train { int ID; char* FromL0cation; char* ToL0cation; char* DepartureTime; }; // custom comparator to do sorting bool sort_by_to_location(train a, train b) { return a.ToL0cation < b.ToL0cation; } string t0UpperCase(string word) { // convert to uppercase for (int i = 0; i < word.size(); i++) { if (word[i] >= 'a' && word[i] <= 'z') { word[i] = word[i] - 'a' + 'A'; } } return word; } int main() { // create a vector of planes vector<train> arr; // read input from command line int n; cin >> n; // read n values while (n--) { train p; // read id cin >> p.ID; // read from cin >> p.FromL0cation; //read to cin >> p.ToL0cation; // read time cin >> p.DepartureTime; // convert from and to to uppercase p.FromL0cation = t0UpperCase(p.FromL0cation); p.ToL0cation = t0UpperCase(p.ToL0cation); // add in vector arr.push_back(p); } // sort using custom comparator sort(arr.begin(), arr.end(), sort_by_to_location); cout << endl << endl; // show output for (int i = 0; i < arr.size(); i++) { train x = arr[i]; cout << x.ID << " " << x.FromL0cation << " " << x.ToL0cation << " " << x.DepartureTime << endl; } }

31st Oct 2020, 3:39 PM
Marina
2 Respuestas