c++ - 没有错误,但代码在第二次输入后崩溃
问题描述
我需要编写这个程序来计算电影院的学校利润。它最初是使用 <>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;
}`
解决方案
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/
推荐阅读
- c++ - 用C++在txt文件中查找最长的单词
- apache-spark - Pyspark pivot data frame based on condition
- point-cloud-library - 如何“正确”初始化和加载 PointCloud
- c# - 如何在 xaml 中为 ToggleButton 设置切换颜色?
- android - Is there a way to test the Kivy App before compiling to APK?
- floating-point - Understanding how these floating point numbers work?
- python - Controlling color, legend when plotting from Pandas DataFrame with multiple y values for each x
- javascript - How to get the sum of values per day grouped by day from an array in JS?
- go - How to access maps inside Structs? cannot take the address of g.vertexes[base]
- javascript - aria-expanded="true" 不向父级添加类