I'm a novice programmer learning C++ through a tutorial and I want to know how I can take a string, take its first letter, compare it to the first letters of 2 other strings and then sort them alphabetically. I don't want code written for me. I want to understand how to do this because other solutions were difficult to understand.
I know that strings are just arrays of char so there should be a method to get the first index.
UPDATE
So this is my attempt:
#include <iostream>
#include <string>
using namespace std;
void valueOutput(string firstName, string secondName, string thirdName){
cout << "\n";
cout << firstName << endl;
cout << secondName << endl;
cout << thirdName << endl;
}
int main(){
string name1, name2, name3;
cout<<"Enter 3 names: "<<endl;
cin>>name1;
cin>>name2;
cin>>name3;
if(
(name1[0] < name2[0] && name2[0] < name3[0])
|| (name1[0] < name3[0] && name3[0] < name2[0])
|| (name2[0] < name1[0] && name1[0] < name3[0])
|| (name2[0] < name3[0] && name3[0] < name1[0])
|| (name3[0] < name1[0] && name1[0] < name2[0])
|| (name3[0] < name2[0] && name2[0] < name1[0]))
{valueOutput(name1, name2, name3);}
else{
return 0;
}
}
My input was: Steinbeck Hemingway Fitzgerald
but the output is the exactly in the same order. I want to sort them alphabetically.