Я работаю над назначением с входным файлом, который необходимо поместить в параллельный массив. Хорошая новость заключается в том, что у меня нет синтаксических ошибок, но я получаю некоторые логические ошибки, которые наказывают меня. Целью программы является сортировка текстового файла, состоящего из боулинга. Есть две команды: синие и белые с таким же количеством игроков. Программа должна вывести победителя матча, суммируя свои баллы и отображая их индивидуальные баллы. Если вы можете дать мне какое-то направление, в котором я испортил, это очень помогло бы мне.Обработка параллельных массивов C++
UPDATE Я внесла некоторые изменения в свой код, но у меня есть одна последняя синтаксическая ошибка, которую я не могу понять. Он должен иметь дело с printArray. Если кто-то может помочь, я буду признателен.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int LENGTH = 10;
// declaration of functions sumArray() and printArray()
int sumArray(int [], int);
string printArray(string, string [], int [], int);
// declaration of main program
int main()
{
string blue_members[LENGTH], white_members[LENGTH];
int blue_scores[LENGTH], white_scores[LENGTH];
// 1) connect to the input file
ifstream fin("bowling.txt");
// declare arrays below
string Team, Member;
int Score;
// 2) initialize array accumulators to zero
int blue = 0;
int white = 0;
// 3) display a descriptive message
cout << "This program reads the lines from the file bowling.txt to determine\n"
<< "the winner of a bowling match. The winning team, members and scores\n"
<< "are displayed on the monitor.\n\n";
// 4) attempt to input the first line of the file
fin >> Member >> Team >> Score;
// 5) test ifstream.eof() condition
while (!fin.eof())
{
// 6) test team color is blue
if (Team == "Blue")
{
// 7) then store blue member and score
blue_scores[blue] = Score;
blue_members[blue] = Member;
// 8) increase blue array accumulator
blue++;
}
// 9) else store white member and score
else
{
white_scores[white] = Score;
white_members[white] = Member;
// 10) increase white array accumulator
white++;
}
// 11) attempt to input next line from file
fin >> Member >> Team >> Score;
}
// 12) if blue team score is larger
if (sumArray(blue_scores, blue) > sumArray(white_scores, white))
{
// 13 then display blue team as winner with the team
printArray(Team, blue_members, blue_scores, Score);
}
// 14) else display white team as winner with the team
else
{
printArray(Team, white_members, white_scores, Score);
}
}
// implement function sumArray() below
int sumArray(int array_name[], int array_end)
{
// 1. initialize accumulator to 0
int sum = 0;
// 2. loop over initialized array indices
for (int i = 0; i < array_end; i++)
// 3. increase accumulator by indexed array element
sum += array_name[i];
// 4. return accumulator
return sum;
}
// implement function printArray() below
void printArray(string team_name, string array_name [], int array_score [], int array_end)
{
// 1. display the team name as the winner
cout << setw(1) << "Winning Team: " << team_name<< endl;
cout << setw(5) << "Player" << setw(7) << "Score" << endl;
// 2. loop over initialized array indices
for (int i = 0; i < array_end; i++)
{
// 3. display member and score for that array index
cout << setw(3) << array_name[i]
<< setw(6) << setfill(' ') << array_score[i]
<< endl;
}
}
Имран, это растягивает цель этого сайта немного, чтобы задать такой широкий вопрос. Но, возможно, что если вы добавите дополнительную информацию, объясняющую то, что вы ожидаете и что вы на самом деле получаете, ИЛИ, упростите программу до сути проблемы, которую вы имеете, вы можете получить полезные ответы. –
Было бы неплохо, если бы вы ответили на ваши вопросы. – 1000ml