Hi, I have a task to perform which all to do with the smurfs, reading from a file and doing some simple stuff to it. The last part I need to do is print out a list of all smurfs and their corresponding role in alphabetical order. I am hoping I can do this using the qsort function but this is giving a segmentation fault. I have attached my code below: [CODE] #include <stdio.h> /*Required for file operations*/ #include <string.h> /*Required for strlen etc*/ #include <stdlib.h> /*Required for Malloc,Free, etc.*/ /*-----------------------------------Declarations----------------------------------*/ FILE* fp; /*Declare the file pointer*/ char str_read[50]; char …

Member Avatar
+0 forum 0

I'm writing a program to convert Decimal numbers to binary, hexadecimal, and BCD as a refresher before i start getting code assignments in school. I'm almost done, but i have a seg_fault (compiler says decToHex function) in 1 function and i am hoping for some feedback on the others. I'll break it up a little so it's not 1 giant block of code and attach the .CPP [B]Class Definition[/B] [CODE] class ConvertDecimal { public: ConvertDecimal(); //void print(); private: int MAX_SIZE; void Convert (); string decToBinary (int decimal); string decToHex (int decimal); string decToBCD (int decimal); char hex(int num); string BCD(char …

Member Avatar
Member Avatar
+0 forum 7

I’m creating a program that displays random lines from a file. I have 8 lines on my text file and after displaying the last/8th random line… it displays [B][I]Segmentation fault[/I][/B] and then terminates the program. The program should display a message saying that there are no lines left to display. Can someone help me with this? Or can somebody tell me how to display the 8 lines only? This is the function I made so far… [CODE]int easy() { vector<string> text_file; string quest; char choice; int line_counter = 1; srand(time(NULL)); ifstream myfile ("./questions/easy.txt"); cout << "Welcome to EASY level category!\n" …

Member Avatar
Member Avatar
+0 forum 5

Please help me fix my code.. It works in CodeBlocks but when I try in Linux, it says it has an error - SEGMENTATION FAULT (core dumped) :( This program converts between bases of a number. After it converts a number it should go back to start to get input again.. [CODE]#include<stdio.h> #include<stdlib.h> typedef char string[64]; //Converting the character to integer int charToInt(char ch){ if(ch>=48 && ch<=57) return (int)(ch-48); if(ch>=65 && ch<=90) return (int)(ch-55); } //Converting the integer to character char intToChar(int x){ if(x>=0 && x<=9) return (char)(x+48); if(x>=10 && x<=35) return (char)(x+55); } int main(){ int currentBase = 0, …

Member Avatar
Member Avatar
+0 forum 3

I am declaring a structure as this [CODE]typedef struct Request { deque<int> roomsList ; }Request;[/CODE] Then , in some other function I get a segmentation fault in the highlighted line : [CODE] int initRoomsList(Request *R) { int roomSatisfying ; char roomName[20] ; int roomNum, capacity,wb,proj, lcd, sound, aud, vid , ac ; int count = 0; FILE *fp ; fp = fopen("rooms.txt","r") ; while(fscanf(fp,"%d %d %d %d %d %d %d %d %d %s", \ &roomNum,&capacity, &wb,&proj,&lcd,&sound,&aud,&vid,&ac,roomName) != EOF ) { if(R->capacity > capacity ) continue ; if(R->wb > wb ) continue ; if(R->proj > proj) continue ; if(R->lcd > lcd …

Member Avatar
Member Avatar
+0 forum 5

This is another homework problem. I thought it was easier, and I wouldn't need help, but I keep getting a segmentation fault when running the program. The assignment is to use a recursive function to output the amount of candybars we can buy with a user-inputted amount of money. Each candybar costs $1, and each candybar gives a coupon. 7 coupons can be redeemed for an additional candybar. The example he gave us is: "For example, if we have $20 dollars then we can initially buy 20 candy bars. This gives us 20 coupons. We can redeem 14 coupons for …

Member Avatar
Member Avatar
+0 forum 12

I wrote this program for insertion sort.I compiled the source file in xubuntu terminal.But when I try to run a.out executable....terminal showing "segmentation fault".I don't about segmentation fault..when segmentation fault occurs...what is the thing in the source file that is causing segmentation fault... Thanks in advance...... [CODE]#include<stdio.h> main(){ int i,j,n,key; int a[n]; printf("Enter size:"); scanf("%d",&n); printf("\n Enter elements:separate each by space\n"); for(j=0;j<n;j++) scanf("%d ",&a[j]); for(j=1;j<=n-1;j++){ key=a[j]; i=j-1; while((i>=0)&&(a[i]>key)){ a[i+1]=a[i]; i=i-1; } a[i+1]=key; } for(j=0;j<=n-1;j++) printf("%d ",a[j]); }[/CODE]

Member Avatar
Member Avatar
+0 forum 5

A recent assignment involved using very large integer values. A formula and very large x values were given and we had to compute the value of f(x). The tutor suggested using long long values as the 0 < x < 10^18. The function F(x) = F(x/2) - x if x even; F((x+1)/2) + x if x odd. F(1) = 3; We were given a range of x values and a memory limit of 1mb (It was an exercise in space/time tradeoff). What we were taught was to compute all the values once, store them and then read off the ones …

Member Avatar
Member Avatar
+0 forum 2

Hello! I am making a program which does many computations for a Photovoltaic system. The problem is that i get a strange segmentation fault error when I run the program. Here is where it crushes (at for-loop of calculation S5) : [CODE] commands..... ... ... /******************** WIRING *******************************/ Ni = N_I/(Ns*Np); S1 = 0; for(j=0; j<Np; j++) S1 += (j-1)*Lpv2 + Lpv2/2; S2 = 0; for(j=0; j<Nr; j++) S2 += 2*h_d + W_T + (j-1)*Np*Lpv2; S3 = 0; for(j=0; j<Nc; j++) S3 += h_d + (j-1)*Ns*Lpv1; S4 = 0; for(j=0; j<Nc_nm; j++) S4 += h_d + (j-1)*Ns*Lpv1; S5 = 0; …

Member Avatar
Member Avatar
+0 forum 4

Hi, help please! I have a struct containing an array of pointers to structs containing Binary Search Trees with nodes containing structs. ... <*sigh*> I hate C soo much... put work wants it to be in C. I can set the struct of BST's just fine, and keep it around, and I can set the innermost struct values. My problem is keeping those innermost values once I'm outside the function that puts them there. [CODE] void function(Struct struct, int i, int k) { Array array; array = InitArray(); array->key = k; struct->array[i] = &array; } void otherFunction(Struct struct, int i, …

Member Avatar
Member Avatar
+0 forum 1

[CODE]/* this code attempts to generate a sudoku board in an non graphical envoirnment the code compiles fine but however it throws segmentation fault everytime it's run*/ # include<stdio.h> # include<stdlib.h> # include<time.h> static int baseArray[9][9]; static void initialise(); void display(); static int checkPosition(int row, int col, int value); int main(void) { srand(time(NULL)); initialise(); display(); return 0; } static void initialise() { int i, j, value, row = 0, col = 0, flag = 0; for(i=0;i < 30; ) { printf("inside initialise\n"); row = rand() % 8; //randomising rows columns and value col = rand() % 8; value = rand() …

Member Avatar
Member Avatar
+0 forum 1

Hi, I am writing a program to solve the 8puzzle, but for some reason i am having more difficulty with the structure than the algorithm.... I have two classes, a tree and node class. My main problem is to put the nodes in an array in the tree class. Second, which is the one om having problems now, my "move" function in my Node class is giving me a seg fault if i create a int? it is also changing values of my root state? Can someone please give me any idea where i am going wrong? Thanks! [CODE] //inside …

Member Avatar
Member Avatar
+0 forum 1

I'm getting a segmentation fault at this line. So far, my program has classes. A blahpod with class of songs in them. Then there is the node class. I call the operator= which is supposed to put one pod into another. It looks like so [CODE]bobcatPod bp2( bp1 );[/CODE] That accesses the functions in my bobcatPod.cpp file. [CODE]bobcatPod::bobcatPod(const bobcatPod & rhs) { (*this) = rhs; }[/CODE] Which in turn accesses [CODE]bobcatPod & bobcatPod::operator=(const bobcatPod & rhs) { while(firstNode != NULL) { removeSong(firstNode->data.getTitle(), firstNode->data.getArtist()); } node *endOfList; for(node *ptr=rhs.firstNode; ptr!=NULL; ptr = ptr->next) { node *temp = new node; temp->data = …

Member Avatar
Member Avatar
+0 forum 2

Hi, I need to create a program to do a threaded merge sort (kinda), but in the meantime I am trying to pass a struct through the the Merge method (thread runs this method), but I keep getting seg faults. I am sure its something obvious, please help me out. Here is my code: [CODE]/****************************************************************************** * FILE: hello_arg1.c * DESCRIPTION: * A "hello world" Pthreads program which demonstrates one safe way * to pass arguments to threads during thread creation. * AUTHOR: Blaise Barney * LAST REVISED: 01/29/09 ******************************************************************************/ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <vector> #include <iostream> #include …

Member Avatar
Member Avatar
+0 forum 2

This is giving me a segmentation fault in the noBucket() function. I don't see why. Does it having something to do with it being a const function? My other accessor functions are working fine and they are declared exactly the same with just a different name. The files are longer than this I just pasted the relevant code(from what I could tell). [CODE]class Bucket { unsigned short _noBucket; public: unsigned short noBucket() const {return _noBucket;} }; class Hashtable { Bucket _buckets[30]; public: void printReport(ostream& o) {o << "Bucket Number: " << _buckets[i].noBucket() << '\n';} };[/CODE]

Member Avatar
Member Avatar
+0 forum 3

The title explains most of it. at the point in the code where this frame gets created it immediately opens, closes, then segfaults, and the whole program dies. the program worked find before this frame was added and the only code using this frame from outside of it is the constructor. It prints "done" and then dies. [CODE]emailDiag=EnterEmail(self,-1,'email win')[/CODE] help would be greatly appreciated [CODE] class EnterEmail(wx.Frame): def __init__(self, parent,id,title): wx.Frame.__init__(self,parent,id,title,size=(RESOLUTION[0]/1.5,RESOLUTION[1]/1.5)) self.parent=parent font5 = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL) font3 = wx.Font(18, wx.SWISS, wx.NORMAL, wx.NORMAL) font1 = wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD,True) self.SetBackgroundColour('#f2f200') sizer=wx.BoxSizer(wx.VERTICAL) panel0=wx.Panel(self,-1) panel1=wx.Panel(self,-1) hsizer=wx.BoxSizer(wx.HORIZONTAL) vsizer=wx.BoxSizer(wx.VERTICAL) vsizer2=wx.BoxSizer(wx.VERTICAL) infoLabel = wx.StaticText(self, …

Member Avatar
Member Avatar
+0 forum 7

Hi all, I write because I am struggling with a very strange thing. I have this class: [CODE] static const long POINTS_VERY_FEW = 100; class Bin2DSphere { /*! Class for 2-dimensional binning on a sphere */ public: string Name; double PhiMin; /* Minimum value of Phi */ double PhiMax; /* Maximum value of Phi */ double ThetaMin; /* Minimum value of Theta */ double ThetaMax; /* Maximum value of Theta */ long Size; /* Size of data */ bool IsFilled; /* Flag is TRUE if histogram has been filled */ bool IsNormalized; /* Flag is TRUE if histogram has been …

Member Avatar
Member Avatar
+0 forum 10

Hello, I'm doing a full traversal of a binary search tree structure looking for a node by a field that is not the sort key. If the node i'm looking for is the root node, no problem. If it's any other node then the function has a Seg Fault. Anyone know what is going on? I'm pretty sure the problem is in my logic in what i'm returning from Find_Trav_Subtree, but cannot pinpoint it. gdb debugger gives me this, but i'm not sure if it helps. Breakpoint 1, BSTClass::Print_Node (this=0x80537a4, NodePtr=0xb7f1e888) at bstree.cpp:227 227 { (gdb) next 228 cout << …

Member Avatar
Member Avatar
+0 forum 3

I have been struggling with this for a while. Will someone please help me? The issue is within a while loop. The while loop is reading info from getline, copying values from an input file to (1)double array type char[i][20] , (2)array type double [i]. The input file is as follows: first 1.2 second 2.7 third 3.4 fourth 4.2 fifth 2.1 sixth 2.0 The problem code segment: [code=c++] cout << "test 1 " ; file_read >> country_name[i][20] >> happiness_values[i]; cout << " test 1.5" << endl; while (!file_read.getline(line, 100).eof()) && (i < 15) ) // (i < 15) used for …

Member Avatar
Member Avatar
+0 forum 8

I'm using Dev-c++ to make a dialog-based app. I had it working, but now I am getting an error: An Access Violation (Segmentation Fault) raised in your program. I have narrowed it down to the winmain function: [CODE]int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { if (iCmdShow > 4) { commandlineopener=szCmdLine; } gCmdShow=iCmdShow; strcat(about,version); strcat(about,"Stuff"); //About stuffs go here DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0,(DLGPROC)MainDlgProc); WSACleanup(); return 0; }[/CODE] What am I doing wrong?

Member Avatar
Member Avatar
+0 forum 4

Ok basically I have a program that is doing a counter clockwise spiral and will print the value i or print out a 0 depending upon if the number is prime. n is equal the length of the spiral while i is the initial center value of the spiral which is incremented with each shift in the spiral. I need the spiral to insert the values counter clockwise starting at the center. The problem I'm having is a segmentation fault. When I put it through a debugger it's pointing to the second else statement: [CODE]else a[k][l] = 0 ;[/CODE] but …

Member Avatar
Member Avatar
+0 forum 5

Yeah this is a simple question and pretty popular. And this is what I did. There is a segmentation fault when we copy characters from [ICODE]src [/ICODE]into [ICODE]dest[/ICODE]. Now, I can understand the source of this fault - when we iterate over the [ICODE]dest [/ICODE]string we reach the end and then the [ICODE]dest [/ICODE]pointer points to the 'string terminator' or 0. So next when we put [ICODE]*dest++=*src++[/ICODE] it obviously will give an error. [CODE] //includes void concat(char * dest, const char * src) { while (*dest) ++dest; while (*dest++ = *src++); } void main(){ concat("hi","hello"); getch(); }[/CODE] The only way …

Member Avatar
Member Avatar
+0 forum 2

My first post on this forum, long time lurker though. I'm running into a problem trying to search through an N-ary tree. I'm tasked with having the user input pairs of job titles and from there putting them into an N-ary tree, with the higher job titles at higher levels. I'm trying to write a function that will take the root and the desired job title and search through the tree and return where it's found so that I can insert the second job title under it. "parentnode" is the node in the tree where the first title entered can …

Member Avatar
Member Avatar
+0 forum 1

can anyone please tell me what is wrong with dis code.....i am getting a segmentation fault even though it has been compiled successfully(in a unix system): [CODE]#include<stdio.h> int low,high,mid; void msort(int a[],int low,int high); void merge(int a[],int low,int high,int mid); int main() { int a[10]; int count; for(count=0;count<10;count++) { printf("Enter element number %d\n",count+1); scanf("%d" , &a[count]); } low=a[0]; high=a[9]; msort(a,low,high); for(count=0;count<10;count++) { printf("%d\n" ,a[count]); } } void msort(int a[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; msort(a,low,mid); msort(a,mid+1,high); merge(a,low,high,mid); } } void merge(int a[],int low,int high,int mid) { int b[50]; int i,j,k; i=low; j=mid+1; k=low; while((i<=mid)&&(j<=high)) { if(a[i]<a[j]) { …

Member Avatar
Member Avatar
+0 forum 8

I am learning to write C++ code to access data in a MySQL database. My goal is to use a separate class to control the database access so I can use this class elsewhere. To facilitate my learning, I'm working on simple code to connect to a database (success!) and return all rows in a simple and small table (failing!). The connection is OK through the class constructor but I'm getting a segmentation fault in the `queryResults = query.store()` in the class method. The backtrace, as I read it, says the failure is in a MySQL routine. (PS: The gtkmm.h …

Member Avatar
Member Avatar
+0 forum 1

Hi all, i've to make an introduction of what am i doing before start making questions. The goal of my work is to create an application quite like an IDE, for specific program development ( always in C++ ) and testing. Since the need of execute these programs under the main IDE process for monitoring and logging purposes, my solution is to compile them as shared library and load them dynamically from the IDE. (Any suggestion about other strategies is welcome!) The application is to be considered a 24h server, so can't be subject to problems like memory leaks or …

Member Avatar
+0 forum 0

I have a program that works fine under windows, but when i run it on Unix I get a very slightly diffent set of results, followed by a 'segmentation fault' message. I'm at a loss to explain why. Any ideas?

Member Avatar
Member Avatar
+0 forum 10

I'm working on a C program that needs a lot of dynamic memory allocation of 1-D and 2-D arrays. The program works fine when I compile and run it using windows and Visual C++, but when I compile and run it in a Unix environment, I get slightly different results, and a message at the end of my results saying 'Segmentation fault'. The programs are identical, so I don't know why I don't get the same results in both instances. I'm sure that it has something to do with the dynamic memory allocation. Please help!

Member Avatar
Member Avatar
+0 forum 5

Hi everyone, I'm a moderately experienced C++ programmer working on code which must do the following: (a) Import data from a lot of little CSV files (b) Load that data into various objects (c) Do stuff with that data The code I've written does (a), (b), and (c) pretty well, but I've noticed a problem with (a), which I want to ask you guys about. Suppose I have 1000 source files. My program successfully processes Files #1 through #500. But when it reaches File #501, my program chokes and seg faults, and I automatically lose [I][B]ALL[/B][/I] the data I've collected. …

Member Avatar
Member Avatar
+0 forum 4

hey guys/gals. i get a segmentation fault whenever the for loop in the print function is executed. any suggestions? im not sure what i did wrong. [CODE]//class member functions #include "experiment.h" #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; int Experiment::numberOfTests = 0; Experiment::Experiment(){} void Experiment::setExpName() { cout << "\nPlease enter a tag name for this experiment: "; cin >> name; } string Experiment::getExpName() { return name; } void Experiment::setPersonForExperiment() { cout << "Please enter the name of person responsible for conducting this experiment: "; cin >> who; } string Experiment::getPersonForExperiment() { return who; } void Experiment::setNumOfTests() …

Member Avatar
Member Avatar
+0 forum 1

The End.