【问题标题】:C++ program to display votes in percentage not showing correct resultC ++程序以百分比显示投票未显示正确结果
【发布时间】:2014-08-15 07:50:30
【问题描述】:

我正在从电子书中解决一些 C++ 问题。我制作了这个 C++ 程序,但它不能正常工作。我有两个问题:

  1. 即使应用了 forumla (votePercentage = firstAnswer/totalVotes*100;),它也没有显示输出,而只有 0。

  2. 程序应该显示条形图,我应该怎么做?任何提示、参考或解决方案将不胜感激。

这是我的代码:

/*
 * Write a program that provides the option of tallying up the
 * results of a poll with 3 possible values.
 * The first input to the program is the poll question;
 * the next three inputs are the possible answers.
 * The first answer is indicated by 1, the second by 2, the third by 3.
 * The answers are tallied until a 0 is entered.
 * The program should then show the results of the poll—try making
 * a bar graph that shows the results properly scaled to fit on
 * your screen no matter how many results were entered.
 */

#include <iostream>
#include <string>

void startPoll (void);
void showPoll (void);
void pollCheck (void);

std::string pollQuestion, answer1, answer2, answer3;
int       pollChoice, firstAnswer, secondAnswer, thirdAnswer;


int main (void)
{
    int totalVotes = 1;
    float votePercentage;

    startPoll();
    showPoll();

    for(;;totalVotes++)
    {
        if      (pollChoice == 1)
        {
            firstAnswer = firstAnswer + 1;
        }
        else if (pollChoice == 2)
        {
            secondAnswer++;
        }
        else if (pollChoice == 3)
        {
            thirdAnswer++;
        }
        else
        {
            std::cout << "==============*======*======*==============\n"
                      << "                   RESULT                  \n"
                      << "==============*======*======*==============\n"
                      << "Question: " << pollQuestion << "\n"
                      << "Total Votes: " << totalVotes << "\n";
            votePercentage = (firstAnswer/totalVotes)*100;
            std::cout << answer1 << ": " << firstAnswer  << " votes.       | " << votePercentage << "\n";

            votePercentage = secondAnswer/totalVotes*100;
            std::cout << answer2 << ": " << secondAnswer << " votes.       | " << votePercentage << "\n";

            votePercentage = thirdAnswer/totalVotes*100;
            std::cout << answer3 << ": " << thirdAnswer  << " votes.       | " << votePercentage << "\n";

            return 0;
        }
        std::cout << "\nEnter your vote again\nOR\nuse 0 to show the results.\n";
        std::cin >> pollChoice;

    }

    std::cout << "Error: Something went wrong!\n";
}

void startPoll (void)
{
    std::cout << "Enter your poll question:\n";
    getline (std::cin, pollQuestion, '\n');

    std::cout << "Enter answer 1:\n";
    getline (std::cin, answer1, '\n');
    std::cout << "Enter answer 2:\n";
    getline (std::cin, answer2, '\n');
    std::cout << "Enter answer 3:\n";
    getline (std::cin, answer3, '\n');

}

void showPoll (void)
{
    std::cout << "==============|======|======|==============\n"
              << "                    POLL                   \n"
              << "==============|======|======|==============\n"
              << pollQuestion << "\n"
              << "1. " << answer1 << "\n"
              << "2. " << answer2 << "\n"
              << "3. " << answer3 << "\n\n"
              << "Enter 1,2 or 3:\n\n";
    std::cin  >> pollChoice;
    pollCheck();
}

void pollCheck (void)
{
    if (pollChoice != 1  &&  pollChoice != 2  &&  pollChoice != 3)
    {
        std::cout << "Wrong choice entered! Please try again.\n\n";
                return showPoll();
    }
}

【问题讨论】:

  • 调试你的程序。要么打印变量和检查点,要么使用调试器。
  • firstAnswer/totalVotes 是整数除法。至于条形图,只需为每个选项打印一行,每 2 个百分点(或您选择的不同数量)使用 +* 或您选择的其他字符。

标签: c++ logic


【解决方案1】:

您需要注意integer/integer = integer。在你的情况下,改变

(firstAnswer/totalVotes)*100

(1.0*firstAnswer/totalVotes)*100

(firstAnswer*100.0/totalVotes)

应该可以。它们都给出浮点结果。

【讨论】:

  • 最后一个选项将给出整数值。使用firstAnswer*100.0/totalVotes 将使其成为浮点值。
  • 是的,四舍五入不正确,我编辑了我的帖子。感谢您的提示。
【解决方案2】:

好吧,条形图的解决方案可能如下:(不是我写的)我认为这是非常自我解释,因为它真的很基本

void line (int n, char c)
{
  // this is the loop for n
  for (int i = 0; i < n; i++)
    cout << c << endl;
}

【讨论】:

  • 如果您现在重新添加链接作为正确的属性,那就更好了。
  • 谢谢,我们终于做到了:)
【解决方案3】:

这是我的解决方案,您可以通过阅读 cmets 来了解我是如何制作条形图的。

#include <iostream>
using namespace std;

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;

    cout << "What is your favorite animal? 1 Cat, ";
    cout <<"2 Dog, 3 Fish, 0 Count votes" << endl;

    //Choice counter
    while (true)
    {
        int choice;
        cout << "Choice: ";
        cin >> choice;

        if(choice == 1)
            a++;
        else if(choice == 2)
            b++;
        else if(choice == 3)
            c++;
        else if(choice == 0)
            break;
        else
            continue;
    }

    cout << endl << "  1: " << a << endl;
    cout << "  2: " << b << endl;
    cout << "  3: " << c << endl;
    cout << endl << "1\t" << "2\t" << "3\t" << endl;

    //Finds the max voted option
    int max = 0;
    if(a > b && a > c)
        max = a;
    else if(b > c && b > a)
        max = b;
    else if(c > a && c > b)
        max = c;

/*  If the max voted option is bigger than 10, find by how much
    we have to divide to scale the graph, also making 10 bar
    units the max a bar can reach before scaling the others too  */
    int div =2;
    if(max > 10)
    {
        do
        {
            max = max/div;
            if(max < 10)
                break;
            div++;
        }while(true);
    }else
        div = 1;

    //Sets the final number for the bars
    a=a/div;
    b=b/div;
    c=c/div;
    if(a==0)
        a++;
    if(b==0)
        b++;
    if(c==0)
        c++;

    //Creates the bars
    while(true)
    {
        if(a>0)
        {
            cout << "[]" << "\t";
            a--;
        }else
            cout << "  ";
        if(b>0)
        {
            cout << "[]" << "\t";
            b--;
        }else
            cout << "  ";
        if(c>0)
        {
            cout << "[]" << "\t";
            c--;
        }else
            cout << "  ";
        cout << endl;
        if(a==0 && b==0 && c==0)
            break;
    }

}

【讨论】:

  • 欢迎来到 Stackoverflow!因为我们希望这个网站尽可能地成为有用的资源,所以最好提供解释为什么您的代码可以解决问题,以及您的代码。
  • 嗯,解释在 cmets 里面,代码也在里面……?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多