Problem:
I am trying to code a sample textbook program that will take a string consisting of a first and last name that are separated by a space, (ie."John Doe"), and then reorder them so as to read: "Doe, John". I am using the below stated pseudocode out of the textbook. Obviously there is more pseudocode than what I list here; however I'm pretty sure that the rest of my code is right. All of my internal testing works fine up to this point. That is, all of my arrays and variables test print just fine up to this point.
I think that my problem lies in my FOR statement. The current code that I am using, just prints out ascii characters on output. I am a newbie and trying to get my brain wrapped around the FOR statement. If you need more of the pseudocode, let me know. I'm trying not to make this post too big.
TEXTBOOK PSEUDOCODE:
Code:
For K = Count + 1 Step 1 To Length(FullName)
Set LastName[K] = FullName[K]
MY POTENTIAL PROBLEM CODE:
Code:
for (k = (Count+1); k>strlen(FullName); Count+1)
{LastName[k] = FullName[k];}
__________________________________________________ __
MY FULL CODE:
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
char FullName[30];
char FirstName[15];
char LastName[15];
char FirstInitial, LastInitial;
int k, Count;
cout << "Enter a name with first name first:";
cin.getline (FullName,30);
Count = 1;
while (FullName[Count] != ' ')
{
FirstName[Count] = FullName[Count];
Count = Count + 1;
}
FirstInitial = FullName[0];
LastInitial = FullName[Count+1];
for (k = (Count+1); k>strlen(FullName); Count+1)
{LastName[k] = FullName[k];}
cout <<LastName << ", " <<FirstName;
return 0;
}