I'm getting an Exception Error when using %S

1
int main()
 {
 int Age;
 char Name;

 //Age

 printf("Type your age: ");
 scanf_s("%d", &Age);
 printf("Your age is %d\n", Age);

 //Name

 printf("Type your Name: ");
 scanf_s("%s", &Name);
 printf("Your name is %s", Name);

 return 0; }

It's the 'Name' section which is throwing out an error. I can't figure out why.

UPDATE: I'm coding in Visual Studio. Therefore, "scanf_s" is essentially required.

The error is "Exception thrown at 0x5B49D4EC (ucrtbased.dll) in Project1.exe: 0xC0000005: Access violation writing location 0x001A0000. occurred"

c
asked on Stack Overflow Jan 22, 2019 by Pablo • edited Jan 22, 2019 by Ken White

4 Answers

2

Your problem is that char Name; can only store a single character. Your code is allowing the user to type in multiple characters which are being stored into Name causing a memory error.

Change char Name; to something like char Name[50] so that you can store up-to 49 characters plus the null byte.

Also you should use scanf_s() properly to avoid the error if the buffer (char array) ends up being too small.

Note, you should always check the return from scanf_s() so you know if the user entered valid data or not.

This code works correctly in Visual Studio:

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>


int main()
{
    int Age;
    char Name[50];

    printf("Type your age: ");
    if(scanf_s("%d", &Age))
    {
        printf("Your age is %d\n", Age);

        printf("Type your Name: ");
        if (scanf_s("%s", Name, (unsigned)_countof(Name)))
        {
            printf("Your name is %s\n", Name);
        }
        else
        {
            printf("Name:: Invalid Input\n");
        }
    }
    else
    {
        printf("Age:: Invalid Input\n");
    }

    return 0;
}
answered on Stack Overflow Jan 22, 2019 by Chimera • edited Jan 22, 2019 by Chimera
1

The problem is that you defined Name as a char - a single character - but you are trying to use it as a string (multiple characters).

To fix this you must either (a) define Name as an array of characters (which would be a string) - such as char Name[100]; or (b) as a pointer (such as char *Name;) - which would require you to malloc() the string before use and free() it after use.

Strings can be tricky, as they are basically just arrays of chars, but that requires you to either know, or find a way to know, how many characters will be in the string. You can read more about how to do that here, in the documentation for scanf_s, which gives this example:

char c[4];
scanf_s("%4c", &c, (unsigned)_countof(c)); // not null terminated
answered on Stack Overflow Jan 22, 2019 by cegfault
0

First off I would just use scanf(), not scanf_s(). Furthermore you need to cast your Name variable as a string, which is an array of characters as I have defined it below. Using just char Name, means you have created a variable with room for just one character.

Hope this helps :)

 int main()
 {
     int Age;
     char Name[10];


     printf("Type your age: ");
     scanf("%d", &Age);
     printf("Your age is %d\n", Age);

     //Name

     printf("Type your Name: ");
     scanf("%s", &Name);
     printf("Your name is %s", Name);

     return 0;
}
answered on Stack Overflow Jan 22, 2019 by sjdm
0

Fixed the problem by going to...

Tools->Options->Debugging->Symbols and select checkbox "Microsoft Symbol Servers", Visual Studio will download PDBs automatically.

Thanks for everyone's help :)

answered on Stack Overflow Jan 22, 2019 by Pablo

User contributions licensed under CC BY-SA 3.0