0

I am reading data from a text file and running the code below. However, I am getting random numbers in mu output along with the correct numbers. I can't figure out where I am getting these random numbers from. My text file includes the following: Fred Blue 20 Harry Blue 35 Tony White 43 Hilda Blue 12 Paul White 34 Tom White 20

My output reads as: This program reads the lines from the file bowling.txt to determine the winner of a bowling match. The winning team, members and scores are displayed on the monitor. winning team: white Player Score Tony 43 Paul 34 Tom 20 0 4197592 0 -1501340552 62 6298312 0

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



// 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[10], white_members[10];
  int blue_scores[10], white_scores[10];

  // 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("Blue", blue_members, blue_scores, 10);
    }

  // 14) else display white team as winner with the team
  else
    {
    printArray("White", white_members, white_scores, 10);
    }
}

// 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;
    }
}
2
  • You should edit the question to include your output as text, not an image. However, you should look at the number you're passing to print_array for the length of the array to print. Commented Jul 18, 2020 at 19:23
  • Thank you! And I will include my output as text. Commented Jul 18, 2020 at 19:29

1 Answer 1

1

The problem is that you are asking the program to print out all 10 members of the array when you don't have 10 valid entries in the array. That's why you get random numbers at the end.

You need to change (for example)

printArray("White", white_members, white_scores, 10);

to

printArray("White", white_members, white_scores, white);

The variable white is the number of valid entries you have in the white_members and white_scores arrays.

Same change for the blue arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.