Stuck on hw! Running into problems! STRUCT AND ARRAYS

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;
    }
}
Last edited on
Here are a couple of problems I noticed right away (there may be more):

1. Your function prototypes do not match your function definitions. From your problem description, the prototypes are correct, not the definitions.

1
2
3
4
void initializeArrays(winRecord* standings, int);
void sortData(winRecord* standings, int);
void displayData(winRecord* standings, int);
int largestI(const int list[], int startingI);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void initializeArrays(winRecord &standings, int SIZE)
{
    //stuff
}

void sortData(winRecord &standings)
{
    //stuff
}

int largestI(const int list[], int startingI)
{
    //stuff
}

void displayData(winRecord &standings)
{
    //stuff
}

2. You are trying to assign a string to an uninitialized char pointer.

1
2
3
4
5
6
struct winRecord
{
    int wins;
    char* name; //just a char pointer
                        //have not allocated any space for an array
};


cin >> standings[i].name; //cannot do this without allocating space for the array

3. What is the point of the largestI function? You don't have any integer arrays in your code.
Last edited on
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
 #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 SIZE)
{
    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);
    }
}

void displayData(winRecord &standings, int SIZE) //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;
    }
}


Here is the sample output suppose to be:
How many teams will you enter?: 4
Enter team #1: Padres
Enter the wins for team #1: 75
Enter team #2: Dodgers
Enter the wins for team #2: 91
Enter team #3: Giants
Enter the wins for team #3: 92
Enter team #4: Red Sox
Enter the wins for team #4: 65
League Standings:
Giants: 92
Dodgers: 91
Padres: 75
Red Sox: 65

I have deleted the largestI function and please check if my prototypes are matching my definitions now. Also, please explain no.2 further, how do i allocated space for an array?
You didn't change the function definitions at all... What I posted was your function definitions. I was saying that they were wrong, not giving you corrected ones. If you want them to be correct, just copy/paste from your prototypes and delete the semicolon.

As for how to allocate space for an array, well normally I would say just do something like

1
2
3
4
5
struct winRecord
{
    int wins;
    char name[50];
};

but your professor specifically says you need to use dynamic array allocation for it. You're already doing dynamic array allocation in your main() so you should be able to figure it out...

The only problem is that you'd have to figure out the length of the name that's input. Maybe you should use your professor's getline.cpp. What's in it anyway?
#include<iostream>
#include<cstring>
using namespace std;

// read in a line of text and return it in a dynamically allocated array
char* getLine()
{
const int BUFFER_SIZE = 1000;

// Allocate a buffer local to this function
char buffer[BUFFER_SIZE];

// Use cin.getline() to input the buffer from the user
cin.getline(buffer, BUFFER_SIZE);

// Find the length of the string in buffer
int length = strlen(buffer);

// Dynamically allocate an array, to be returned
char *rValue = new char[length + 1];

// Copy the string into the dynamically allocated array
strncpy(rValue, buffer, length);

// Return the address of the dynamically allocated array
return rValue;
}


int main() {
char *name;

cout << "String: ";
name = getLine();
cout << name << endl;

delete [] name;

return 0;
}

This is what the getline.cpp is.
Looks exactly like what I would have suggested. Your professor already implemented dynamic array allocation in the function, so I guess you don't have to do it. Use it like this:

standings[i].name = getLine();
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
#include <iostream>
#include <cstring>
using namespace std;
void initializeArrays(winRecord* standings, int);
void sortData(winRecord* standings, int);
void displayData(winRecord* standings, int);

struct winRecord
{
    int wins;
    char* name;
};

char* getLine()
{
    const int BUFFER_SIZE = 1000;
    char buffer[BUFFER_SIZE];
    cin.getline(buffer, BUFFER_SIZE);
    int length = strlen(buffer);
    char *rValue = new char[length + 1];
    strncpy(rValue, buffer, length);
    return rValue;
}
int main()
{
    int i = 0;
    int SIZE;
    char* name;
    standings[i].name = getLine();
    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 [] name;
    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 SIZE)
{
    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);
    }
}

void displayData(winRecord* standings, int SIZE) //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;
    }
}


There are still a ton of errors. It keeps saying 'winRecord', 'standings', 'SIZE', 'i' are not declared?

Last edited on
1. You need to define your struct before your function prototypes.
2. Line 29 should not be there... It's supposed to replace line 46.
3. Sorry, I guess you really did use the largestI() function. But you still don't have any integer arrays, so you would have to change the largestI function or the sortData function so it actually works.
Last edited on
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
#include <iostream>
#include <cstring>
using namespace std;

struct winRecord
{
    int wins;
    char* name;
};

void initializeArrays(winRecord* standings, int);
void sortData(winRecord* standings, int);
void displayData(winRecord* standings, int);

char* getLine()
{
    const int BUFFER_SIZE = 1000;
    char buffer[BUFFER_SIZE];
    cin.getline(buffer, BUFFER_SIZE);
    int length = strlen(buffer);
    char *rValue = new char[length + 1];
    strncpy(rValue, buffer, length);
    return rValue;
}
int main()
{
    int i = 0;
    int SIZE;
    char* name;
    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 [] name;
    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;
        standings[i].name = getLine();
        cout << "Enter the wins for team #" << (i + 1) << ": ";
        cin >> standings[i].wins;
    }
    cout << endl;
}

void sortData(winRecord* standings, int SIZE)
{
    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);
    }
}

void displayData(winRecord* standings, int SIZE) //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;
    }
}


It looks better now. What you said about point 2, am i doing it correctly now? And do i have to do the same for line 49?
Also, what if i only want to change the sortData function and have integer arrays, how would i do that?
Last edited on
No you don't need to do it for line 49. The getLine function is only for c-strings (character arrays) and wins is an integer.

You would have to create an integer array in your sortData function, use a loop to fill that array with all the wins in standings, and then pass that array to your largestI function. You would also need to change largestI to accept 3 parameters.
Last edited on
Could you please give me an example of the whole integer array part? Sorry for asking so much, i just want to learn and understand it.
Last edited on
Topic archived. No new replies allowed.