Having problem with linear search function

so this code runs, but when I'm trying to output the percent that corresponds to the genre that was inputed, it crashes... any ideas? so if the user inputs Rock then it should output the correct percent to discount

1
2
3
4
5
6
7
8
9
10
[code][code]#include<iostream>
#include<math.h>
#include<iomanip>

using namespace std;


void welcome();
void header();
char *artist();[code]

char *title();
char *genre();
float listPrice();
float taxation();
void readAll(char **cdArtist, char **cdTitle, char **cdGenre, float **price,float taxRate, int &aCount);
bool mData();
int linearsearch(const char *const *dgenre,int length, char* searchItem );
void bubblesortTitle(char **cdArtist, char **cdTitle, char **cdGenre, float **price, int &aCount);
void print(char **cdArtist, char **cdTitle, char **cdGenre,int *discounts, float **price,int &aCount);
void bubblesortGenre(char **cdArtist, char **cdTitle, char **cdGenre, float **price, int &aCount);
char **setupgenres();
int *setpercents();
void cSalePrice(float **price, char *cdGenre, const char * const *dgenre, int * discount, int aCount);
float cSalesTax(float **price, float &taxRate, int aCount);

int main()
{
int aCount = 0;
float taxRate = 0;
const char *const *dgenre = setupgenres();// holds genres in array
int *discount = setpercents();// sets the percent value to each genre
char **cdTitle = new char *[50];
for(int i = 0; i < 50; i++)
cdTitle[i] = new char[25];
char **cdArtist = new char *[50];
for(int v=0; v < 50; v++)
cdArtist[v] = new char[25];
float **price = new float*[50];
for (int i = 0; i < 50; i++)
price[i] = new float[4];
char **cdGenre = new char *[50];
for(int a=0; a < 50; a++)
cdGenre[a] = new char[25];
int *discounts = new int[50];

do
{
readAll(cdArtist, cdTitle, cdGenre, price,taxRate, aCount);// reads the inputs of the user
int index = linearsearch(dgenre, 7 , cdGenre[aCount]);// i suspect it crashes here
cout<< discount[index]<<endl;
aCount++;

}while ( mData() == true);

welcome();
header();
bubblesortTitle(cdArtist, cdTitle, cdGenre, price, aCount);
bubblesortGenre(cdArtist, cdTitle, cdGenre, price, aCount);
print(cdArtist, cdTitle, cdGenre,discount, price, aCount);
return 0;
}
void welcome()
{
cout<<"\t\t\tIvan's Jams Record Company"<<endl;
cout<< "\t\t Sale Report for March 18, 2010"<<endl<<endl;
}
void header()
{
cout<<"\t\tList\t\t\tDiscount\tSale\tSales\tCash"<<endl;
cout <<"Artist\tTitle\tPrice\tGenre\t\tPercent\t\tPrice\tTax\tPrice"<<endl;
}
char *artist()
{
char *nArtist=new char[30];
cout<<"Enter Artist: ";
cin.getline(nArtist,30,'\n');

return nArtist;
}
char *title()
{
char*cd= new char[30];
cout<< "Enter Title: ";
cin.getline(cd,30,'\n');

return cd;
}
char *genre()
{
char *type= new char[30];
cout<< "Enter Genre: ";
cin.getline(type,30);

return type;
}
float listPrice()
{
float price;
cout<<"Enter List Price: ";
cin>>price;

return price;
}
float taxation()
{
float tax;
cout<<"Enter Tax Rate: ";
cin>> tax;

return tax;
}
void readAll(char **cdArtist, char **cdTitle, char **cdGenre, float **price,float taxRate, int &aCount)
{
strcpy(cdArtist[aCount], artist());
strcpy(cdTitle[aCount], title());
strcpy(cdGenre[aCount], genre());
price[aCount][0]= listPrice();
taxRate = taxation();
}
bool mData()
{
char t;
bool data=false;
cout << "More CD's, T, F?: ";
cin>> t;
if(t == 'T'|| t == 't')
data = true;

return data;
}
int linearsearch(const char *const *dgenre,int length, char* searchItem )
{
int loc;
for (loc = 0; loc < length;loc++);
if (strcmp(dgenre[loc],searchItem)== 0)
return loc;
return -1;
}
void bubblesortTitle(char **cdArtist, char **cdTitle, char **cdGenre, float **price, int &aCount)
{
for (int iteration = 1; iteration < aCount; iteration++)
for(int index = 0; index < aCount - iteration; index++)
if (cdTitle[index]< cdTitle[index +1])
{
float *temp = price[index];
price[index]= price[index + 1];
price[index+1] = temp;

char *temp1 = new char[50];
strcpy(temp1,cdArtist[index]);
strcpy(cdArtist[index],cdArtist[index+1]);
strcpy(cdArtist[index+1],temp1);

char *temp2 = new char[50];
strcpy(temp2,cdTitle[index]);
strcpy(cdTitle[index],cdTitle[index+1]);
strcpy(cdTitle[index+1],temp2);

char *temp3 = new char[50];
strcpy(temp3,cdGenre[index]);
strcpy(cdGenre[index],cdGenre[index+1]);
strcpy(cdGenre[index+1],temp3);

}// end of if statement
}//end of bubble

void print(char **cdArtist, char **cdTitle, char **cdGenre, int *discount, float **price,int &aCount)
{

for(int i = 0; i < aCount; i++)
cout<<cdArtist[i]<<"\t"<<cdTitle[i]<<"\t"<< price[i][0]<<"\t"<<cdGenre[i]<<"\t\t"<< discount[i]<<"\t"<<price[i][1]<<endl;
}
void bubblesortGenre(char **cdArtist, char **cdTitle, char **cdGenre, float **price, int &aCount)
{
for (int iteration = 1; iteration < aCount; iteration++)
for(int index = 0; index < aCount - iteration; index++)
if (cdGenre[index]< cdGenre[index +1])
{
float temp = price[index][0];
price[index][0]=price[index + 1][0];
price[index+1][0] = temp;

float temp1 = price[index][1];
price[index][1] = price[index + 1][1];
price[index + 1][1] = temp1;

float temp2 = price[index][2];
price[index][2] = price[index + 1][2];
price[index + 1][2] = temp2;

float temp3 = price[index][3];
price[index][3] = price[index + 1][3];
price[index + 1][3] = temp3;

char *temp4 = new char[50];
strcpy(temp4,cdArtist[index]);
strcpy(cdArtist[index],cdArtist[index+1]);
strcpy(cdArtist[index+1],temp4);

char *temp5 = new char[50];
strcpy(temp5,cdTitle[index]);
strcpy(cdTitle[index],cdTitle[index+1]);
strcpy(cdTitle[index+1],temp5);

char *temp6 = new char[50];
strcpy(temp6,cdGenre[index]);
strcpy(cdGenre[index],cdGenre[index+1]);
strcpy(cdGenre[index+1],temp6);

}// end of if statement
}//end of bubble
char **setupgenres()
{
char **mygenre = new char*[7];
for(int k = 0; k < 7; k++)
mygenre[k]= new char[12];
strcpy(mygenre[0], "Classical");
strcpy(mygenre[1], "Country");
strcpy(mygenre[2], "International");
strcpy(mygenre[3], "Jazz");
strcpy(mygenre[4], "Pop");
strcpy(mygenre[5], "Rock");
strcpy(mygenre[6], "Show");

return mygenre;
}
int *setpercents()
{
int *mypercent = new int[7];
mypercent[0]=9;
mypercent[1]=3;
mypercent[2]=11;
mypercent[3]=18;
mypercent[4]=0;
mypercent[5]=10;
mypercent[6]=7;

return mypercent;
}
void cSalePrice(float **price, char **cdGenre, const char * const *dgenre, int * discount, int aCount)
{
int index = 0;
//index = linearsearch(dgenre, 7, cdGenre);
price[aCount][1] = price[aCount][0] - price[aCount][0] * static_cast<float>(discount[index] / 100.0);
}
float cSalesTax(float **price, float &taxRate, int aCount)
{
float tax= 0;
tax = price[aCount][1] * taxRate;

return tax;
}[/code][/code][/code]
Last edited on
Reformat the entire code example. Highlight and press the <> button on the right to insert within code tags. What you have posted is not readable. Also add a comment that indicates the line where the code is crashing so that we can more easily find what you are talking about.
Topic archived. No new replies allowed.