防止打印失败检查

时间:2021-02-18 06:35:53

标签: c++

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

bool check(int count[], string race){
  for(int i = 0; i < ('Z' + 1); i++){
   if(count[i]!= 0 && (count[race[0]] != count[i])) return false;
  }
  return true;
}

void score(string race){
  int numteams = 0;
  int scores['Z' + 1] = {0};
  int count['Z' + 1] = {0};

  for(int i = 0; i < race.length(); ++i){
    if(scores[race[i]] == 0){
      numteams++;
    }
    scores[race[i]] = i + 1 + scores[race[i]];
    count[race[i]]++;
  }

  if(check(count, race) != true){
    cout << "The teams are not equal, please re-enter your values: ";
    cin >> race;
    score(race);
     
  }
  
  cout << "There are " << numteams << " teams." << endl;
  cout << "There are " << count[race[0]] << " runners on each team." << endl;

  double avg = (scores[race[0]])/(count[race[0]] * 1.0);
  char win = race[0];
  for(int i = 'A'; i < ('Z' + 1); i++){
    if(scores[i]!=0){
      cout << "Average for team " << (char)(i) << " is: " << scores[i]/(count[i] * 1.0) << endl;
      if(avg > (scores[i])/(count[i] * 1.0)){
        avg = (scores[i])/(count[i] * 1.0);
        win = i;
      }
    }
  }
  cout << "The winning team is '" << win << "' with a score of " << avg << endl;
  cout << endl;
}



int main() {
  string race;
  cout << "Enter the outcome of the race (in all uppercase letters): ";
  cin >> race;
  while(race != "done"){
    score(race);
    cout << "Enter the outcome of the race (in all uppercase letters): ";
    cin >> race;
  }
  return 0;
}

任务是让用户在不知道比赛中有多少团队和参与者的情况下输入比赛结果。我已经完成了大部分工作并且工作正常,唯一的问题是当字符串通过检查功能。该字符串一旦被纠正就会失败,它会通过其余代码但之后,被拒绝的字符串也是如此。我怎样才能让错误的字符串被完全忽略?

1 个答案:

答案 0 :(得分:0)

您首先存储字符串,然后进行检查。
而是先检查并仅在没有失败时才存储。