Help with array to manipulate name data

This is a discussion on Help with array to manipulate name data within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to create a program that asks the user to input a first and last name and ...

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

    Help with array to manipulate name data

    Hello, I am trying to create a program that asks the user to input a first and last name and then outputs it in the following format:

    George Washington
    Washington, G.

    Here is what I have; it won't print the last name when I run it.

    Code:
    #include<stdio.h>
    int main(void)
    {
     int i;         /*indexing integer*/
     char last[50]; /*two seperate arrays to hold char strings*/
     char name[50];
     printf("Enter a first and last name: \n\n");
     fgets(name, sizeof name, stdin);  /*better than scanf*/
      for(i=0; i<' '; i++)                 /*loop until space*/
       {
        for(i=' ';i<='\0';i++)           /*loop after space*/
         {
          name[i>' ']=(char)last[i>' ']; /*define part of name to be      last*/
         }
        }
        printf("%s,%c.",last,name[0]);  /*attempt to print as described*/
        return 0;
    }                           /*the code doesn't print the last     name*/

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Go into your basic textbook on C and actually read it and attempt to understand it. It is quite clear to anyone with a basic knowledge of C, given the nature of your code, that you have just hacked together code without attempting to understand.

    What you've done is akin to a scratching few random German words on a page, walking into a room full of German people, and expecting your writing to be remotely meaningful to the people there.

    Also note that, since this is obviously a homework exercise, people here will not give you a solution. To understand why, read this site's homework policy, here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    I have a C textbook, I have looked at it and I have not found anything that explains how to print a fragment of the data in an array. From what I understand homework is meant to be a learning exercise and if I do all the research and spent a large amount of time on it and I can't find the answer, then I search for help on forums such as this one. If you do not want to help me, then why even post anything? Telling me to do things that I have already done doesn't help me. Neither does assuming that I just through something together without trying to understand it. If you don't want to help then don't, but then why even put forth the effort to explain why you won't help me and imply that I don't have any understanding of C?

    Also, I have done the homework problem, not asked you or anyone else to do the whole thing for me. It also reads "feel free to ask" if you get stuck and can't figure something out. If this is the way people respond to others' requests for help, I don't understand how this forum could possibly be for the benefit of people trying to learn to program.
    Last edited by gallupingcactus; 03-31-2013 at 09:10 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by gallupingcactus View Post
    Telling me to do things that I have already done doesn't help me. Neither does assuming that I just through something together without trying to understand it.
    The thing you're not grasping is that I know what that code means and I have enough experience with other code to recognise how you put it together. And I know that fixing the code to actually address your problem will amount to rewriting it from scratch.

    How about you explaining in detail what this line
    Code:
        name[i>' ']=(char)last[i>' '];
    is actually doing? Hint : the comment you have on that line has no relationship to what it actually does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    I'm trying to separate the array into the the first name and last name parts. I don't know the ascii equivalent of the space-bar and in general, it is better to use the single quotes to refer to ascii characters than use the actual ascii numbers themselves. No doubt, what I intend for the line to do and what it actually does may be entirely different; hence I am posting to a c programming forum and asking for help. From what I understand (which is clearly not at the same level as your understanding) is that for any index of the array that has a larger value than ' ', I am defining last with the same index to be character type. One alternative (which is not what I intended) is that it could be taking all values with a greater ascii equivalent than ' ' and setting them equals to the values in last that are also greater than the ascii ' '.

    Feel free to tell me where I am wrong, as well as any hints as to how to print part of the data in an array if you so choose.
    Last edited by gallupingcactus; 03-31-2013 at 09:59 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by gallupingcactus View Post
    I'm trying to separate the array into the the first name and last name parts. I don't know the ascii equivalent of the space-bar and in general, it is better to use the single quotes to refer to ascii characters than use the actual ascii numbers themselves. No doubt, what I intend for the line to do and what it actually does may be entirely different; hence I am posting to a c programming forum and asking for help. From what I understand (which is clearly not at the same level as your understanding) is that for any index of the array that has a larger value than ' ', I am defining last with the same index to be character type. One alternative (which is not what I intended) is that it could be taking all values with a greater ascii equivalent than ' ' and setting them equals to the values in last that are also greater than the ascii ' '.
    There is absolutely NO RELATIONSHIP between what you are describing (either alternative) and what the code actually does. And this is a difference in only ONE LINE of your code. Your code has numerous problems on other lines.

    The line of your code that I quoted is actually equivalent to
    Code:
        if (i > ' ')
            name[1] = last[1];
        else
            name[0] = last[0];
    In other words, at best, that line of code changes a value of a single character.

    The problem is that your understanding of what your code does is very far removed from reality of what it actually does. There are so many things you have misunderstood, that it is impossible to use your current understanding as a basis for helping you learn.

    My situation is akin to trying to teach a belching baby how to write a novel. That baby might understand a few sounds, but hasn't even learned yet what letters and words are, let alone grammatical structure. Without that understanding of letters, words, and grammar, that baby will not be able to read or write a novel.

    That leaves only two options for ANYONE to help you;

    1) Tell you that you are not understanding what your own code does, and suggest you go back and read a basic text.

    2) Give you code that actually solves your homework for you.

    I have done the first. The second is at odds with the site policy here because, if people did that, you would learn EXACTLY NOTHING OF USE from the exercise.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by gallupingcactus View Post
    I'm trying to separate the array into the the first name and last name parts. I don't know the ascii equivalent of the space-bar and in general, it is better to use the single quotes to refer to ascii characters than use the actual ascii numbers themselves. No doubt, what I intend for the line to do and what it actually does may be entirely different; hence I am posting to a c programming forum and asking for help. From what I understand (which is clearly not at the same level as your understanding) is that for any index of the array that has a larger value than ' ', I am defining last with the same index to be character type. One alternative (which is not what I intended) is that it could be taking all values with a greater ascii equivalent than ' ' and setting them equals to the values in last that are also greater than the ascii ' '.
    There is absolutely NO RELATIONSHIP between what you are describing (either alternative) and what the code actually does. And this is a difference in only ONE LINE of your code. Your code has numerous problems on other lines.

    The line of your code that I quoted is actually equivalent to
    Code:
        if (i > ' ')
            name[1] = last[1];
        else
            name[0] = last[0];
    In other words, at best, that line of code changes a value of a single character.

    The problem is that your understanding of what your code does is very far removed from reality of what it actually does. There are so many things you have misunderstood, that it is impossible to use your current understanding as a basis for helping you learn.

    My situation is akin to trying to teach a belching baby how to write a novel. That baby might understand a few sounds, but hasn't even learned yet what letters and words are, let alone grammatical structure. Without that understanding of letters, words, and grammar, that baby will not be able to read or write a novel.

    That leaves only two options for ANYONE to help you;

    1) Tell you that you are not understanding what your own code does, and suggest you go back and read a basic text.

    2) Give you code that actually solves your homework for you.

    I have done the first. The second is at odds with the site policy here because, if people did that, you would learn EXACTLY NOTHING OF USE from the exercise.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Code:
    #include<stdio.h>
    int main(void)
    {        
        char first[50]; 
        char last[50];
        printf("Enter a first and last name: \n\n");
        scanf("%s %s", first, last);
        printf("%s, %c.", last, first[0]);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2012, 05:42 AM
  2. Replies: 2
    Last Post: 05-06-2011, 08:43 AM
  3. Replies: 5
    Last Post: 04-05-2011, 10:51 AM
  4. read in data, manipulate, and print back out
    By zeebo17 in forum C Programming
    Replies: 10
    Last Post: 09-22-2009, 11:28 PM
  5. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 02:52 AM

Tags for this Thread


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