【问题标题】:How to add scores to the names they correspond to如何将分数添加到它们对应的名称
【发布时间】:2015-04-13 03:19:27
【问题描述】:

我正在编写一个代码,它使用函数(而不是数组)来创建一个美国偶像风格投票系统的程序。我没有使用数组,因为我确实需要先掌握函数。

每位参赛者有 5 票,将最高和最低的票数去掉,最后 3 票平均得到答案,谁的平均票数最高者获胜。但是,参赛者的数量取决于用户输入(可能有无限数量的参赛者)

我不知道如何获得每个学生的平均值,只是作为一个整体,到目前为止,我的程序只将最后输入的值作为获胜者。这是我的代码。

如果对如何获得不同参赛者的不同平均值有任何帮助,请告诉我,我是函数新手,对 c++ 还很陌生

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void validCheck();
void calcAvgCheck();
void findHigh();
void findLow();

ofstream outputFile;
ofstream inputFile;
double totalScore, average, score;
string name;

int main(){
    int judge = 1;
    outputFile.open("contestant.txt");
    while (name != "done" || name != "Done"){
        cout << "Enter Contestant Name, if no more, type 'done': ";
        cin >> name;

        outputFile << name << " ";

        if (name == "done" || name == "Done"){ break; }
        for (judge = 1; judge < 6; judge++){
            cout << "Enter score " << judge << " ";
            validCheck();
            outputFile << " " << score << " ";
            calcAvgCheck();
        }
    }

    cout << "Winner is: " << name << "with a score of: " << average;
    system("pause");
    return 0;
}

void validCheck(){
    cin >> score;
    if (score < 1 || score > 10){
        cout << "Please Enter a score between 1 and 10: ";
        cin >> score;
    }
    totalScore += score;
}

void calcAvgCheck(){
    inputFile.open("contestant.txt");

    average = totalScore / 5;
}

【问题讨论】:

  • 只要你使用c++,你应该避免杂散函数。请将所有全局成员保持为class members,并将全局函数保持为class functions,然后您可以使用对象的类数组并将每个数组成员视为单独的参赛者
  • @Youssef:这个“一切都必须是成员函数”的神话不会消失,对吗?免费函数并没有错,在许多情况下,they increase encapsulation

标签: c++ function loops for-loop sum


【解决方案1】:

函数 calcAvgCheck(); 应该在 for 循环之外调用。

您可以将分数保存在向量中,并且可以重复用于计算平均值,如下所示。

请注意,定义全局值不是一个好习惯。我知道还有更多的工作要做,这应该是你的功课。请从这里开始,如果您有更多问题,请发布一个不同的问题作为此问题的继续。

#include <iostream>
#include <string>
#include<vector>


using namespace std;

void validCheck();
double calcAvgCheck(std::vector<int>& scores);
void findHigh();
void findLow();


double totalScore, score;
string name;

int main(){
    int judge = 1;
    vector<int> scores;
    double  average =0.0;

    while (name != "done" || name != "Done"){
        cout << "Enter Contestant Name, if no more, type 'done': ";
        cin >> name;

        if (name == "done" || name == "Done"){ break; }
        for (judge = 1; judge < 6; judge++){
            cout << "Enter score " << judge << " ";
            validCheck();
            scores.push_back(score);
        }
        average =calcAvgCheck(scores);
        std::cout<< " average:"<< average<<"\n";
    }

    cout << "Winner is: " << name << "with a score of: " << average;
    system("pause");
    return 0;
}

void validCheck(){
    cin >> score;
    if (score < 1 || score > 10){
        cout << "Please Enter a score between 1 and 10: ";
        cin >> score;
    }
    totalScore += score;
}
double  calcAvgCheck( std::vector<int>& scores)
 {
        int sum=0;
        for(auto &x : scores)
           sum+=x;
        return sum/scores.size();
 }

【讨论】:

  • 我应该使用我的文件吗?我正在使用它们来尝试读回信息,向量会做得更好吗?
  • 我希望您可能更愿意将个人乐谱保存在文件中,如果您不关心,则此程序中不需要文件
  • 那么我不需要它,我不在乎将其保存到文件中,但我在编辑代码时遇到了一些调试错误,让我发布它们...警告 C4244: 'argument' : 从 'double' 转换为 'const int',可能丢失数据错误 C2660: 'calcAvgCheck' : function does not take 1 arguments
  • 目前编辑的代码:for (judge = 1;judge > score; if (score 10){ cout > score;} totalScore += score;} double calcAvgCheck(vector& scores){ int sum = 0; for (auto &x : 分数) sum += x;返回总和/scores.size();}
  • @Ace Ebert 我重写了我的答案,如果有帮助请接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
相关资源
最近更新 更多