-
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.
-
September 26th, 2011, 04:59 AM
#2
Re: Segmentation Fault
Hello.
An efficient way to debug is to use WinDbg which is a great tool from microsoft..and its free. This must be an exe right?
You can open the exe directly insite the debugger. Make sure the sources and the symbols are properly set. Also make sure that the microsoft symbol server is in the symbol path...so that the call stack gets established properly..
and bing..there you go..u can use locals to watch hw the local variables change..watch memory..or use it to slowly step thru and the find the cause of the failure..
Best Regards,
Aijaz Baig.
-
September 26th, 2011, 07:12 AM
#3
Re: Segmentation Fault
Or just use the Visual Studio IDE and debug it there.
-
September 26th, 2011, 07:51 AM
#4
Re: Segmentation Fault
Aside from the correct recommendations of the other posters to simply debug your program, I see at least one thing that looks strikingly wrong:
Code:
inFile.read((char*) this, sizeof(SalesDB));
I have no idea what you actually had in mind when writing that line, but by executing it, your SalesDB objet overwrites itself with the contents of the file, which for certain is wrong.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
September 26th, 2011, 08:07 AM
#5
Re: Segmentation Fault
Your segmentation fault is probably caused from an out_of_bounds exception
You are probably trying to access a bad array location.
Didn't read all of your code but i would say that your problem lurks somewhere in your sort method.
i got a few suggestions.
1. map
Code:
#include <map>
map<int, string> Data;
Data[9385] = "John";
Data[50] = "Alex";
Data[-50] = "Steve";
2. (A better approach) ::vector
Code:
#include <algorithm>
#include <vector>
using namespace std;
vector<int> nums;
nums.push_back(50);
nums.push_back(20);
nums.push_back(30);
// nums contains { 50, 20, 30 }
// ASC sorting
bool __SortAsc (int i,int j) { return (i<j); }
sort(nums.begin(), nums.end(), __SortAsc);
// nums contains : 20, 30, 50
// DESC sorting
bool __SortDesc (int i, int j) { return (i>j); }
sort(nums.begin(), nums.end(), __SortDesc);
// nums contains : 50, 30, 20
you can also access a vector like you would do in an array.
nums[0] = 50;
looping on vectors :
Code:
for(vector<int> :: iterator i = nums.begin( ), i != nums.end( ); i++) //
{
cout << (*i) << endl; // (*i) points to our element inside the vector
}
read more about vectors here : http://www.cplusplus.com/reference/stl/vector/
hope i helped
Regards
Like Eri523 stated you are doing that very wrong.
why don't u use getline() ?
Code:
ifstream inFile;
string textfile = "mytext.txt";
inFile.open( textfile.c_str( ) );
if( !inFile.fail( ) )
{
string line = "";
getline(inFile, line);
}
Last edited by JacobNax; September 26th, 2011 at 08:17 AM.
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
|