For Loop Issues

This is a discussion on For Loop Issues within the C++ Programming forums, part of the General Programming Boards category; Problem: I am trying to code a sample textbook program that will take a string consisting of a first and ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    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 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;
    }
    Last edited by jtkhoskinson; 03-27-2010 at 01:26 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should look at what the bits of the for loop mean, because they don't exactly match the way for works in BASIC.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I haven't read all of your description and code, but in your for loop
    Code:
    for (k = (Count+1); k>strlen(FullName); Count+1)
    You're condition ("k>strlen(FullName)") involves "k" which is never modified in your loop. This means that once control enters this for loop, it will loop infinitely. In most cases, your condition (the "middle part") shouldn't be constant; it should change. This is often done by modifying this value in the "third part" of the for loop. Also note that the statement "Count+1", as you may see if your compiler warning levels are high enough, doesn't "do" anything. Do you mean to increase this value? If so, instead do "Count++" to actually modify this value. But, again, this "third part" should involve modifying "k" or "FullName", so that you don't have an infinite loop.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Full Pseudocode and explanation

    I appreciate the replies, though still not quite getting it.

    Here is the full program pseudocode and what the text says that the program should do:


    PSEUDOCODE:

    Code:
    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

    EXPLANATION:

    After the person’s full name is input, the counter-controlled While loop assigns characters in FullName to FirstName until the blank between the first and last names is encountered. At this point, the value of Count is one more than the length of FirstName, and (since the blank is character number Count) the first character in LastName is numbered Count + 1. Thus, the two assignment statements that follow the While loop correctly store the person’s initials, and the For loop copies the correct part of FullName to LastName. Finally, the Write statement, with the help of the concatenation operator +, displays the person’s name, last name first.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So pseudocode/BASIC for and C for aren't the same, as far as syntax goes.

    Pseudo:
    Code:
    FOR variable = start STEP number TO stop
    where start, number, and stop are just numbers.

    C:
    Code:
    for (beginning_assignment_statement; keep_going_condition; update_statement)
    Here the outside pieces of the for are statements -- things that could stand alone, by themselves, as pieces of code. (In "For k = 1 step 1 to 5", 1 would not stand by itself as a piece of code. In C you need to do an assignment that will add 1 to your variable.) The middle thing is a condition -- something that will evaluate to either true or false -- and will evaluate to true when you want the loop to keep going, not when you want it to stop.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    I have updated my code to:

    Code:
    for (k = (Count+1); k<strlen(FullName); k++)
        {LastName[k] = FullName[k];}
    It is getting closer. It still has some ASCII that it is printing out. (a = a random ASCII character, instead of the correct letter)

    If I input John Doe, I get the following printout.

    It now prints aaa, aohnaa

    Instead of Doe, John

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Despite what your textbook's pseudocode says, you need to start writing your LastName at index 0 instead of index k. (For that matter, you also need to start writing your FirstName at index 0.)

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    That is because in C++ indexing begins at 0, correct?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jtkhoskinson View Post
    That is because in C++ indexing begins at 0, correct?
    That is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I hate pointers or Pointer Issues
    By bolivartech in forum C Programming
    Replies: 9
    Last Post: 11-14-2009, 11:48 AM
  2. My new ASUS Crosshair 2 Formula issues
    By VirtualAce in forum Tech Board
    Replies: 8
    Last Post: 03-02-2009, 08:47 PM
  3. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 04:46 PM
  4. Multi-platform support issues.
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-27-2004, 07:41 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21