For Loop Issues

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 a 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.

The current code that I am using, just prints out ascii characters on output. I am a newbie (obviously, if I am posting in this forum) 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.


PSEUDOCODE:

For K = Count + 1 Step 1 To Length(FullName)
Set LastName[K] = FullName[K]


POTENTIAL PROBLEM CODE:

for (k = (Count+1); k>strlen(FullName); Count+1)
{LastName[k] = FullName[k];}


____________________________________________________

FULL 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 <<FullName << ", " <<FirstName;


return 0;
}
Last edited on
You are starting count from 1.. yet arrays start from 0... uhm...
You overcomplicated the code just too much :| . Side note.. cin separates input at ' ' (space character). soo...
A simpler code would look like this..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main ()
{
using namespace std;
  char FirstName[15];
  char LastName[15];

  cout << "Enter your full name: ";
  cin >> LastName;
  cin >> FirstName;
  cout << FirstName << ", "  << LastName;

return 0;
}

This is what you wanted right ?
Enter your full name: Adi Kid
Kid, Adi
Last edited on
@OP: Also the "increment" part of your for loop does nothing.
Thank you for the reply, adikid.

Yes, you are right about indexing starts at 0 in C++. I am in an introduction to programming class, and this week we are learning about arrays and how to manipulate them. It is an introduction to programming so it is not code specific. Basically, it is a bunch of pseudocode that I am trying to port over to C++. I tend to learn processes better if I do it as part of something, rather than just by reading. Soooo, I am trying to learn C++ as I learn proper programming structures. Anyway, the program is supposed to take an entered name like John Doe, stick it into an array, do some magic (split the first and last name using the "space" as the marker), and then output Doe, John.

Here is the pseudocode from which I am trying to piece this thing together from.

Declare FullName[30] Of Characters
Declare FirstName[15], LastName[15] Of Characters
Declare FirstInitial, LastInitial As Character
Declare K, Count As Integer

Write “Enter a name with first name first: ”

Input FullName
Set Count = 1
While FullName[Count] <> “ ”
Set FirstName[Count] = FullName[Count]
Set Count = Count + 1
End While

Set FirstInitial = FullName[1]
Set LastInitial = FullName[Count+1]

For K = Count + 1 Step 1 To Length(FullName)
Set LastName[K] = FullName[K]
End For

Write LastName + “, ” + FirstName
Last edited on
Zhuge,

Yes, you are right. I think that it should be:

for (k = (Count+1); k<strlen(FullName); k++)
Topic archived. No new replies allowed.