Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-09-2003, 02:19 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Exclamation Segmentation Fault

I am writing a program for my C++ class, and i got the program to compile, but when i run it it outputs a few lines then says "Segmentation Fault", and exits. What could cause this and how would I fix this? The project is due tonight at 11EST, and this is bugging me. I will post the code in a little bit, just wanted to know if there is somewhat of a quick fix or if I just did something stupid.

thanks ^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 02:25 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
What does the program do? Could you post the code?
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 02:34 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
The program is supposed to keep tract of a number of taxis and keep two arrays, one for taxis in service, and one for taxis needing repair. The taxi can be in both. It reads in info from a data file with commands on what to do. That is the general jist of the program, I am sure by looking at the code you can tell whats supposed to be going on.

thanks ^dan
Attached Files
File Type: zip lab5.zip (2.9 KB, 28 views)
GSXdan is offline   Reply With Quote
Old 04-09-2003, 02:46 PM   #4
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I got it to compile properly and run without an error, so it may be your compiler. What compiler are you using?
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 02:48 PM   #5
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
not exactly sure what compiler it is, but it is being run on my college's unix server. What did you use to compile it?

thanks ^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 02:53 PM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I'm using the GNU complier, g++. When you compile your program, how do you do it? What commands do you use?
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 02:54 PM   #7
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Yea that is what I use on the unix server, i just type g++ lab5.cpp
GSXdan is offline   Reply With Quote
Old 04-09-2003, 02:56 PM   #8
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
can you post the output that you get when you run it?

thanks ^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 02:58 PM   #9
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
At what point does it crash for you?

This is what I get:

0
11111
74532

Here are the taxis in use:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired

Cab number 11395 is in use.
Cab number 11395 needs to be repaired
0
11111
64988
74532

Here are the taxis in use:
0
30412

Here are the taxis needing repair:
Cab number 30412 is in use.
Cab number 30412 needs to be repaired

0
11111
11395
30412

Here are the taxis needing repair:
0
11111
11395
64988
74532

Here are the taxis in use:
0
0
11111
11395
12146

Here are the taxis needing repair:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired

Cab number 99999 is in use.
Cab number 99999 needs to be repaired

Taxi not found.

Sorry, taxi already in array.

0
11111
11395
22233
48620
64988
74532

Here are the taxis in use:
0
0
0
11111

Here are the taxis needing repair:
Cab number 87664 is in use.
Cab number 87664 needs to be repaired

0
11111
11395
22233
26777
30412
48620
54112
64988
74532
87664

Here are the taxis in use:
Sorry, taxi already in array.

Sorry, taxi already in array.

0
0
11395
21163
22233
26777
30412
48620
54112
64988
74532

Here are the taxis in use:
0
0
0
11111

Here are the taxis needing repair:
0
0
0
11111

Here are the taxis needing repair:
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 03:03 PM   #10
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
I get this much:
0
11111
74532

Here are the taxis in use:
Cab number 64988 is in use.
Cab number 64988 needs to be repaired
Segmentation Fault



That is still not the right output though. I have what the output should be. I think the only thing that should not be there is the ID numbers w/o any kind of header, like this part:

0
11111
74532
GSXdan is offline   Reply With Quote
Old 04-09-2003, 03:05 PM   #11
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
I checked the code and there are no cout statements outputting what ID numbers are read in, so there is some error that is causing that. And i dont know what all those 0's are for.

^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 03:10 PM   #12
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Ok, i got rid of the segmentation fault. I forgot to put in code to make sure the array doesnt get more than 10 values, but the output is not correct. getting closer....

void Add(int CabArray[10], int& NumInArr, int ID)
{

int check;


check = Search(CabArray, ID, NumInArr);

if(check == -1){
if(NumInArr > 10){
CabArray[NumInArr+1] = ID;
NumInArr++;
}
}
else
cout << "Sorry, taxi already in array." << endl << endl;

Sort(CabArray, NumInArr);

return;
}

thanks ^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 03:18 PM   #13
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
This is my first program passing arrays, am i doing that correctly? I have a feeling it is something with the arrays or the number of items in the array variables, since they get passed around so much.

^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 03:37 PM   #14
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
That line you added should be If(NumInArr<10), not greater than.

Arrays in C++ are zero-based, so the Add function shouldn't add one to the index number when adding something to the array. It should start at zero.

Still working...
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 03:46 PM   #15
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
I make dumb mistakes when i rush, good thing you caught that
GSXdan is offline   Reply With Quote
Old 04-09-2003, 04:05 PM   #16
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
This is what I get now after making all the changes:

Here are the taxis in use:
11111
64988
74532

Cab number 64988 is in use.

Cab number 11395 is in use.
Cab number 11395 needs to be repaired.

Here are the taxis in use:
11111
11395
64988
74532

Here are the taxis needing repair:
11395
30412

Cab number 30412 needs to be repaired.

Here are the taxis needing repair:
11111
11395
22233
30412

Here are the taxis in use:
11111
11395
48620
64988
74532

Here are the taxis needing repair:
11111
11395
12146
30412
56565

Cab number 64988 is in use.

The taxi was not found.

Sorry, can't add.

Here are the taxis in use:
11111
11395
22233
30412
48620
64988
74532

Here are the taxis needing repair:
11111
12146
56565

Cab number 87664 is in use.

Here are the taxis in use:
11111
11395
22233
26777
30412
48620
54112
64988
74532
87664

Sorry, can't add.

Here are the taxis in use:
11111
11395
21163
22233
26777
30412
48620
54112
64988
74532

Here are the taxis needing repair:
11111
12146
56565

Here are the taxis needing repair:
11111
12146
56565
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 04:12 PM   #17
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
can you post the updated code? the only thing wrong with the output that i can get from the data file is the two repair arrays output at the end. From what i can tell, there should be only one, but that should be good enough. I dont know how many times i have said this, but it is very well deserved; Thank You


^dan
GSXdan is offline   Reply With Quote
Old 04-09-2003, 04:12 PM   #18
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Attached is the source file. I used the #define TEST and the #ifdef...#endif blocks to help debug by printing out the arrays every time something was added or deleted. To turn it back on, uncomment the #define TEST line.

Let me know if the output is what it should be.
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 04:22 PM   #19
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
This source takes care of the repeat of the last request for the repair array. You have to test for end of file after you read the line, so I moved the test below the read. Now there is no repeat.
doctorgonzo is offline   Reply With Quote
Old 04-09-2003, 08:22 PM   #20
usual suspect
 
hobey19's Avatar
 
Join Date: Jun 2002
Location: not here
Posts: 2,051
doctor gonzo, i am working on the same project with dan, how did u get rid of the zeroes that it was outputting that it is not supposed to. we can't figger that out. dan got it, but i cannot tell how he did it. thanks. hopefully you remember how you did that.

craig
__________________
the universe is against this current wave of success i'm having. -johnny drama, entourage
hobey19 is offline   Reply With Quote
Old 04-10-2003, 08:12 AM   #21
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
The problem was with the sort. When you removed an item from the array and replace it with zero, when you ran the sort to re-order the IDs from lowest to highest, that zero you just entered would be moved to the first item in the array, because it is lowest.

I fixed that by replacing the item you want to remove with a large number, not zero. Then you run the sort, which puts the number at the end. At that point, after you reset the number of elements in the array, you can set that large number to zero. It is all in the Remove function in the code I did.
doctorgonzo is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:56 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2