【发布时间】:2016-02-17 10:14:29
【问题描述】:
我在 c 中的一段代码似乎有错误。 当我尝试运行它时,它会给出“Microsoft Visual Studio C++ 运行时库”。 在我给出第一个输入并按回车后,它给出了这个错误。 在我的代码中,我试图从用户那里获取一些信息,然后以新格式打印出来。
#include <stdio.h>
int main(void)
{
int Code;
int Day, Month, Year;
float Price;
printf("Enter Item Number: ");
scanf_s("%d", &Code);
printf("Enter Unit Price: ");
scanf_s("%7.2f", &Price);
printf("Enter Purchase Date (mm/dd/yyyy): ");
scanf_s("%2.2d/%2.2d/%4.4d", &Month, &Day, &Year);
/* Here is the New Format Printout */
printf("Item\tUnit\tPurchase\n\tPrice\tDate\n");
printf("%3.3d\t$%f\t%d/%d/%d", Code, Price, Month, Day, Year);
return 0;
}
【问题讨论】:
-
再次阅读 scanf 文档。您的格式有误。
-
%7.2f-->%2f,%2.2d-->%02d -
你应该已经收到一堆编译器警告,例如
'scanf_s' : unknown type field character '.' in format specifier或'scanf_s' : too many arguments passed for format string。请不要忽视他们 -
构建代码后没有任何错误。
-
您将此标记为
C++。你为什么不使用cin?使用scanf_s或类似函数会删除cin的类型安全功能。如果scanf_s的格式说明不正确,则程序的行为未定义。
标签: c visual-studio-2015