Home > Net >  My code's display for reading from a file is not working as intended
My code's display for reading from a file is not working as intended

Time:01-14

So for my program It is suposed to read from a file and display my information but the display is misbehaving. I have asked my collegues and they are unsure and I even asked my professor but he was unsure of what was causing the error. If anyone could help me I would be very very thankful and honnostly amazed



//LIBRARIES
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <thread>
#include <stdexcept>
#include <limits>
#include <cctype>
using namespace std;


//STORED ARRAY NAMES AND FUNCTION NAMES
int fillArrays (string teamWin[], string teamLose[],
        float theScore[], ifstream & final);
float pointAverage (float theScore[], float numberOfGames);
int teamsScoredForty (float theScore[], int gameNum);
int savedMax (float theScore[], int gameNum);
int savedMin (float theScore[], int gameNum);
void printStoredInformation (string teamWin[], string teamLose[],
         float theScore[], int gameNum);
void finalDisplay (float theScore[], string teamWin[],
         string teamLose[], int teamsOverForty,
         float averagePoints, int maxSavedInfo, int minSavedInfo);


//START OF MAIN
int
main ()
{

  ifstream final;

  float theScore[100];
  string teamWin[100];
  string teamLose[100];

  int gameNum;
  float numberOfGames;

  float averagePoints;

  int teamsOverForty;

  int maxSavedInfo;
  int minSavedInfo;

  gameNum = fillArrays (teamWin, teamLose, theScore, final);

  numberOfGames = gameNum;

  averagePoints = pointAverage (theScore, numberOfGames);

  teamsOverForty = teamsScoredForty (theScore, gameNum);
  maxSavedInfo = savedMax (theScore, gameNum);
  minSavedInfo = savedMin (theScore, gameNum);

  printStoredInformation (teamWin, teamLose, theScore, gameNum);
  finalDisplay (theScore, teamWin, teamLose, teamsOverForty,
          averagePoints, maxSavedInfo, minSavedInfo);


  return 0;
}


//FINAL DISPLAY OF INFORMATION
void
finalDisplay (float theScore[], string teamWin[],
        string teamLose[], int teamsOverForty[],
        float averagePoints, int maxSavedInfo, int minSavedInfo)
{
  cout << "AVERAGE = " << averagePoints << endl;
  cout << "TEAMS WHO SCORED OVER 40 POINTS IN ONE GAME = " << teamsOverForty << endl;
  cout << endl;
  cout << endl;
  cout << "TEAM WHO SCORED THE MOST POINTS IN ONE GAME" << endl;
  cout << "WINNERS = " << teamWin[maxSavedInfo] << endl;
  cout << "LOSERS = " << teamLose[maxSavedInfo] << endl;
  cout << "FINAL SCORE OF WINNERS" << theScore[maxSavedInfo] << endl;
  cout << endl;
  cout << endl;
  cout << "TEAM WHO SCORED THE LEAST POINTS AND STILL WON" << endl;
  cout << "WINNERS = " << teamWin[minSavedInfo];
  cout << "LOSERS = " << teamLose[minSavedInfo];
  cout << "FINAL SCORE OF WINNERS = " << theScore[minSavedInfo];
  cout << endl;
}


//INFORMATION STORAGE FOR ARRAYS
int
fillArrays (string teamWin[], string teamLose[],
        float theScore[], ifstream & final)
{
  int i = 0;
  final.open ("superbowl.dat");
  while (!final.eof ())
    {
      getline (final, teamWin[i]);
      getline (final, teamLose[i]);
      final >> theScore[i];
      final.ignore (1, '\n');
      i  = 1;
    }
  final.close ();
  return i;
}


//FUNCTION TO FIND AVERAGE OF THE TEAMS SCORE
float
pointAverage (float theScore[], float numberOfGames)
{
  float sum;
  float avg;

  for (int i = 0; i < numberOfGames; i  )
    {
      sum = sum   theScore[i];
    }
  avg = sum / numberOfGames;
  return avg;
}


//FUNCTION FOR ALL THE TEAMS WHO SCORED MORE THEN FORTY
int
teamsScoredForty (float theScore[], int gameNum)
{
  int teamSavedScoreOverForty = 0;

  for (int i = 0; i < gameNum; i  )
    {
      if (theScore[i] > 40)
    {
      teamSavedScoreOverForty = teamSavedScoreOverForty   1;
    }
    }
  return teamSavedScoreOverForty;
}


//FUNCTION TO FIND WHICH TEAM SCORED MORE THEN ANY OTHER TEAM AND WON
int
savedMax (float theScore[], int gameNum)
{
  int val = -1;
  int idx;

  for (int i = 0; i < gameNum; i  )
    {
      if (val < theScore[i])
    {
      val = theScore[i];
      idx = 1;
    }
    }
  return idx;
}


//FUNCTION TO FIND WHICH TEAM SCORED LESS THEN ANY OTHER TEAM AND WON
int
savedMin (float theScore[], int gameNum)
{
  int val = theScore[0];
  int idx;
  for (int i = 0; i < gameNum; i  )
    {
      if (theScore[i] < val)
    {
      idx = i;
    }
    }
  return idx;
}


//FINAL DISPLAY FUNCTION FOR ALL OF THE INFORMATION ABOVE
void
printStoredInformation (string teamWin[], string teamLose[],
        float theScore[], int gameNum)
{

  int a = 1;

  for (int i = 0; i < gameNum; i  )
    {

      cout << "SUPER BOWL " << a << endl;
      cout << "NAME OF WINNING TEAM - " << teamWin[i] << endl;
      cout << "NAME OF LOSING TEAM - " << teamLose[i] << endl;
      cout << "TOTAL POINTS SCORED BY WINNERS - " << theScore[i] << endl;
      cout << endl;
      a = a   1;
    }

}

That is the main code that it is having errors with.

PackersChiefs35PackersRaiders33JetsColts16ChiefsVikings23ColtsCowboys16CowboysDolphins24DolphinsRedskins14
DolphinsVikings24SteelersVikings16SteelersCowboys21RaidersVikings32CowboysBroncos27Broncos27SteelersVikings
35SteelersRams31RaidersEagles2749ersBengals26RedskinsDolphins27RaidersRedskings3849ersDolphins38Bears
Patriots46GiantsBroncos39RedskinsBroncos4249ersBengals2049ersBroncos55GiantsBills20RedskinsBills37Cowboys
Bills52CowboysBills3049ersChargers49CowboysSteelers27PackersPatriots35BroncosPackers31BroncosFalcons34
RamsTitans23RavensGiants34Patriotsrams20BuccaneersRaiders48PatriotsPanthers32PatriotsEagles24Steelers
Seahawks21ColtsBears29GiantsPatriots17SteelersCardinals27SaintsColts31PackersSteelers31GiantsPatriots
21Ravens49ers34SeahawksBroncos43PatriotsSeahawks28BroncosPanthers24PatriotsFalcons34EaglesPatriots41
PatriotsRams13Chiefs49ers31BuccaneersChiefs31RamsBengals23

Between Each word is an enter, I am just not alloud to put it in on this website sadly. It says it looks like Spam

I tried to mess with the 2 different displays I have and I tried to even add more functions to kep things more organized but it did not work. I also spent a few hours trying to debug it.

CodePudding user response:

In addition to the concerns expressed by other users in the comments, your output is broken because you have a typo in your input file on record 13. You repeated Bronco 27 twice and the file input function breaks because it expects two team names and then an integer. Instead it receives a team name followed by a number:

SUPER BOWL 12
NAME OF WINNING TEAM - Cowboys
NAME OF LOSING TEAM - Broncos
TOTAL POINTS SCORED BY WINNERS - 27

SUPER BOWL 13
NAME OF WINNING TEAM - Broncos
NAME OF LOSING TEAM - 27
TOTAL POINTS SCORED BY WINNERS - 0

//...

  • Related