2016-03-22 2 views
0

私のプログラムで問題が発生しているのは、まず%を計算すると配列のすべての要素を合計してダイビングしていないことです。私は合計+ =パーセントを入れようとしました[i];ネストされたforループでは、それは私に負の%を与えました。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); 
} 

マイファイルのスタイルである:

bob (tab) 1254 

joe (tab) 768 

など

+0

これは、値として 'calcPercents()'に変数 'total'を渡すためです。そして、あなたは 'displayResults()'の中でそれを使用しようとしますが、値は '0'です – DimChtz

+0

' percents'の配列と同じです – DimChtz

答えて

0

あなたの代わりにstd::vector Sの配列を使用する必要がある場合は、それらを使用して関数にそのサイズを渡す必要があります。あなたがパーセントを取得するために、2つのループ、合計用とその他を使用することができる割合を計算するには

#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); 
} 

:一つの方法は、このように、定数を使用してサイズを設定することです。あなたの関数ではtotalをパラメータとして値渡しするので、コピーされ、変更は関数の外では決して見えません。私はそうする関数の名前が紛らわしいlitleなっても、そのvauleを返すために選択します。

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; 
} 

あなたはまた、私は唯一のサイズパラメータを追加し、入力機能にいくつかのチェックを追加する必要があります。それは配列がランダムな値が含まれていないファイルの読み込み失敗しても、配列を初期化したことに注意してください:について

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]; 
} 

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 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;  
} 
関連する問題