Rewrite your baseball league application from Assignment 6 so that each team/wins pair is stored in a struct named winRecord. Your program must meet the following requirements:
1. The winRecord struct must have two fields: an int named wins, and a char* named name.
2. The data must be stored in a single array, a dynamically allocated array of winRecord structs. You must ask the user how many teams are in the league to determine how big this array needs to be and then allocate memory for it.
3. Your program must use three functions that accept the array of winRecord structs by address (i.e., pass a winRecord pointer):
void initializeData(winRecord* standings, int size)
void sortData(winRecord* standings, int size)
void displayData(winRecord* standings, int size)
4. Note that the name field of each winRecord struct is just a char* which you will use to store a C-String. Unlike a C++ string object, the memory to store the actual characters of the C-String is not allocated for you automatically! You will need to allocate memory for each name field dynamically, based on the length of the team name that the user inputs. I encourage you to develop a function to do this on your own, but I have provided the getLine() function getLine.cpp to use if you wish. This function returns a line of text from the keyboard in a dynamically allocated array. It’s up to you to free up this memory using delete [] when you are done with it.
I am still very new to C++ and don't quite get how to do this program right. I am very slow at CS, so please walk me through the way to fix and complete this program. There are a lot of errors in here :(
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
|
#include <iostream>
using namespace std;
void initializeArrays(winRecord* standings, int);
void sortData(winRecord* standings, int);
void displayData(winRecord* standings, int);
int largestI(const int list[], int startingI);
struct winRecord
{
int wins;
char* name;
};
int main()
{
int SIZE;
cout << "How many teams will you enter?" << endl;
cin >> SIZE;
winRecord *standings = new winRecord[SIZE];
winRecord initializeArrays(standings, SIZE);
winRecord sortData((standings, SIZE);
winRecord displayData((standings, SIZE);
delete [] standings;
standings = NULL;
}
void initializeArrays(winRecord &standings, int SIZE) //This function asks the user to enter in the data that needs to be stored.
{
for(int i = 0; i < SIZE; i++)
{
cout << "Enter team #" << (i + 1) << ": ";
cin >> standings[i].name;
cout << "Enter the wins for team #" << (i + 1) << ": ";
cin >> standings[i].wins;
}
cout << endl;
}
void sortData(winRecord &standings)
{
int bigI;
for(int count = 0; count < SIZE - 1; count++)
{
bigI = largestI(wins, count);
swap(standings[bigI].name, standings[count].name);
swap(standings[bigI].wins, standings[count].wins);
}
}
int largestI(const int list[], int startingI)
{
int wantedI = startingI;
for(int count = startingI + 1; count < SIZE; count++)
{
if(list[count] > list[wantedI])
{
wantedI = count;
}
}
return wantedI;
}
void displayData(winRecord &standings) //This function displays the sorted list of names and wins.
{
cout << "League Standings: " << endl;
for(int i = 0; i < SIZE; i++)
{
cout << standings[i].name << ": " << standings[i].wins << endl;
}
}
|