错误Assert失败表达式:_format_parser.error_code()== 0

kmbjn2e3  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(328)
#include <stdio.h>

int main()
{
    int a, n;
    float p;

    printf("Please enter article characteristics.\n");
    printf("Article Number: ");
    scanf_s("%d", &a);
    printf("Number of pieces: ");
    scanf_s("%d", &n);
    printf("Price per piece: ");
    scanf_s("%.1f", &p);

    printf("\n\tArticle Number\tQuantity\tPrice Per Piece\n");
    printf("\t     %d\t\t%d\t     %.1f Dollar", a, n, p);
    return 0;
}

当我运行代码时,我能够输入第一部分,但一旦我输入第二部分,我就会得到错误。即使我删除了第二部分,当我输入第一个输入时,我也会得到错误。

y1aodyip

y1aodyip1#

用户输入可能因人而异,因此最好使用scanf(“%f”,&p)而不是scanf(“%.1f”,&p),但在显示值或执行其他操作时,您可以根据需要更改小数点后一位(.1f)或两位(.2f)。下面是正确的代码:

#include <stdio.h>
int main()
{
    int a, n;
    float p;
    printf("Please enter article characteristics.\n");
    printf("Article Number: ");
    scanf("%d", &a);
    printf("Number of pieces: ");
    scanf("%d", &n);
    printf("Price per piece: ");
    scanf("%f", &p);
  
    printf("\n\tArticle Number\tQuantity\tPrice Per Piece\n");
    printf("\t     %d\t\t%d\t     %.1f Dollar", a, n, p);
    return 0;
}

您可以在

下面查看示例输出

相关问题