Thread: How do I read in a string to an array?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    How do I read in a string to an array?

    I have some data
    "name of client"
    I have to read in the name (it is entered within double quotes) and store it has a string (with 20max characters)
    How do I read the name into a string array?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read up on the use of fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    we didn't learn fgets yet.
    i only know scanf and getchar()

    can they be used?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So use what you know and post what you tried then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    well....I know I have to read in the name between the quotes, and I know that getchar() only reads one character at a time, so how do I get it to read the name (everything within quotes) and put it into a string?

    what im trying so far is along the lines of this.

    Code:
    #include <stdio.h>
    #include <conio.h>
    void main (void) {
    char a;
    printf ("enter lines:- ");
    a=getname();
    printf ("name is %s",a);
    }
    
    char getname (void) {
    char ch;
    char string [21];
    ch=getchar();
    while (ch!='"') ch=getchar();
    if (ch='"') {
    	ch=getchar();
    	while (ch!='"') scanf ("%s" ,&string);
    	}
    return string;
    	}

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    How about something more like this:
    Code:
    void getName ( char *buff, size_t limit )
    {
      char ch;
      char *pb = buff;
    
      while ( ( ch = getchar() ) != '\"' )
        ;
      while ( --limit > 0 && ( ch = getchar() ) != '\"' )
        *pb++ = ch;
      *pb = '\0';
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    well I don't exactly understand that. and we didn't learn pointers yet, so I shouldn't use them...but can reading the name in be done with scanf and getchar() cause to me it can't be done with getchar() seeing that it only reads one character at a time and I need to read in all the name characters and set it into a string.

    by the way, how do I set something to a string? C doesn't have string data types does it...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to use loops? If you must use getchar, loop until you reach the end of the line, or until you've run out of room in your array. Place the characters one at a time in the array. And no, C doesn't have a 'string' data type.

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

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe this, which is a bit like Prelude's code only perhaps within your reach

    Code:
    #include <stdio.h>
    
    void getname(char string[], int max);
    
    int main(void)
    {
        char a[21];
        printf("enter lines:- ");
        getname(a,21);
        printf("name is %s\n", a);
        return 0;
    }
    
    void getname(char string[], int max)
    {
        int ch;
        int i = 0;
    
        /* find the " */
        while ( getchar() != '"' );
    
        /* now copy some chars */
        while ( i < (max-1) && (ch=getchar()) != '"' ) {
            string[i] = ch;
            i = i + 1;
        }
        string[i] = '\0';
    
        /* find the other " if we didn't already */
        if ( ch != '"' ) {
            while ( getchar() != '"' );
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    why doesn't this code work?

    Code:
    #include <stdio.h>
    #include <conio.h>
    void main (void) {
    void getname();
    
    printf ("enter line:- ");
    getname();
    }
    
    void getname (void) {
    char ch;
    char string [21];
    ch=getchar();
    while (ch!='"') ch=getchar();
    if (ch=='"') {
    	ch=getchar();
    	while (ch!='"') scanf ("%s", &string);
    		}
    printf ("the name is %s", string);
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (ch!='"') scanf ("%s", &string);
    This never changes ch
    So either it doesn't execute at all, or it executes forever
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    ok, I did it over as
    Code:
    #include <stdio.h>
    #include <conio.h>
    void getname(char string[21]);
    
    main () {
    
    char name[21];
    
    printf ("enter line:- ");
    getname(name);
    printf ("name is %s", name);
    }
    
    void getname (char string[21]) {
    char ch;
    int i=0;
    ch=getchar();
    while (ch!='"') ch=getchar();
    if (ch=='"') {
    	ch=getchar();
    	while (ch!='"') {
    		string[i]=ch;
    		i++;
    		ch=getchar(); }
    		}
    string[i]='\0';
    }
    Last edited by Hexxx; 11-02-2003 at 07:09 AM.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    Im suppose to let the program stop when the (Data) name entered is "$$$$"

    i put
    Code:
    while (name!='$$$$')
    but I get an error "character constant must be one or two characters long" and a warning "Non portable pointer conversion"

    how do i make it stop when I enter "$$$$"?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Any length of characters that doesn't represent an escape character is a string, you need to use double quotes. And since you can't compare strings with the equality operators, include the string.h header and do this:
    Code:
    while ( strcmp ( name, "$$$$" ) != 0 )
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    thanks a million!!!
    the functions seem to be working now, now to try and put them together!!!!

    thanks for helping!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc 1d string array
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-07-2008, 09:20 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM