【问题标题】:"Debug Assertion Failed" Visual Studio 2015“调试断言失败”Visual Studio 2015
【发布时间】: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;
}

Error Image

【问题讨论】:

  • 再次阅读 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


【解决方案1】:

这段代码唯一的问题是,在“单价”的scanf中,不是只写“%f”,而是写了“%7.2f”。 任何形式的格式都应该在 printf 部分完成,并且不能在 scanf 中定义。

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2014-09-09
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多