entering numbers to array in a row whithout length input

This is a discussion on entering numbers to array in a row whithout length input within the C Programming forums, part of the General Programming Boards category; i use this code in order to input to a char array in a row but i need to input ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    entering numbers to array in a row whithout length input

    i use this code
    in order to input to a char array in a row
    but i need to input the length of the string i am about to enter

    i have a char array of 40 cell
    how to input to it in a row?
    and how to tell where my input chars end in this array ?
    Code:
        int index;
        char input[40];
        int i;
        int length=5;
    
    
     printf("enter string\n");
     for(index=0;index<length;index++){
       scanf("%c",&input[index]);
     }
     i=getchar();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by transgalactic2
    i have a char array of 40 cell
    how to input to it in a row?
    Perhaps you would like to use fgets(). I have an example in another thread of yours.

    Quote Originally Posted by transgalactic2
    and how to tell where my input chars end in this array ?
    Strings are C are null terminated, so you know that at the first '\0' character the string ends.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    i use this code
    in order to input to a char array in a row
    but i need to input the length of the string i am about to enter
    not sure what this means, could you clarify this last bit?

  4. #4
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    I think the intent is to be able to insert a string without having to specify the length of the array before hand.

    I suppose you could dynamically allocate the memory, but not without knowing how much you need to store. (i.e. the user would still have to say how much they were going to type in before they type it, which wouldn't be very useful)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by shoutatchickens
    I think the intent is to be able to insert a string without having to specify the length of the array before hand.
    Heh, now that you and itCbitC mentioned it, I see that I missed the part about "need to input the length of the string i am about to enter".

    Quote Originally Posted by shoutatchickens
    I suppose you could dynamically allocate the memory, but not without knowing how much you need to store. (i.e. the user would still have to say how much they were going to type in before they type it, which wouldn't be very useful)
    One can allocate some small amount and expand if necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I need to enter my input in one line.
    but in the previous method that i was using.
    I had to say what whould be the length of the string in order for it to work.
    but here i cant do that

    i have to enter a row of chars into input[40] array .and to know what length of "string" did I entered.

    regarding the example of laser light:
    there is EOF
    it for input from a file
    where do i put input[40]?
    what is stdin?
    Code:
    char name[40];
    
    /* ... */
    
    if (fgets(name, 40, stdin))
    {
        char *newline_ptr = strchr(name, '\n');
        if (newline_ptr)
        {
            *newline_ptr = '\0';
        }
    }
    else
    {
        /* Read error or pre-mature EOF. */
    }

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont need (cant) to use memory allocation
    my array is 40 places max

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by transgalactic2
    where do i put input[40]?
    What do you mean?

    Quote Originally Posted by transgalactic2
    what is stdin?
    The standard input stream, typically keyboard input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    fgets() terminates the input string with a NULL and handles the EOF and EOL condition so you don't have to worry about them.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to use it

    this is the command
    Code:
    fgets(name, 40, stdin)
    
    my array of chars is called input
    where to put it??
    what is stdin?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by transgalactic2
    how to use it
    Read some documentation on how to use fgets(). For example, search the Web for man fgets, or read cppreference.com's entry on fgets().

    Quote Originally Posted by transgalactic2
    my array of chars is called input
    where to put it??
    Instead of name you would use input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    how to use it

    this is the command
    Code:
    fgets(name, 40, stdin)
    for using fgets(), Yes!

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i was told that i cant use fgets

    is there any other way?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i was told that i cant use fgets

    is there any other way?
    If you can't use certain things or should use certain things, then perhaps you can post a list of what functions you ARE ALLOWED to use (or a list of what functions you are NOT supposed to use, if that list is shorter). It is terribly hard to help someone if they are only telling AFTER a suggestion has been made that it's not going to work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i use "gets"
    for this sort of input??

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  4. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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