Problems with runtime error

This is a discussion on Problems with runtime error within the C Programming forums, part of the General Programming Boards category; I am stumped with this exercise. I'm supposed to write a program that "simulates dating". The original circle is described ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Problems with runtime error

    I am stumped with this exercise. I'm supposed to write a program that "simulates dating".
    The original circle is described by a string circle containing lowercase letters for women
    and uppercase letter for men. The last one in circle is actually adjacent to the rst one.
    Starting with the rst person in circle, we count from 1 to k around the circle, wrapping
    around if necessary, and letting the k-th person be the rst chooser. Letters nearer the
    beginning of the alphabet represent more desirable individuals, and the chooser always
    chooses the most desirable remaining member of the opposite sex. Starting with the
    next remaining person after the last chooser we again count from one to k (counting only
    people still remaining in circle) to determine the next chooser. This continues until the
    party breaks up.

    Right now, I'm getting a big runtime crash, don't know why.
    Then, I can't get them to prefer the letters closer the beginning of the alphabet.

    Any help would greatly appreciated.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define MAX_PERSONS 50
    #define MAX_CH 100
    
    
    void dating(char *circle, char *dates, int k);
    int my_strlen(char *s);
    
    int main(int argc, char *argv[]) 
    {
    	char date_array[50];
    	char *circle;
    	char *dates = date_array;
    
    
    	circle = argv[1];
    	int k = atoi(argv[2]);
    
    	dating(circle, dates, k);
    
    	printf("%s\n", date_array);
    
    
    	return 0;
    }
    
    void dating(char* circle, char* dates, int k)
    {
    	int circleLen = my_strlen(circle);
    	int end = 0;
    	//int empty = 0;
    	int gender;
    
    	char male[26] = "ZYXWVUTSRPQONMLKJIHGFEDCBA";
    	char female[26] = "zyxwvutsrqponmlkjihgfedcba";
    
    	while((my_strlen(circle)) > 0)
    	{
    		int temp = k;
    
    		while(end == 0)
    		{
    			if(temp > circleLen)
    			{
    				temp -= circleLen;
    			}
    			else
    			{
    				end = 1;
    			}
    		}
    		
    		if(isupper(circle[temp]))
    			gender = 1;
    		else
    			gender = 0;
    
    		if(gender == 1)
    		{
    			while(temp < (my_strlen(circle)))
    			{
    				int rank = 0;
    				int place;
    
    				for(int a = 0; a < 26; a++)
    				{
    
    					if((circle[temp] == female[a])&&(a > rank))
    					{
    						rank = a;
    						place = temp;
    					}
    				}
    
    				if(rank > 0) //writing a date to *dates
    				{
    					dates += sprintf(dates, "%c%c ", circle[place], male[rank]);
    					
    					char *newCircle;
    
    					for( int d = 0; d < place; d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					for( int d = (place +1); d < (my_strlen(circle)); d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					circle = newCircle;
    				}
    
    				printf("%c\n", circle[temp]);
    
    				temp++;
    			}
    		}
    		else
    		{
    			while(temp < (my_strlen(circle)))
    			{
    				int rank = 0;
    				int place;
    
    				for(int a = 0; a < 26; a++)
    				{
    					if((circle[temp] == male[a])&&(a > rank))
    					{
    						rank = a;
    						place = temp;
    					}
    				}
    
    				if(rank > 0) //writing a date to *dates
    				{
    					dates += sprintf(dates, "%c%c ", circle[place], female[rank]);
    					
    					char *newCircle;
    
    					for( int d = 0; d < place; d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    
    					}
    
    					for( int d = (place +1); d < (my_strlen(circle)); d++)
    					{
    						newCircle += sprintf(dates, "%c%c ", circle[d]);
    					}
    
    					circle = newCircle;
    				}
    
    				temp++;
    			}
    		}
    
    	}
    
    }
    
    int my_strlen(char *s)
    {
       int i = 0;
    
       // Count until you reach the end-of-string termination character
       while (*s != '\0')
       {
          i++;
          s++;
       }
       return i;
    }
    Last edited by azrael; 03-20-2009 at 03:53 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	char male[26] = "ZYXWVUTSRPQONMLKJIHGFEDCBA";
    	char female[26] = "zyxwvutsrqponmlkjihgfedcba";
    a string literal of 26 chars doesn't fit into a char array of 26 chars.
    Kurt

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by ZuK View Post
    Code:
    	char male[26] = "ZYXWVUTSRPQONMLKJIHGFEDCBA";
    	char female[26] = "zyxwvutsrqponmlkjihgfedcba";
    a string literal of 26 chars doesn't fit into a char array of 26 chars.
    Kurt
    who said it is string literal? it is just char array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Did initialize that one correctly? I wasn't sure, and it kept giving me messages when I tried putting them individually.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    if you want to initialize a char array you have to do it like this

    Code:
    char male[26] = {'Z', 'Y' .... }
    otherwise a '\0' would be added.
    Kurt

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by azrael View Post
    Did initialize that one correctly? I wasn't sure, and it kept giving me messages when I tried putting them individually.
    this will initialize the char array with 26 characters from the string, but of course it will not add \0 because you have no space for it. so you cannot pass the array to any function expecting nul-terminating string...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by vart View Post
    this will initialize the char array with 26 characters from the string, but of course it will not add \0 because you have no space for it. so you cannot pass the array to any function expecting nul-terminating string...
    Is that so ? My compiler doesn't like it. Just prints
    Code:
    error: initializer-string for array of chars is too long
    Kurt

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    So if I made it have 27 elements, or initialized each element individually, the program would work?

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't know. It doesn't crash for me but then I don't know what parameters I have to pass to that program it just doesn't do anything.
    Code:
    newCircle += sprintf(dates, "%c%c ", circle[d]);
    That looks wrong too, the format string specifies 2 chars but you pass only one.
    Kurt

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    oh, yeah. might be different in the code I have now.

    try typing in a bunch of letters, followed by an integer, like:

    asGDwuX 2

    it's supposed to pair up a lower case letter with the uppercase letter closes to the beginning of the alphabet, repeat this until there are no more possible pairs, then print them all out.

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yeah it does crash.

    Code:
        char *newCircle;
    
        for( int d = 0; d < place; d++)
        {
            newCircle += sprintf(dates, "%c%c ", circle[d]);
        }
    
        for( int d = (place +1); d < (my_strlen(circle)); d++)
        {
            newCircle += sprintf(dates, "%c%c ", circle[d]);
        }
    
        circle = newCircle;
    newCircle never points to anything useful
    Kurt

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    oh. Hm. Well, it was supposed add all the unpaired letters into newCircle, and then set circle to newCircle.
    How would I do that?

    or if possible, can I remove an element from circle?

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    are you sure your crash is not caused by acessing argv without chacking the argc value before?

    what are the command line arguments you are using?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    well, in a command line...(I use Pelles, so I don't have to enter in the program.)
    you'd have the program, and then two arguments.

    so i'm typing in:

    dating asFJsdW 2

    You're right, I didn't check argc, but I AM accessing the correct values. So I don't know why it would be an issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21