|
-
September 23rd, 2011, 03:11 PM
#1
Segmentation Fault
I've been working on this project for school for quite some time, and I just can't get it right. I'm not looking for a direct answer, because I still want to try and learn and figure out, but instead for some clues as to what the heck the problem seems to be. Now, I have 3 .cpp files and 2 header files. Most of them work. The only problem I believe is causing all of this is one of the .cpp files entitled: "SalesDB.cpp". I'll post the source code here (along with the header file just to give you an idea of what I'm dealing with.
SalesDB.cpp
Code:
#include "SalesDB.h"
#include "Seller.h"
SalesDB::SalesDB()
{
numSellers = 0;
}
SalesDB::SalesDB(const char * DBFile)
{
ifstream inFile;
inFile.open(DBFile);
if(inFile.fail()){
cout << "File not found" << DBFile << endl;
}
inFile.read((char*) this, sizeof(SalesDB));
inFile.close();
sortSellers();
}
void SalesDB::print()
{
cout << "Sales Database Listing" << endl;
for(int i = 0; i < numSellers; i++)
sales[i].print();
}
void SalesDB::sortSellers()
{
int i, j;
Seller bucket;
for (i = 1; i < numSellers; i++)
{
bucket = sales[i];
for (j = i; (j > 0) && (strcmp(sales[j-1].getName(), bucket.getName())> 0); j--)
sales[j] = sales[j-1];
sales[j] = bucket;
}
}
int SalesDB::searchForSeller(char name[])
{
int low = 0;
int high = numSellers - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if(strcmp(sales[mid].getName(), name) == 0)
return mid;
else if(strcmp(sales[mid].getName(), name) > 0)
high = mid - 1;
else
low = mid + 1;
}
return - 1;
}
void SalesDB::processSalesData(const char * DBFile)
{
ifstream inFile;
char lastName[21];
char firstName[11];
double salesAmount = 0;
char name[31];
inFile.open(DBFile);
if(inFile.fail())
{
cout << "File not found" << DBFile << endl;
}
inFile >> lastName;
while(!inFile.fail())
{
inFile >> firstName;
inFile >> numSellers;
strcat(name, lastName);
strcat(name, ", ");
strcpy(name, firstName);
int index = searchForSeller(name);
if(index == -1)
cout << "No Record on File";
else
sales[index].setSalesTotal(sales[index].getSalesTotal()+salesAmount);
inFile >> lastName;
}
inFile.close();
}
SalesDB.h
Code:
#ifndef SELLER_H
#define SELLER_H
//*****************************************************************
// FILE: Seller.h
// AUTHOR: Damani Brown
// LOGON ID: z1619599
// DUE DATE: 9/23/11
//
// PURPOSE: Contains the declaration for the Seller class.
//*****************************************************************
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using std::cout;
using std::fstream;
using std::endl;
using std::setw;
using std::ios;
class Seller
{
private:
char name[31];
double sales_total;
public:
Seller();
Seller(const char*, double);
const char* getName();
double getSalesTotal();
void setSalesTotal(double);
void print();
};
#endif
Any help would be great guys/gals of this forum. I came here because it looks like all the tier 1 programmers come here lol.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|