Single line user inputted string to Array?

This is a discussion on Single line user inputted string to Array? within the C Programming forums, part of the General Programming Boards category; How would I go about putting a single line of user inputted characters into an array ? Example... User inputs: ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Single line user inputted string to Array?

    How would I go about putting a single line of user inputted characters into an array ?

    Example...

    User inputs:
    987654321
    Then I want an array of say

    x[10]
    x[0] will be 9
    x[1] will be 8

    I'm really more stuck at the part of what to do with the user input... If I could atleast get it as one big chunk I could iterate it or something... It seems like I'm overlooking something basic in doing this but... I've tried using scanf_s to put it into an array e.g.
    Code:
    int main (void)
    {
    	char x[81]={'\0'};
    	printf("Type your #s here:\n");
    	scanf_s("%c", &x);
    	printf("Element 0: %c\n", x[0]);
    	printf("Element 1: %c\n", x[1]);
    	printf("Element 2: %c\n", x[2]);
    }
    Output from the above gives:
    Type your #s here:
    987654321
    Element 0: 9
    Element 1:
    Element 2:
    Press any key to continue . . .
    So the first thing is being input I believe, but how can I get the entire thing?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    never come across scanf_s(); sure you don't mean sscanf()? and instead of %c lookup the %s conversion spec.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by itCbitC View Post
    never come across scanf_s(); sure you don't mean sscanf()? and instead of %c lookup the %s conversion spec.
    Hmm looking up sscanf that looks interesting... But what I did mean was scanf_s which is the same as scanf but with a few security features... I guess scanf_s is new I don't see it used much in examples I find through google or books.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf_s is MS's "safe" version of scanf. It's not functionally different from scanf other than preventing overflows better than normal scanf (don't ask me exactly how, as I haven't actually looked at this particular function - there is a HUGE number of functions in the MS libraries that have the additiona _s variant, and _s stands for "safe" - that's about all I know).

    --
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Alright so it's a Windows API; makes sense now since I work on Unix and I have never heard of it or seen it before and thanks for the explanation matsp.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Alright so it's a Windows API; makes sense now since I work on Unix and I have never heard of it or seen it before and thanks for the explanation matsp.
    Nope, it's a C library function, not a Windows API function.

    --
    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.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Ok found out what I needed...

    The gets (or in my case gets_s) function is what I was looking for.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sparrowhawk View Post
    Ok found out what I needed...

    The gets (or in my case gets_s) function is what I was looking for.
    A portable, yet secure, way to do that is to use fgets() with stdin as the last argument.

    --
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Can't say that it's part of the standard C library if it's not under UNIX, more likely the Windows-specific secure extension.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Can't say that it's part of the standard C library if it's not under UNIX, more likely the Windows-specific secure extension.
    No, it's not part of the STANDARD C library, but it's also not part of the Windows API. Most compiler/library vendors supply some functions that are non-standard. In this instance, MS Visual Studio's C library has a whole lot of non-standard functions. And the compiler will warn if you use for example sprintf() and suggest that you should use sprint_s instead.

    --
    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.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Concur that it's not an API, however I didn't know that compiler vendors supply their own lib functions like the makers of Visual Studio do.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by itCbitC View Post
    Concur that it's not an API, however I didn't know that compiler vendors supply their own lib functions like the makers of Visual Studio do.
    Think of all the stuff in unistd.h. That's all (well, maybe most) found in "the C library" (libc) as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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