私のコードはここにありますが、実行すると出力が0になりますが、何が欠けているのか分かりません。私は無知だ。基本的には、私は投票総数のうち各候補者の投票率を出力しようとしています。どんな助けもありがとう。ここに私の出力です。 Output displayまた、勝者は何らかの理由で最後に到達するまですべてのユーザーをループし、まだキンクを解決しようとしています。すなわち、任意の画分は廃棄される:(amountOfVotes[i]/total) * 100.0
で計算結果を出力しない、ちょうど0
#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");
};
ありがとうございました!しかし、私はそれを変更したとき、何らかの理由で同じ値としていくつかの値を出力します。すなわち、私はスコア34,56,78,96,45を入力し、それは34,56,78,96,96を出力するか? –
これは、最終的な 'for'ループの* body *に' int winHigh = amountOfVotes [0]; 'を再割り当てしたためです。あなたの出力は単調に増加していることに注意してください。スコアが減少すると、偏差が発生します。 – Bathsheba
私はちょうど私のコードが間違って "候補得点"を出力していることを認識しています!何らかの理由で値が繰り返されていますか?あなたのソリューションは、パーセントのために働いた、ありがとう! –