I'm having some problems dynamically changing the size of stucts. I know there must be a way to do it...but I guess it must be a bit different than with standard variables.

I'll put up some code, maybe I'm just being dumb ;)

struct SScore
{
	char m_szFirstName[15];
	char m_szLastName[15];
	int m_Score;
};

//const TOTAL = 25;

int main()
{
	SScore Golfers[1];
	SScore* pGolfers[1];
	int i	   = 0,
		iTotal = 0;
	
	cout << "\nEnter number of golfers: ";
	cin >> iTotal;
	
	Golfers = new (nothrow) SScore[iTotal] ;

theres alot more to the program, but this is where the error is occuring, so the rest is a bit reduntant. Any help would be greatly appreciated!!

>I know there must be a way to do it...
There's not a way to do it. Structures have a set size at compile-time. However, I get the impression that you simply don't know how to express your problem and ended up asking for something impossible. It looks like you want to have a dynamic array using pointers and memory allocation:

#include <iostream>

using namespace std;

struct SScore {
    char m_szFirstName[15];
    char m_szLastName[15];
    int m_Score;
};

int main()
{
    SScore *golfers;
    int total;

    cout<<"Enter number of golfers: ";
    cin>> total;

    golfers = new (nothrow) SScore[total];

    //...
}

However, you'd be much better off using the std::vector class, because it saves you the hassle of managing the memory.

>I know there must be a way to do it...
There's not a way to do it. Structures have a set size at compile-time. However, I get the impression that you simply don't know how to express your problem and ended up asking for something impossible. It looks like you want to have a dynamic array using pointers and memory allocation:

#include <iostream>

using namespace std;

struct SScore {
    char m_szFirstName[15];
    char m_szLastName[15];
    int m_Score;
};

int main()
{
    SScore *golfers;
    int total;

    cout<<"Enter number of golfers: ";
    cin>> total;

    golfers = new (nothrow) SScore[total];

    //...
}

However, you'd be much better off using the std::vector class, because it saves you the hassle of managing the memory.

Hmmm I'm not familiar with that class, but I see it brought up pretty frequently. So if you are loading an array of structs from a file, there's no way to take up the exact amount of memory required to load it? Insanity!

Oh nevermind I undestand what your saying. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.