c++ - 如何计算和显示正确的百分比?
问题描述
我是一个 C++ 菜鸟。
我正在尝试为学校开发这款基于文本的游戏,但无法显示正确的百分比。我认为这与我在程序中的计算方式有关,或者我在用函数搞砸了。
我将非常感谢一些帮助。干杯。
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
double menu (float crew_count);
double calculatePct(float crew_count, float number_of_deaths)
{
double percent = ((crew_count - number_of_deaths) / crew_count) * 100;
}
double welcome ()
{
int crew_count;
string backstory = "\nYou are in charge of a top-secret military mission, when your\nspace ship makes an emergency landing, on the largest moon of planet Gork.\nThe space ship is damaged. Oxygen levels begin to drop.\nHow many military personnel are on your ship?\nNumber of personnel: ";
cout << backstory;
cin >> crew_count;
if (crew_count >= 1)
menu(crew_count);
else if (crew_count < 1)
cout << "\nThere must be 1 or more members of the crew! Please enter a valid number!\n";
}
double menu (float crew_count)
{
double percent;
double main_option;
cout << "\nChoose one:\n1. Attempt repairs on the ship.\n2. Request an emergency rescue from mission command.\n3. Break protocol and reveal the top-secret space ship's location,\nto the Russians on a nearby moon, asking for their assistance.\nYour choice: ";
cin >> main_option;
if (main_option == 1)
{
cout << "\nToxic material on the moon has corroded the launch gear, and the \nlaunch exploded!\n\nThere were no survivors.\n";
}
else if (main_option == 2)
{
cout << "\nThe oxygen was depleted before the rescue team arrived.\nThere were 4 people killed.\n";
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
}
else if (main_option == 3)
{
cout << "\nThe Russians agree to send a rescue ship, but secretly attempt to hack into the ships systems remotely, which triggers an automatic shut down of all\ncommunications systems and locks all mission critical storage units, including\none of the storage unit that holds emergency oxygen tanks.\n\nOne quarter of all personnel are lost.\n";
}
else if (main_option != 1, 2, 3)
{
cout << "\nYou have been eaten by a Grue!\n";
}
}
int main()
{
cout << "Welcome to Gork 1.0\nCreated by Cortez Phenix\nTo make selections, enter the number of each option!\n\n";
int choice;
cout << "What would you like to do?\n1. Play Game\n2. Exit\nYour Choice: ";
cin >> choice;
if (choice == 1)
welcome();
else if (choice == 2)
cout << "\nGoodbye!\n";
else
cout << "\nPlease choose 1 or 2.\n";
return 0;
}
打扰一下。我敢肯定这篇文章很忙。
解决方案
如果我们对您的部分代码进行一些轻微的重新格式化,它看起来像这样:
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
您打印的值不是由 计算的值,它是函数前面定义calculatePct
的变量的不确定值。percent
简而言之:您忘记了花括号:
if (crew_count <=4)
cout << "0% of crew members were rescued!\n";
else
{ // Note curly brace here
float percent = calculatePct(crew_count, 4);
cout << percent << "% of the crew was rescued.\n";
} // And also here
我建议您启用更详细的警告,因为编译器应该能够检测到您使用了未初始化的变量,以及正在初始化但未使用的新变量。
推荐阅读
- javascript - 与缓存 SVG 数据相比,从磁盘读取 SVG 图标文件?
- javascript - 如何让其他计算机能够访问我的 socket.io 服务器
- vb.net - 当函数在VB.Net中可能有可变数量的参数时,将函数作为参数传递的有效且实用的方法?
- c# - MediaElement 在 WPF C# 中不显示视频
- r - 使用 lidR 绘制点云时的比例尺
- python - Django Robots.txt 更改内容类型标头
- python - 我们可以在类中使用方法定义内部的变量而不在方法外部和类内部先验定义它吗?
- python - 递归调用被循环中断
- r - 发生错误 应用程序无法启动(以代码 1 退出)。加载所需的包:carData 值 [[3L]](cond) 中的错误:
- jupyter-notebook - 如何启动 Anaconda 以使用 Jupyter 笔记本或 Jupyter 实验室开始 Python 编程?