首页 > 解决方案 > 没有错误,但代码在第二次输入后崩溃

问题描述

我需要编写这个程序来计算电影院的学校利润。它最初是使用 <>cin 编写的,但后来我需要使用 printf 重写它。代码零错误,输入成人票销售数量后就崩溃了。

`#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{ char movie[50];

printf("What is the name of the movie?");
scanf(" %s", movie);

int AdultSales, ChildrenSales;

printf("How many adults attended? ");
scanf(" %d", AdultSales);

printf("How many children attended? ");
scanf(" %d", ChildrenSales);

float GrossBoxOfficeProfit= ChildrenSales*9.25+AdultSales*11.25;
float AmountPaidToDistributer=GrossBoxOfficeProfit*0.8;
float NetBoxOfficeProfit=GrossBoxOfficeProfit-AmountPaidToDistributer;

scanf("movie: %s",movie);
scanf("\nAdult tickets sold: %d",AdultSales);
scanf("\nChild tickest sold: %d",ChildrenSales);
scanf("\nGross Box office profit: $%f",GrossBoxOfficeProfit);
scanf("\nNet Box office proft: $%f",NetBoxOfficeProfit);
scanf("\nAmount paid to distributer: $%f",AmountPaidToDistributer);

return  0;

}`

标签: c++variablesprintf

解决方案


Scanf 采用指向整数的指针而不是整数本身,尝试更改它:

int AdultSales, ChildrenSales;

printf("How many adults attended? ");
scanf(" %d", &AdultSales);

printf("How many children attended? ");
scanf(" %d", &ChildrenSales);

您可以在此处查看函数参考:http ://www.cplusplus.com/reference/cstdio/scanf/


推荐阅读