Hello guys. I am just a few weeks into my C++ course. I am working on an assignment in which the user has to input three names into the console and then they have to be arranged in alphabetical order. I can only use if/else statements to do so (nothing more advanced). I cannot seem to figure it out and I have searched all over the web for an answer to no avail. How would I solve this? This is the code I have so far.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string name1, name2, name3, displayName1, displayName2, displayName3;
// Reads 3 names.
cout << "A name is a single word with no spaces.\n";
cout << "Enter 3 DIFFERENT names separated by spaces:\n";
cin >> name1 >> name2 >> name3;
cout << endl;
replace the elseif on line 18 with if. The first elseif statement in a chain of if-else if statements has to be preceded by an if statement. Also, you can change the if on line 34 to an elseif and replace all of line 51 with else.
Thank you. I followed your advice except with replacing line 51 with else. When I did that I got a compiler error saying "; expected before {", so I changed line 51 back to else if and it worked.
When I compile it, it works. I'm thinking you only replaced the if with else. What I meant to say was replace if ((name3 < name1) && (name3 < name2)) with else.