首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C绝对入门指南Chapter8Ex2

C绝对入门指南Chapter8Ex2
EN

Stack Overflow用户
提问于 2020-12-12 12:03:01
回答 1查看 83关注 0票数 0

我完全按照Miller's C编程绝对入门指南中打印出来的代码复制了以下代码。

一切都很顺利,直到第四个scanf,它应该请求一个日期。程序停止工作,弹出以下错误:

enter image description here

代码语言:javascript
运行
复制
// Example program #2 from Chapter 8 of Absolute Beginner's Guide to
// C, 3rd Edition
// File Chapter8ex2.c

/* This is a sample program that asks users for some basic data and prints it on
screen in order to show what was entered */

#include <stdio.h>

main()
{
    char topping[24];
    int slices;
    int month, day, year;
    float cost;

// The first scanf will look for a floating-point variable, the cost
// of a pizza
// If the user doesn't enter a $ before the cost, it could cause
// problems

    printf("How much does a pizza cost in your area?");
    printf("enter as $XX.XX)\n");
    scanf(" $%f", &cost);

// The pizza topping is a string, so your scanf doesn't need an &

    printf("What is your favorite one-word pizza topping?\n");
    scanf(" %s", topping);

    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", slices);

    printf("What is today's date (enter it in XX/XX/XX format).\n");
    scanf(" %d/%d/%d", &month, &day, &year);

    printf("\n\nWhy not treat yourself to dinner on %d/%d/%d", month, day, year);
    printf("\nand have %d slices of %s pizza!\n", slices, topping);
    printf("It will only cost you $%.2f!\n\n\n", cost);

    return (0);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-12 12:19:39

您忘记了将一些参数转换为指针。替换

代码语言:javascript
运行
复制
    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", slices);

使用

代码语言:javascript
运行
复制
    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", &slices);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65261481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档