【问题标题】:Not outputting the calculation, just 0不输出计算,只有 0
【发布时间】:2016-10-05 07:29:56
【问题描述】:

我在这里有我的代码并且它运行,但是,当我尝试输出它只输出 0 的百分比时,我花了很长时间试图弄清楚我缺少什么,但我一无所知。基本上,我试图输出每个候选人在总票数中的百分比。任何帮助,将不胜感激。这是我的输出; Output display 另外,我知道获胜者会遍历每个用户,直到由于某种原因到达终点,仍在努力解决问题。

这是我的代码 -

#include <iostream>
#include <string>
#include <iomanip>


using namespace std;

class candidatesElection
{
public:

    string last;
    float votePercent;
    void winnerOfElection();
    void outputDis();
    int total = 0;
};

int main()
{

    string lastName[5];
    int amountOfVotes[5];
    double percentTotal[5];
    int total = 0;
    int winnerNo = 0;
    int winningCandidate;
    string winningName;


    for (int i = 0; i < 5; i++)
    {
        cout << "Enter the last name of the Candidate: " << endl;
        cin >> lastName[i];
        cout << endl;

        cout << "Enter the votes received by the Candidate: " << endl;
        cin >> amountOfVotes[i];


        total += amountOfVotes[i];
        cout << "Total number of votes is: " << total << endl;
    }


    for (int i = 0; i < 5; i++)
    {
        if (amountOfVotes[i] > amountOfVotes[winnerNo]) winnerNo = i;
        amountOfVotes[i] = amountOfVotes[winnerNo];
    }


    for (int i = 0; i < 5; i++)
    {
        percentTotal[i] = (amountOfVotes[i] / total) * 100.0; // need to make it floating point
    }

    void outputDis();
    {
        cout << endl << left << setw(25) << "Candidate" << right << setw(25) << "Votes Received" << setw(25) << "% of Total Votes" << endl;


        for (int i = 0; i < 5; i++)
            cout << endl << left << setw(25) << lastName[i] << right << setw(25) << amountOfVotes[i] << setw(25) << percentTotal[i] << endl;
        cout << endl << left << setw(25) << "Total" << right << setw(25) << total << endl;


        for (int i = 1; i < 5; i++)
        {

            int winHigh = amountOfVotes[0];
            string win = lastName[0];
            if (amountOfVotes[i] > winHigh)
            {
                winHigh = amountOfVotes[i];
                win = lastName[i];
            }
            cout << "The Winner of the Election is " << win << endl;

        }
    }

        system("pause");



};

【问题讨论】:

    标签: c++


    【解决方案1】:

    (amountOfVotes[i] / total) * 100.0 中的系数amountOfVotes[i] / total整数运算 中求值:即丢弃任何分数。

    所以对于amountOfVotes[i] 小于total 的所有情况,您最终都会得到0 * 100

    解决方案是将公式重新排列为100 * amountOfVotes[i] / total;,或者更好的是100.0 * amountOfVotes[i] / total;,这将强制以双精度浮点计算 - 您有溢出int 的危险,在某些系统上,它可能有上限低至 32767。

    即使使用逐行调试器,这也不是很明显。但请务必使用该调试器来解决其他“问题”。

    【讨论】:

    • 谢谢,这更有意义!但是,当我更改它时,由于某种原因,它会输出一些相同的值。 IE。我输入分数 34, 56, 78, 96, 45 它输出 34, 56, 78, 96, 96?
    • 我认为,这是因为您在最终 for 循环的 body 中重新分配了 int winHigh = amountOfVotes[0];。请注意,您的输出是单调递增的,而当您输入的分数降低时,就会出现偏差。
    • 忽略我我刚刚意识到我的代码输出“候选分数”错误!它出于某种原因重复了一些值?您的解决方案适用于该百分比,谢谢!
    • 谢谢!如果我把它从身体上取下来,你认为它应该放在哪里?现在运行一些检查。 (当它允许我时也会标记你的最佳答案)
    • for 循环开始之前。
    猜你喜欢
    • 2021-04-29
    • 2015-04-30
    • 1970-01-01
    • 2022-12-31
    • 2017-01-27
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多