|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#2 |
|
Professional gadfly
|
What does the program do? Could you post the code?
|
|
|
|
|
|
#3 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#4 |
|
Professional gadfly
|
I got it to compile properly and run without an error, so it may be your compiler. What compiler are you using?
|
|
|
|
|
|
#5 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#6 |
|
Professional gadfly
|
I'm using the GNU complier, g++. When you compile your program, how do you do it? What commands do you use?
|
|
|
|
|
|
#7 |
|
Member (10 bit)
|
Yea that is what I use on the unix server, i just type g++ lab5.cpp
|
|
|
|
|
|
#8 |
|
Member (10 bit)
|
can you post the output that you get when you run it?
thanks ^dan |
|
|
|
|
|
#9 |
|
Professional gadfly
|
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: |
|
|
|
|
|
#10 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#11 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#12 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#13 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#14 |
|
Professional gadfly
|
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... |
|
|
|
|
|
#15 |
|
Member (10 bit)
|
I make dumb mistakes when i rush, good thing you caught that
|
|
|
|
|
|
#16 |
|
Professional gadfly
|
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 |
|
|
|
|
|
#17 |
|
Member (10 bit)
|
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 |
|
|
|
|
|
#18 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#19 |
|
Professional gadfly
|
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.
|
|
|
|
|
|
#20 |
|
usual suspect
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 |
|
|
|
|
|
#21 |
|
Professional gadfly
|
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. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|