Thread: Help this noob get string input

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    Question Help this noob get string input

    Hey guys,

    I'm starting computer science at uni this year and I'm teaching myself some C using online resources.

    I'm trying to get a string input from a user in the console and save it into a char array, using scanf_s. This is what I have so far :

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
        char str[50];
    
    
        printf("Enter a word : ");
        scanf_s("%s", str);
    
    
        getchar();
        return 0;
    }
    I used 'scanf_s' because I'm using visual studio and it forces you to. When I run the propgram, enter a string and press enter it crashes and I get a visual studio error (not an output error) that says :

    Unhandled exception at 0x654D3FD4 (msvcr120d.dll) in Practice2.exe: 0xC0000005: Access violation writing location 0x00B70000.

    When doing it with a single char and %c it's fine but when I try to save a string it crashes.

    Weird thing is, even when copy+pasting working online examples of saving strings in this manner it crashes with this error message. I don't know if it's Visual Studio or something I'm missing. I've been trying different things for ages with no success . Any help would be great!

    Cheers,

    -Dinipants

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    https://msdn.microsoft.com/en-us/lib...v=vs.120).aspx

    Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.
    You specified an input parameter of type 's', therefore you need to pass in the buffer size.

    The documentation gives an example as well:

    Code:
    char s[10];
    scanf_s("%9s", s, _countof(s)); // buffer size is 10, width specification is 9

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dinipants
    I used 'scanf_s' because I'm using visual studio and it forces you to.
    No, it does not. It recommends scanf_s with a, but you do not actually have to use it, e.g., you can silence the warnings by defining _CRT_SECURE_NO_WARNINGS as part of the compiler options. However, you should still specify the field width as in arpsmack's example, which would then be:
    Code:
    char s[10];
    scanf("%9s", s);
    Remember to check the return value of scanf too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-27-2013, 03:45 PM
  2. Replies: 10
    Last Post: 12-09-2011, 02:52 AM
  3. Replies: 13
    Last Post: 11-04-2011, 06:16 PM
  4. Noob question(User input Arrays)
    By wildex999 in forum C++ Programming
    Replies: 12
    Last Post: 02-19-2006, 04:25 PM
  5. Replies: 5
    Last Post: 11-01-2002, 06:09 PM

Tags for this Thread