繁体   English   中英

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

[英]How do I calculate and display the correct percentage?

我是 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: 在底部,你可以看到酷儿号码

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

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计算的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. int crew_count然后你喂了double menu(float crew_count)
  2. 您的菜单功能不会返回任何其认为无效的内容,您的欢迎功能也不会返回任何内容
  3. 此外,您calculatePct不会返回计算出的百分比。 通过增加return percent;做到这一点return percent;

看看有没有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM