如果发现两个数字相等,那么如何输入第一个数字

时间:2015-07-25 12:10:37

标签: c++ function loops

我已经完成了我的所有程序,除了最后一步之外应该如何工作。

结束时,“胜利者”是一名'和' winnerName'如果多个名称具有相同的平均值,则会将所有这些名称输出到屏幕上。

我想知道的是,是否有办法只输入输入该平均值的名字输出到屏幕。而不是所有适用的。

#include <iostream>
#include <string>   //for name entry of contestants
#include <iomanip>  //rounding and point perceision
#include <fstream>  //write names and averages to a file for storage

using namespace std;

double calcAvgScore(int, int, int, int, int);   //calculates the average after the highest and lowest scores are found 
double findHighest(int, int, int, int, int);    //finds the highest of the 5 scores 
double findLowest(int, int, int, int, int);     //finds the lowest of the 5 scores

int main()
{
string cName;   //name of the contestant
int jScore1, jScore2, jScore3, jScore4, jScore5;    //scores of each judge
double AvgScore;    //average of the scores
ofstream outFile;

outFile.open("NamesAndAverage.txt");    //creation and opening of text file where the names and averages will be stored

cout << "All judges' scores are from 1 to 10" << endl;
cout << " " << endl;

for (int count = 1;; count++)   //loop to keep track of number of Contestants and exits when Done is entered
{
    cout << " " << endl;
    if (count = 1)
        cout << "Please enter the name of Contestant #" << count << ". If there are no Contestants enter ""Done""" << endl;
    else
        cout << "Please enter the name of Contestant #" << count << ". If there are no more Contestants please enter ""Done""" << endl;
    cin >> cName;
    if (cName == "Done")
        break;

    cout << " " << endl;

    cout << "Please enter the score from Judge 1 ";
    cin >> jScore1;
    while (jScore1 < 1 || jScore1 > 10)
    {
        cout << "scores must be from 1 to 10" << endl;
        cout << "Please re-enter the score from Judge1 ";
        cin >> jScore1;
    }
    cout << "Please enter the score from Judge 2 ";
    cin >> jScore2;
    while (jScore2 < 1 || jScore2 > 10)
    {
        cout << "scores must be from 1 to 10" << endl;
        cout << "Please re-enter the score from Judge2 ";
        cin >> jScore2;
    }
    cout << "Please enter the score from Judge 3 ";
    cin >> jScore3;
    while (jScore3 < 1 || jScore3 > 10)
    {
        cout << "scores must be from 1 to 10" << endl;
        cout << "Please re-enter the score from Judge3 ";
        cin >> jScore3;
    }
    cout << "Please enter the score from Judge 4 ";
    cin >> jScore4;
    while (jScore4 < 1 || jScore4 > 10)
    {
        cout << "scores must be from 1 to 10" << endl;
        cout << "Please re-enter the score from Judge4 ";
        cin >> jScore4;
    }
    cout << "Please enter the score from Judge 5 ";
    cin >> jScore5;
    while (jScore5 < 1 || jScore5 > 10)
    {
        cout << "scores must be from 1 to 10" << endl;
        cout << "Please re-enter the score from Judge5 ";
        cin >> jScore5;
    }

    double AvgScore = calcAvgScore(jScore1, jScore2, jScore3, jScore4, jScore5);    //call to function
    outFile << cName << " " << AvgScore;    //writing variables to the file
}
outFile.close();


cout << " " << endl;
cout << " "<<endl;

double winnerScore = 0;     //variable for the highest average
string winnerName = "Null"; //variable for name with highest average
ifstream inFile;
inFile.open("NamesAndAverage.txt");

while (inFile >> cName >> AvgScore) //reading variables from file
{
    if (AvgScore > winnerScore)
        winnerScore = AvgScore;

    if (AvgScore == winnerScore)
        winnerName=cName;

}
cout << "The winner is " << winnerName << " with a score of " << setprecision(2) << fixed << winnerScore << endl;


system("pause");
return 0;
}




double calcAvgScore(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double highest = findHighest(jScore1, jScore2, jScore3, jScore4, jScore5);
double lowest = findLowest(jScore1, jScore2, jScore3, jScore4, jScore5);
double total = (jScore1 + jScore2 + jScore3 + jScore4 + jScore5 - highest - lowest);
double AvgScore = (total / 3);
cout << "inside Average function Avg= " << AvgScore << endl;
return AvgScore;
}


double findHighest(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double highest = 0;
if (jScore1 > highest)
    highest = jScore1;
if (jScore2 > highest)
    highest = jScore2;
if (jScore3 > highest)
    highest = jScore3;
if (jScore4 > highest)
    highest = jScore4;
if (jScore5 > highest)
    highest = jScore5;
cout << "inside highest function highest = " << highest << endl;
return highest;
}


double findLowest(int jScore1, int jScore2, int jScore3, int jScore4, int jScore5)
{
double lowest = 10;
if (jScore1 < lowest)
    lowest = jScore1;
if (jScore2 < lowest)
    lowest = jScore2;
if (jScore3 < lowest)
    lowest = jScore3;
if (jScore4 < lowest)
    lowest = jScore4;
if (jScore5 < lowest)
    lowest = jScore5;
cout << "inside lowest function lowest= " << lowest << endl;
return lowest;
}

1 个答案:

答案 0 :(得分:1)

改变这个:

if (AvgScore > winnerScore)
    winnerScore = AvgScore;

if (AvgScore == winnerScore)
    winnerName=cName;

为:

if (AvgScore > winnerScore)
{
    winnerScore = AvgScore;
    winnerName = cName;
}

应该做的 - 换句话说,除非你也在更新分数,否则不要更新名称。如果分数相等,则找到的FIRST将是“胜利者”。