Thread: Why doesn't this work

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31

    Why doesn't this work

    I'm new to c programming and working with little knowledge of it.
    I'm writing a program to read a name into the console and split it up into first middle last name.

    I've tried many things excluding tolkens because I don't know about them yet. With what I know so far I can't understand why this doesn't work.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char full_name[100];
    char first_name[50];
    char last_name[50];
    int ch;
    
    int main()
    {
        printf("Enter full name: ");
        fgets(full_name, sizeof(full_name), stdin);
        if(full_name[0] == ' ')
        {
            printf("Error reading name\n");
            getchar();
            return 1;
        }
        int i=0;
        while(full_name[i]<strlen(full_name))
        {
            strcpy(first_name[i], full_name[i]);
            ++i;
        }      
        printf("First name: %s \n", first_name);
        getchar();
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What on earth are you doing comparing the length of your string to one letter in the string?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31
    the idea I had in mind was a loop that ran from character to character until it read the entire input. Didn't know any other way to do it.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You should compare each charactor to zero
    Code:
    while(full_name[i])

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31
    thx I think that will work to get my loop going. but I still cant figure out how to copy one array to another. I've been trying to do it one character at a time.
    I'm thinking strcpy would work but the only way I know how to do it is if I already know the string to copy
    ie strcpy(first_name, "Spud")

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    strcpy( to_here, from_here );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM