I'm trying to make a program where it displays the users name based on what they entered. For example if the user enters: John Average User the output would display User, John A. And if the user enters John A. User the program would output User, John A. The problem I'm running into is if no middle name is entered. If I enter John User the program will not execute until I enter a third name. I want my program to be able to output if no middle name is entered.
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
string firstName;
string lastName;
string middleName;
string middleInitial;
string placePeriod();
int main(){
cout<<"Enter your first, middle and last name: "; cin>>firstName >>middleName >>lastName;
if(lastName==""){
lastName=middleName;
middleName="";
}
cout<<lastName <<", " <<firstName<<" " <<middleName[0] <<placePeriod() <<endl;
}
string placePeriod(){
if(middleName!=""){
return ".";
}
else{
return "";
}
}