I am a novice @ coding right now, and am getting it by trial and error, when i run this program after the 2nd round of input it skips some of the steps and then crashes. I threw the if (i > 0) to help bypass it on the first pass. Something must just be plain wrong. Can someone shed some light?

//Exc_6.cpp - Patrons and donations

#include <iostream>
#include <string>

using namespace std;

struct contributors
{
	string name;
	double donation;
};

int main()
{
	int numberOfContributors;
	cout << "Please input the number of contributors: ";
	cin >> numberOfContributors;
	cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
	contributors * thePatrons = new contributors [];
	for (int i = 0; i < numberOfContributors; i++)
	{
		cout << "Please input the name: ";
		if (i > 0)
			cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
		getline(cin, thePatrons[i].name);
		cout << "Please input the donation ammount: ";
		cin >> thePatrons[i].donation;
		cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
	}
	contributors * grandPatrons = new contributors [];
	contributors * regularPatrons = new contributors [];
	int j = 0, k = 0;
	for (int i = 0; i < numberOfContributors; i++)
	{
		if (thePatrons[i].donation > 10000)
		{
			grandPatrons[j].name = thePatrons[i].name;
			grandPatrons[j].donation = thePatrons[i].donation;
			j++;
		}
		else
		{
			regularPatrons[k].name = thePatrons[i].name;
			regularPatrons[k].donation = thePatrons[i].donation;
			k++;
		}
	}
	cout << "The grand patrons are as follow: " << endl;
	for (int i = 0; i < j; i++)
	{
		cout << grandPatrons[i].name << "\t\t" << grandPatrons[i].donation << endl;
	}
	cout << "The regular patrons are as follow: " << endl;
	for (int i = 0; i < k; i++)
	{
		cout << regularPatrons[i].name << "\t\t" << regularPatrons[i].donation << endl;
	}
	delete [] regularPatrons;
	delete [] grandPatrons;
	delete [] thePatrons;
	system("PAUSE");
}

Recommended Answers

All 4 Replies

line 20 should be: contributors * thePatrons = new contributors [numberOfContributors];

I did that, but after the first input it hits an infinite loop....why is it doing this?

You have three places (line 20, 31 and 32) where you do something like this:

contributors * thePatrons = new contributors [];

I have no clue why your compiler allows this or into what it gets compiled.. I just know that it is wrong (or at least very weird). You cannot allocate an undetermined amount of memory (which is what the [] means for static arrays), that simply makes no sense. Do as Ancient Dragon suggested.

Also, I find your use of ignore with those arguments a bit unusual, why not use just the normal "cin.ignore();"? If it doesn't work, and considering that your compiler will actually compile something like "int* ptr = new int[];", it makes me think there might be something fishy going on with your compiler / IDE. I compiled your code and it works as it should (with the size given for the allocated arrays).

I changed all 3 pointers to that and fixed the infinite loop. Ty 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.