Thread: Converting a program I wrote in c++ to c

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    Converting a program I wrote in c++ to c

    So I have been programming c++ for a year, but I've never coded in c before until this recent attempt.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    struct Command {
    int n;
    double start_time;
    double duration;
    double note;
    double amplitude;
    };
    
    
    struct Node{
    struct Command* c;
    struct Node* next;
    };
    
    
    struct LinkedList{
    struct Node* head;
    struct Node* tail;
    };
    
    
    void copy(struct Command* origin, struct Command* destination);
    void addToList(struct LinkedList* list, struct Node* p);
    struct Node* getHighest(struct LinkedList* l);
    void removeFromList(struct LinkedList* l, struct Node* n);
    void sort(struct LinkedList* unsorted, struct LinkedList* sorted);
    
    
    int main(int argc, char** argv)
    {
    	struct LinkedList* unsorted = malloc( sizeof(struct LinkedList) );
    	struct LinkedList* sorted = malloc( sizeof(struct LinkedList) );
    	struct Node* curr = NULL;
    
    
    	struct Command* temp = malloc( sizeof(struct Command) );
    
    
    	char line[250];
    	FILE *fr;
    	fr = fopen ("C:\\Users\\shrikis\\Downloads\\output.sco", "rt");
    
    
    	while(fgets(line, 250, fr) != NULL)
    	{
    		if(line[0]=='i')
    		{
    			sscanf(line, "%*c %d %lf %lf %lf %lf", &temp->n, &temp->start_time,&temp->duration,&temp->note,&temp->amplitude);
    			curr = malloc( sizeof(struct Node) );
    			curr->c = malloc( sizeof(struct Command) );
    			copy(temp,curr->c);
    			curr->next = 0;
    			addToList(unsorted,curr);
    		}
    	}
    
    
    	sort(unsorted, sorted);
        return 0;
    }
    void copy(struct Command* origin, struct Command* destination)
    {
        destination->amplitude = origin->amplitude;
        destination->duration = origin->duration;
        destination->n = origin->n;
        destination->note = origin->note;
        destination->start_time = origin->start_time;
    }
    void sort(struct LinkedList* unsorted, struct LinkedList* sorted)
    {
    	struct Node* highest;
    	while(unsorted->head!=NULL)
    	{
    		highest = getHighest(unsorted);
    		removeFromList(unsorted,highest);
    		addToList(sorted,highest);
    		printf("%lf\n", highest->c->start_time);
    	}
    }
    
    
    struct Node* getHighest(struct LinkedList* l)
    {
    	struct Node* current_head = l->head;
    	struct Node* smallest = current_head;
    	while(current_head!=NULL)
    	{
    		if(current_head->c->start_time < smallest->c->start_time)
    			smallest = current_head;
    		current_head = current_head->next;
    	}
    	return smallest;
    }
    
    
    void removeFromList(struct LinkedList* l, struct Node* n)
    {
    	struct Node* head = l->head;
    	struct Node* previous = NULL;
    
    
    	while(head!=NULL)
    	{
    		if(head==n)
    		{
    			if(previous!=NULL)//Incase first node of list (has no previous node)
    					previous->next = head->next;
    			else
    				l->head = head->next;
    			break;
    		}
    		else
    		{
    			previous = head;
    			head = head->next;
    		}
    	}
    	head->next = NULL;
    }
    
    
    void addToList(struct LinkedList* list, struct Node* p)
    {
    	if(list->tail==0)
    		list->head = list->tail = p;
    	else
    	{
    		list->tail->next = p;
    		list->tail = p;
    	}
    
    
    }
    I wrote this program successfully in c++ but I tried to convert it to C with almost no luck at all. The above script is my sad attempt at the conversion. Can anyone offer any suggestions to help me get this program working in C?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What indications do you have that your program does not work? Are you getting compile errors? If so post the complete error message, exactly as they appear in your development environment. Otherwise ask specific questions about the posted code. Also supply a small sample of your input file.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    not so sad an attempt. code looks pretty good. it compiles ok on VC10. so as jimblumberg asked, what does it do wrong and how is it different from your C++ version?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    3

    more info

    Quote Originally Posted by dmh2000 View Post
    not so sad an attempt. code looks pretty good. it compiles ok on VC10. so as jimblumberg asked, what does it do wrong and how is it different from your C++ version?
    It compiles fine, but I get this when debugging:
    Code:
    Segmentation fault.
    
    At C:\Users\shrikis\Projects Summer\Pauls Sorter\main.c:119
    Here is a sample input:
    Code:
    t 0 125
    
    ;Pattern 0
    
    i 1 0 0.5 9.04 11
    
    i 1 0.5 0.5 9.02 3
    
    i 1 1 0.5 9.04 2
    
    i 1 1.5 0.5 9.02 7
    
    i 1 2 0.5 9.04 1
    
    i 1 2.5 0.5 9.02 9
    
    i 1 3 0.5 9.04 8
    
    i 1 3.5 0.5 9.02 4
    
    i 1 0 6 9.07 10
    
    ;Pattern 1
    
    i 1 0 9 9.00 5
    
    
     
    This is my c++ version before i tried to convert it to c, I changed some of the things as I was coding the program that I knew wouldnt work in C like cout >> and the use of classes.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    struct Command {
    int n;
    double start_time;
    double duration;
    double note;
    double amplitude;
    };
    typedef struct Node{
    Command c;
    struct Node* next;
    };
    typedef struct LinkedList{
    Node* head;
    Node* tail;
    }q;
    void addToList(q* list, Node* p);
    Node* getHighest(q* l);
    void removeFromList(q* l, Node* n);
    void sort(q* unsorted, q* sorted);
    int main(int argc, char** argv)
    {
     q* unsorted = new q();
     q* sorted = new q();
     Node *head = unsorted->head;
     Node *curr = NULL;
     Node *tail = unsorted->tail;
     Command *temp = new Command();
     
     char line[250];
     FILE *fr;
     fr = fopen ("C:\\Users\\shrikis\\Downloads\\output.sco", "rt");
     while(fgets(line, 250, fr) != NULL)
     {
      if(line[0]=='i')
      {
       sscanf(line, "%*c %d %lf %lf %lf %lf", &temp->n, &temp->start_time,&temp->duration,&temp->note,&temp->amplitude);
       curr = new Node();
       curr->c = *temp;
       addToList(unsorted,curr);
      }
     }
     
     sort(unsorted, sorted);
        return 0;
    }
    void sort(q* unsorted, q* sorted)
    {
     Node* highest;
     while(unsorted->head!=NULL)
     {
      highest = getHighest(unsorted);
      removeFromList(unsorted,highest);
      addToList(sorted,highest);
     }
    }
    Node* getHighest(q* l)
    {
     Node* current_head = l->head;
     Node* smallest = current_head;
     while(current_head!=NULL)
     {
      if(current_head->c.start_time < smallest->c.start_time)
       smallest = current_head;
      current_head = current_head->next;
     }
     return smallest;
    }
    void removeFromList(q* l, Node* n)
    {
     Node* head = l->head;
     Node* tail = l->tail;
     Node* previous = NULL;
     while(head!=NULL)
     {
      if(head==n)
      {
       if(previous!=NULL)//Incase first node of list (has no previous node) 
         previous->next = head->next;
       else 
        l->head = head->next;
       break;
      }
      else
      {
       previous = head;
       head = head->next;
      }
     }
     head->next = NULL;
    }
    void addToList(q* list, Node* p)
    {
     if(list->tail==0)
      list->head = list->tail = p;
     else
     {
      list->tail->next = p;
      list->tail = p;
     }
    }
    
    
    
    Last edited by jaytounit; 05-07-2012 at 07:17 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your C code seems to compile and run without problems for me. Are you sure your file is opening correctly?
    Here is the output the program produces on my machine with your input file.
    0.000000
    0.000000
    0.000000
    0.500000
    1.000000
    1.500000
    2.000000
    2.500000
    3.000000
    3.500000
    Your next step is to run your program thru your debugger. The debugger will tell you exactly where it detects the problem and will allow you to view the values of the program variables at the time of the crash.


    Jim

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Your C code seems to compile and run without problems for me. Are you sure your file is opening correctly?
    Here is the output the program produces on my machine with your input file.

    Your next step is to run your program thru your debugger. The debugger will tell you exactly where it detects the problem and will allow you to view the values of the program variables at the time of the crash.


    Jim
    hmm thanks jim, it must be a problem with my codeBlocks setup. I thought i had it but I guess some path or link isn't set correctly which is weird since I don't get any error regarding that type of problem. That output is correct and i'm glad i successful (some how) convert my program to C. Thanks again jim and dmh for your assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little program I wrote for fun !
    By manasij7479 in forum C++ Programming
    Replies: 43
    Last Post: 07-25-2011, 02:53 AM
  2. Just wrote my first Curses program
    By guesst in forum Linux Programming
    Replies: 14
    Last Post: 04-02-2008, 10:44 AM
  3. filesplitting program I wrote
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-30-2007, 03:24 PM
  4. Can someone look over the program I wrote?
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-16-2006, 07:23 AM
  5. Replies: 2
    Last Post: 04-25-2005, 11:59 AM