C++ 无法在for循环中显示数组中的正确百分比或总数

C++ 无法在for循环中显示数组中的正确百分比或总数,c++,arrays,for-loop,percentage,C++,Arrays,For Loop,Percentage,我的程序遇到的问题是,首先,当我计算百分比时,它没有将数组中的所有元素相加到一个总数中,然后从中跳出。我试着把总数+=百分比[I];在一个嵌套的for循环中,但它只给了我负的% 另外,我最后的总数不会显示任何内容。起初,我在main()中定义了它和所有函数,但它什么也没做。即使在改变之后,它也不起作用。还有,最后一件事,我的文件有20项,但循环只读取19项。如果我改为20,它就会崩溃 #include <iostream> #include <fstream> #incl

我的程序遇到的问题是,首先,当我计算百分比时,它没有将数组中的所有元素相加到一个总数中,然后从中跳出。我试着把总数+=百分比[I];在一个嵌套的for循环中,但它只给了我负的%

另外,我最后的总数不会显示任何内容。起初,我在main()中定义了它和所有函数,但它什么也没做。即使在改变之后,它也不起作用。还有,最后一件事,我的文件有20项,但循环只读取19项。如果我改为20,它就会崩溃

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void inputValues(string names[], int votes[])
{

    ifstream inputfile;

    inputfile.open("votedata.txt");

    if(inputfile.is_open())
    {
         for(int i = 0; i < 19; i++)
         {
             inputfile >> names[i] >> votes[i];
         }
    }
}

double *calcPercents( double percents[], int votes[], double total)
{
    for(int i = 0; i < 19; i++)
    {
        percents[i] = votes[i];

        total += percents[i];

        percents[i] = (percents[i]/total)*100;
    }

    return percents;
}

string determineWinner(string names[], double percents[])
{
    double temp = 0;
    string winner;
    for(int i = 0; i < 19; i++)
    {
        if(percents[i] > temp)
        {
            temp = percents[i];
            winner = names[i];
        }
    }
    return winner;
}

void displayResults(string names[], int votes[], double percents[])
{
    int total = 0;
    calcPercents(percents, votes, total);
    cout << "Candidate  Votes Received  % of Total Votes " << endl; 
    for(int i = 0; i < 19; i++)
    {
        cout << names[i] << "       " << votes[i] << "      " << percents[i] << "%" << endl;
    }
    cout << "Total " << total << endl;

    cout << " The winner of the election is " << determineWinner(names, percents) << endl;

}

int main()
{
  string names[19], winner;
  int votes[19];
  double percents[19];

  inputValues(names, votes);
  displayResults(names, votes, percents);
}

等等。

如果必须使用数组而不是
std::vector
s,则应将它们的大小传递给使用它们的函数。一种方法是使用常量设置大小,如下所示:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;  // bad practice

const int size = 20;

void inputValues(string names[], int votes[], int n);
// add the size as a parameter of the function ^^^^

int calcPercents( double percents[], int votes[], int n );
//^^ I'll explain later why I changed your signature ^^^

string determineWinner(string names[], double percents[], int n );
void displayResults(string names[], int votes[], double percents[], int n);

int main()
{
    // It's always better to initialize the variables to avoid undefined behavior
    // like the negative percentages you have noticed
    string names[size] ="";
    int votes[size] = {0};
    double percents[size] = {0.0};

    inputValues(names, votes, size);
    displayResults(names, votes, percents, size);
}
#包括
#包括
#包括
使用命名空间std;//坏习惯
常数int size=20;
无效输入值(字符串名称[],整数投票[],整数n);
//将大小添加为函数的参数^^^^
国际计票率(百分之二[],国际票数[],国际n);
//^^我稍后会解释我为什么更改你的签名^^^
字符串确定整数(字符串名称[],双百分比[],整数n);
无效显示结果(字符串名称[],整数投票[],双百分比[],整数n);
int main()
{
//最好初始化变量以避免未定义的行为
//比如你注意到的负百分比
字符串名称[大小]=“”;
整数投票[大小]={0};
双百分比[大小]={0.0};
输入值(名称、投票、大小);
显示结果(名称、投票、百分比、大小);
}
要计算百分比,可以使用两个循环,一个用于求和,另一个用于获取百分比。在您的函数中,您将total作为参数按值传递,因此它将被复制,并且所做的更改在函数外部永远不可见。我选择返回该值,即使这样做函数的名称会有点误导:

int calcPercents( double percents[], int votes[], int n )
{
    int total = 0;
    for(int i = 0; i < n; i++)
    // note the bound ^^^
    {
        total += votes[i];
    }
    double factor = 100.0 / total;
    for(int i = 0; i < n; i++)
    {
        percents[i] = factor * votes[i];
    }
    return total;
}
int-calcPercents(百分之二[],int-vots[],int-n)
{
int-total=0;
对于(int i=0;i
您还应该向输入函数添加一些检查,我只添加了size参数。请注意,初始化阵列后,即使读取文件失败,阵列也不包含随机值:

void inputValues(string names[], int votes[], int n)
{

    ifstream inputfile;

    inputfile.open("votedata.txt");

    if(inputfile.is_open())
    {
        for(int i = 0; i < n; i++)
        {
            inputfile >> names[i] >> votes[i];
        }
    }
}
void输入值(字符串名称[],整数投票[],整数n)
{
ifstream输入文件;
open(“votedata.txt”);
if(inputfile.is_open())
{
对于(int i=0;i>姓名[i]>>投票[i];
}
}
}
我也会改变决定胜利者的函数:

string determineWinner(string names[], double percents[], int n )
{
    if ( !n ) 
    {
        return "";
    }
    double max = percents[0];
    // update an index instead of a string
    int winner = 0;              
    for( int i = 1; i < n; i++ )
    {
        if( percents[i] > max )
        {
            max = percents[i];
            winner = i;
        }
    }
    return names[winner];
}
string determineWinner(字符串名称[],双百分比[],整数n)
{
如果(!n)
{
返回“”;
}
双倍最大值=百分比[0];
//更新索引而不是字符串
int=0;
对于(int i=1;i最大值)
{
最大值=百分比[i];
获胜者=我;
}
}
返回名称[获胜者];
}
对于最后一个函数,只需记住添加大小:

void displayResults(string names[], int votes[], double percents[], int n)
{
    int total = calcPercents(percents, votes, n);
    cout << "Candidate  Votes Received  % of Total Votes " << endl; 
    for( int i = 0; i < n; i++ )
    {
        cout << names[i] << "       " << votes[i] << "      "
             << percents[i] << "%" << endl;
    }
    cout << "Total " << total << endl;

    cout << " The winner of the election is "
         << determineWinner(names, percents,n) << endl;    
}
void displayResults(字符串名称[],整数投票[],双百分比[],整数n)
{
int total=计算百分比(百分比、票数、n);

这可能是因为您将变量
total
作为值传递给
calcPercents()
,然后尝试在
displayResults()
中使用它,但是
percents
数组的值是
0
相同的
void displayResults(string names[], int votes[], double percents[], int n)
{
    int total = calcPercents(percents, votes, n);
    cout << "Candidate  Votes Received  % of Total Votes " << endl; 
    for( int i = 0; i < n; i++ )
    {
        cout << names[i] << "       " << votes[i] << "      "
             << percents[i] << "%" << endl;
    }
    cout << "Total " << total << endl;

    cout << " The winner of the election is "
         << determineWinner(names, percents,n) << endl;    
}