如何计算和显示正确的百分比? 我是C++ NoOB < /P>

如何计算和显示正确的百分比? 我是C++ NoOB < /P>,c++,C++,我正在努力为学校制作这个基于文本的游戏,但我在显示正确的百分比方面遇到了问题。我想这与我在程序中如何计算有关,或者我把函数搞砸了 如果能得到一些帮助,我将不胜感激。干杯 #include <string> #include <cmath> #include <iostream> using namespace std; double menu (float crew_count); double calculatePct(float crew_count,

我正在努力为学校制作这个基于文本的游戏,但我在显示正确的百分比方面遇到了问题。我想这与我在程序中如何计算有关,或者我把函数搞砸了

如果能得到一些帮助,我将不胜感激。干杯

#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";

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

您混合了整数、浮点和void

int crew_count然后你输入了两个menufloat crew_count。 你的菜单功能不会返回任何无效的内容,欢迎功能也是如此 此外,calculatePct不会返回计算的百分比。通过增加收益率来实现这一点;
<>看看它是否有助于

标签不在C++中形成块。你需要牙套。浮动百分比=calculatePctcrew_计数,4;我建议大家总是使用可选的括号来帮助人们学习像C++这样使用括号的语言。如果大括号总是在适当的位置,那么这个bug很难被忽略。另外,在这个函数中定义了两个不同的百分比变量。如果你去掉了第一个多余的函数,那么你将得到一个编译器错误,这将为你指明正确的方向。此外:calculatePct甚至不返回任何东西,即使它是非void函数。因此,即使调用它,也会调用未定义的行为。
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