我在使用多个字符和scanf_s()时遇到麻烦 [英] I am having trouble with multiple chars and scanf_s()

查看:418
本文介绍了我在使用多个字符和scanf_s()时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用scanf_s()读取多个值,但是每次运行程序时,我都会得到

I'm trying to use scanf_s() to read in multiple values but every time I run the program, I get

实验室2.exe中0x592AD6AC(msvcr120d.dll)处未处理的异常:0xC0000005:访问冲突写入位置0x00000000.

Unhandled exception at 0x592AD6AC (msvcr120d.dll) in lab 2.exe: 0xC0000005: Access violation writing location 0x00000000.

在弹出窗口中.我该如何解决?

in a popup window. How do I fix this?

float inTemp;
char inUnit;
char outUnit;

printf("Please enter the starting temperature with its units and the units\nyou would like to convert to (i.e. 74.5 F C): ");
scanf_s("%f %c %c", &inTemp, &inUnit, &outUnit); //takes in all user input (NOT WORKING)

推荐答案

您尚未提供长度指示器.从MSDN:

You have not provided a length indicator. From MSDN:

与scanf和wscanf不同,scanf_s和wscanf_s需要缓冲区大小 为c,C,s,S或字符串类型的所有输入参数指定 []中包含的控件集.缓冲区大小(以字符为单位)为 紧跟在指针之后作为附加参数传递 缓冲区或变量.例如,如果您正在读取字符串,则 该字符串的缓冲区大小按以下方式传递:char s [10]; scanf_s(%9s",s,_countof(s));//缓冲区大小为10,宽度 规格是9

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. For example, if you are reading a string, the buffer size for that string is passed as follows: char s[10]; scanf_s("%9s", s, _countof(s)); // buffer size is 10, width specification is 9

此处是该页面的链接.

这篇关于我在使用多个字符和scanf_s()时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆