I have to load two arrays, merge the two arrays into a third array, then create a control break report on name while outputting the most frequent style, then sort the array by style and to a control break on style. Can someone look at my code and see if they can see what I am doing wrong.

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

struct salesTran{
	string name;
	string style;
	double price;
	double quantity;
};

void MERGE(salesTran A[],int count1,salesTran B[],int count2,salesTran C[],int size, salesTran Temp[]){
	for (int i=0; i<count1; i++){
		C[i]=B[i];}
	int j=count1;
	for (int f=0; f<size; f++){
		C[j]=B[f];
		j++;}
	for (int i=0; i<size; i++){
		for (int f=0; f<size; f++){
			if (C[f].name>C[f+1].name){
				Temp[0]=C[f];
				C[f]=C[f+1];
				C[f+1]=Temp[0];}
		}
	}
	return;
}
void CBR_NAME(salesTran C[], int size){
	ofstream outdata1;
	outdata1.open("CBR_Name.txt");
	outdata1<<"Name"<<"\t"<<"\t"<<"Style"<<"\t"<<"\t"<<"Price""\t"<<"\t"<<"Quantity"<<"\t"<<"\t"<<endl;
	int	elegcount=0, excelcount=0, exqcount=0, excalcount=0, imagcount=0, inspcount=0, impresscount=0;
	string freqStyle;
	int count = 0;
	int highest=0;
	for(int i = 0; i < size; i++)
	{
		if(C[i].name == C[i+1].name)
		{
				outdata1 << C[i].name  <<"\t"<< C[i].style <<"\t"<< C[i].price <<"\t"<< C[i].quantity << endl;
				if (C[i].style=="Elegant") elegcount++;
				else if (C[i].style=="Excelsior") elegcount++;
				else if (C[i].style=="Exquisite") exqcount++;
				else if (C[i].style=="Excalibur") excalcount++;
				else if (C[i].style=="Imagine") imagcount++;
				else if (C[i].style=="Inspire") inspcount++;
				else if (C[i].style=="Impression") impresscount++;
		}
		else
		{
			if(elegcount<elegcount){
				highest=elegcount;
				freqStyle="Excelsior";}
			else {
				highest=elegcount;
				freqStyle="Elegant";}
			if (highest<exqcount){
				highest=exqcount;
				freqStyle="Exquisite";}
			if (highest<excalcount){
				highest=excalcount;
				freqStyle="Excalibur";}
			if (highest<imagcount){
				highest=imagcount;
				freqStyle="Imagine";}
			if (highest<inspcount){
				highest=inspcount;
				freqStyle="Inspire";}
			if (highest<impresscount){
				highest=impresscount;
				freqStyle="Impression";}
			if (highest!=0) outdata1 << "The most frequently sold style was " << freqStyle << ", being sold " << highest << " times." << endl << endl;
			highest = 0, elegcount=0, elegcount=0, exqcount=0, excalcount=0, imagcount=0, inspcount=0, impresscount=0;
		}
	}
	outdata1.close();
return;} 
void SORT_STYLE(salesTran C[], salesTran Temp[], int size){
	for (int k=0; k<size; k++){
		for (int j=0; j<(size-1); j++){
			if (C[j].style>C[j+1].style){
				Temp[0]=C[j];
				C[j]=C[j+1];
				C[j+1]=Temp[0];
			}
		}
	}
	return;
}

void CBR_STYLE(salesTran C[],int size){
	ofstream outdata2;
	outdata2.open("CBR_Style.txt");
	outdata2<<"Name"<<"\t"<<"\t"<<"Style"<<"\t"<<"\t"<<"Price""\t"<<"\t"<<"Quantity"<<"\t"<<"\t"<<endl;
	int count=0;
	string oldstyle=C[0].style;
	outdata2<< C[0].name << "\t" << "\t"<< C[0].style << "\t" << "\t"<< C[0].price << "\t" << "\t"<< C[0].quantity<< "\t" << "\t"<< endl;
	count++;
	for (int i=1; i<(size+1); i++){
		if (C[i].style==oldstyle){
			outdata2<< C[i].name << "\t" << "\t"<< C[i].style << "\t" << "\t"<< C[i].price << "\t" << "\t"<< C[i].quantity<< "\t" << "\t"<< endl;
			count++;}
		else{
			outdata2 << endl << "Count is" << count << endl;
			count=0;
			oldstyle=C[i].style;
			outdata2<< C[i].name << "\t" << "\t"<< C[i].style << "\t" << "\t"<< C[i].price << "\t" << "\t"<< C[i].quantity<< "\t" << "\t"<< endl;
			count++;}
	}
	outdata2 << endl << "Count is" << count << endl;
	outdata2.close();
	return;
}
int main(){
	ifstream indata1,indata2;
	indata1.open("FPData1.txt");
	indata2.open("FPData2.txt");
	
	salesTran C[100],Temp[1];
	int count1=100, count2=100;
	int size=count1+count2;
	
	salesTran A[100];
	for (int i=0;i<100;i++){
		indata1>>A[i].name;
		indata1>>A[i].style;
		indata1>>A[i].price;
		indata1>>A[i].quantity;
		}
	
	salesTran B[100];
	for (int i=0;i<100;i++){
		indata2>>B[i].name;
		indata2>>B[i].style;
		indata2>>B[i].price;
		indata2>>B[i].quantity;
		}
	MERGE(A,count1,B,count2,C,size,Temp);
	CBR_NAME(C,size);
	SORT_STYLE(C,Temp,size);
	CBR_STYLE(C,size);
	indata1.close();
	indata2.close();
	return 0;
}

What is it not doing? (or what is it doing that you didn't expect?) In other words, point us in a direction. Also, posting a snippet of your data file would allow someone to test it.

Also, when you have written in a 1 element array, you don't need it. Just use a regular variable.

Edited 5 Years Ago by jonsca: n/a

file1
Dwight Elegant 96 4
Dwight Elegant 96 1
Dwight Excelsior 68.5 2
Dwight Exquisite 89.9 1
Dwight Exquisite 89.9 2

file2
Dwight Imagine 62.5 1
Dwight Imagine 62.5 1
Dwight Imagine 62.5 4
Jim Imagine 62.5 1
Jim Imagine 62.5 1

These are snippets from the files. When I open up the CBR_Name file I only get the header and when I open CBR_STYLE I get a header, garbage, and then count=1

Here's a few to get you started:
In main, C is half the size it's supposed to be
In merge, which is the biggest problem, on line 15 B should be A, on 16 should be count1-1 (as the last element of a SIZE array is SIZE-1) and on 17, size should be count2. On line 22, trace it by hand and you'll see you're falling of the end of the array.

I'm sure there are more errors, but those are the glaring ones I saw.

Edited 5 Years Ago by jonsca: n/a

thanks....first C++ class and this is my final program and we hadn't even covered most of this.

This article has been dead for over six months. Start a new discussion instead.