Okay, this is for a school assignment that nobody can figure out. The assignment is to get the user to input something like John D. Smith. The computer then takes that and converts it into Smith, John D. The code I've tried is here. Any suggestions would be most welcome.

#include<iostream.h>
#include<fstream.h>

main()
{
int lastnamechar=0;
int countspace=0;
int adder=0;
int countspaces=0;
int countchars=0;
char user_name[25];
char ch;
ofstream outfile;
ifstream infile;
outfile.open("NEW.TXT",ios:ut);
if(!outfile)
{
cout << "Error opening file.\n";
return 0;
}
cout << "Enter your full name.";
cin.get(user_name, 25);
cout << '\n';
outfile << user_name << endl;
infile.open("NEW.TXT",ios::in);
if(!infile)
{
cout << "Error opening file.\n";
return 0;
}
infile.unsetf(ios::skipws);
while(!infile.eof())
{
if(ch==32)
{
countspace++;
}
if(countspace>=2)
{
lastnamechar++;
}
infile >> ch;
countchars++;
if(countchars==1)
{
if((ch>96)&&(ch<123))
{
ch=ch-32;
adder++;
}
outfile << ch;
}
if(countspaces>0)
{
if((ch>96)&&(ch<123))
{
ch=ch-32;
adder++;
}
outfile << ch;
}
if(countspaces>0)
{
countspaces=0;
}
if(ch==32)
{
countspaces++;
}
if(adder==0)
{
outfile << ch;
}
if(adder>0)
{
adder=0;
}
}
countspaces=0;
outfile.close();
infile.close();
infile.open("NEW.TXT",ios::in);
while(!infile.eof())
{
infile >> ch;
if(ch==32)
{
countspaces++;
}
if(countspaces>=countspace*2)
{
while(adder<lastnamechar)
{
if(ch==0)
{ ch=44; }
adder++;
infile >> ch;
cout << ch;
if(adder==lastnamechar)
{
cout << ", ";
}
}
}
}
infile.close();
return 0;
}