如何计算和显示正确的百分比?

时间:2020-09-28 23:31:48

标签: 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;
}

不好意思。我确定这篇文章很忙。

IMAGE: At the bottom, you can see the queer number

2 个答案:

答案 0 :(得分:3)

如果我们对代码的某些部分进行一些重新格式化,则看起来像这样:

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

我建议您启用更多详细警告,因为编译器应该能够检测到您使用了未初始化的变量,以及正在初始化但未使用的新变量。

答案 1 :(得分:0)

您混合了整数,浮点数和空白

  1. int crew_count,然后您喂饱double menu(float crew_count)
  2. 您的菜单功能不会返回任何可能无效的内容,因此您的欢迎功能也不会返回
  3. calculatePct也不返回计算出的百分比。通过添加return percent;

看看是否有帮助

相关问题