Superbowl Array ?!?!!?!?!?!

Hi,

So I've been tasked via project to write an array using a data file. I know how to write basic beginner c++, but for some reason arrays are ALWAYS lost to me...

The prompt for this is to make it a 3 column array, the first being Winning Superbowl Teams, the second being Losing Superbowl Teams, and the third being the Winning Points Scored...which I'm completely lost on.

If anyone could direct me in the correct way to fix this code, I would greatly appreciate it.


As of right now, I'm getting a major headache and plotting the death of a stuffed animal via frustration...

ANY HELP IS EXTREMELY APPRECIATED!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

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

//Main Function

int main()
{
  ifstream file;
  const int MAX_ARRAY_SIZE = 47;
  string wteam;
  string lteam;
  int sum;
  int counter=0;
  int high;
  int low;
  double teamsaboveforty;
  float totalaverage;

  file.open("superbowl.dat");
  string winningteam [MAX_ARRAY_SIZE];
  string losingteam [MAX_ARRAY_SIZE];
  float score [MAX_ARRAY_SIZE];
  cout << left;
  cout << setw(15) << "Winning Team" << setw(15) << "Losing Team" << setw(15) << "Score" << endl;
  cout << "===============================" << endl;


  while (file.eof()== false)
    {
      file>>wteam>>lteam>>score;
      winningteam[counter]=wteam;
      losingteam [counter]=lteam;
      score[counter];
      cout<< left;
      cout << setprecision(0)<< fixed;
      cout << setw(15) << wteam << setw(15) << lteam << setw(15) << score << endl;
      counter++;
    }

  for (int i=0; i<47; i ++)
    {
      sum = sum + score[i];
    }

  totalaverage = sum /47;


  high = 0;
  for (int i = 0; i<47; i++)
    {
      if (score[i]> score[high])
        {
          high = i;
        }
    }


  low = 0;
  for(int i = 0; i<47;i++)
    {
      if(score[i] < score[low])
        {
          low = i;
        }
    }

  cout << "There are " << teamsaboveforty << " teams that scored above 40." << endl;
  cout << endl;
  cout << setw(15) << "Highest Points:" << setw(10) << wteam << setw(10) << lteam << score[high] << endl;
  cout << setw(15) << "Lowest Points:" << setw(10) << wteam << setw(10) << lteam << score[low] << endl;
  file.close();
  return 0;
}

Last edited on
You did it so well with winningteam/losingteam. Same situation with score array.
file>>wteam>>lteam>> score;

You need a variable to put the data into here (just like wteam and lteam). You can't just use the name of the array. Use a float variable called TempScore or something then do score[counter] = TempScore. Don't forget to change the output after to TempScore too.

I don't know if this is what you have been taught but don't use .eof() to check for the end of a file. Instead use
while (file>>wteam>>lteam>>TempScore) // Rest of loop

sum = sum + score[i];
Don't use sum without setting it to something first (like 0).

You haven't set teamsaboveforty anywhere, loop through the array like you have been doing and increment it if an element is > than 40.

If you need any more specific help let me know, but you have most of the stuff done correctly already.
I updated the program to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 #include <iomanip>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

//Main Function

int main()
{
  ifstream file;
  const int MAX_ARRAY_SIZE = 47;
  string wteam;
  string lteam;
  float TempScore;
  int sum = 0;
  int counter=0;
  int high;
  int low;
  double teamsaboveforty;
  float totalaverage;

  file.open("superbowl.dat");
  string winningteam [MAX_ARRAY_SIZE];
  string losingteam [MAX_ARRAY_SIZE];
  float score [MAX_ARRAY_SIZE];
  cout << left;
  cout << setw(15) << "Winning Team" << setw(15) << "Losing Team" << setw(15) << "Score" << endl;
  cout << setw(45) << "=====================================================================" << endl;

  while (file>>wteam>>lteam>>TempScore)
    {
      file>>wteam>>lteam>>TempScore;
      winningteam[counter]=wteam;
      losingteam [counter]=lteam;
      score [counter]=TempScore;

      cout<< left;
      cout << setprecision(0)<< fixed;
      cout << setw(15) << wteam << setw(15) << lteam << setw(15) << TempScore << endl;

      counter++;

    }

 for (int i=0; i<47; i ++)
    {
      sum = sum + score[i];
    }

  totalaverage = sum /47;


  high = 0;
  for (int i = 0; i<47; i++)
    {
      if (score[i]> score[high])
        {
          high = i;
        }
    }


  low = 0;
  for(int i = 0; i<47;i++)
    {
      if(score[i] < score[low])
        {
          low = i;
        }
    }
  cout << "Winning teams scored " << totalaverage << " points on average." << endl;
  cout << endl;
  cout << "There are " << teamsaboveforty << " teams that scored above 40." << endl;
  cout << endl;
  cout << setw(15) << "Highest Points:" << setw(10) << wteam << setw(10) << lteam << score[high] << endl;
  cout << setw(15) << "Lowest Points:" << setw(10) << wteam << setw(10) << lteam << score[low] << endl;
  file.close();
  return 0;
}



The printed output is given as:

Winning Team Losing Team Score
=====================================================================
Packers Raiders 33
Chiefs Vikings 23
Cowboys Dolphins 24
Dolphins Vikings 24
Steelers Cowboys 21
Cowboys Broncos 27
Steelers Rams 31
49ers Bengals 26
Raiders Redskings 38
Bears Patriots 46
Redskins Broncos 42
49ers Broncos 55
Redskins Bills 37
Cowboys Bills 30
Cowboys Steelers 27
Broncos Packers 31
Rams Titans 23
Patriots Rams 20
Patriots Panthers 32
Steelers Seahawks 21
Giants Patriots 17
Saints Colts 31
Giants Patriots 21
Ravens 49ers 34
Winning teams scored 15 points on average.

There are 0 teams that scored above 40.

Highest Points:Ravens 49ers 55
Lowest Points: Ravens 49ers -0

1. For some reason it won't read all the data for all 47 superbowls...it is skipping over every other...

I understand that this is what's causing my totalaverage to be set to 15, which is incorrect.

2. The program is only using the last of the teams (Ravens and 49ers) for the Highest and Lowest points outputs, as well as an incorrect Lowest Points output for the value

3. I'm also not sure what to do about the teamsaboveforty part of the array....

I felt pretty confident after your reply, but I think now I'm lost again..
Last edited on
1
2
3
4
  while (file>>wteam>>lteam>>TempScore)
    {
      //file>>wteam>>lteam>>TempScore;
You only need the first "file >> line", it is reading it while making sure you aren't at the end of the file.

By the look of it your program is printing the wrong values for the lowest value because your array isn't 47 elements long, and you are trying to read the data that isn't there.
Changing your loops to something like this should help. (So it only reads how many lines actually exist)
for (int i=0; i<counter; i ++)

Teamsaboveforty would be something similar to this.
1
2
3
4
5
6
7
   for (int i = 0; i<counter; i++)
    {
      if (score[i]> 40)
        {
              Teamsaboveforty++;
        }
    }


Hopefully that helps.
Okay, so I got everything working by following your help except that it for some reason is still using:

Highest Points: Ravens 49ers 55
Lowest Points: Ravens 49ers 14

It should be:

Highest Points: 49ers Broncos 55
Lowest Points: Dolphins Redskins 14

Would changing
1
2
cout << setw(15) << "Highest Points:" << setw(10) << wteam << setw(10) << lteam << score[high] << endl;
  cout << setw(15) << "Lowest Points:" << setw(10) << wteam << setw(10) << lteam << score[low] << endl;


To
1
2
cout << setw(15) << "Highest Points:" << setw(10) << wteam[score[high]] << setw(10) << lteam[score[high]] << score[high] << endl;
  cout << setw(15) << "Lowest Points:" << setw(10) << wteam[score[low]] << setw(10) << lteam[score[low]] << score[low] << endl;

fix it?
You just need the position of the highest/lowest values, you don't need the score array.

1
2
cout << setw(15) << "Highest Points:" << setw(10) << winningteam [high] << setw(10) << losingteam[high] << score[high] << endl;
cout << setw(15) << "Lowest Points:" << setw(10) << winningteam [low] << setw(10) << losingteam[[ow] << score[low] << endl;
Thank you so much, got it figured out!!
Topic archived. No new replies allowed.