Terminating loop properly?

This is a discussion on Terminating loop properly? within the C Programming forums, part of the General Programming Boards category; Hmm need some help trying to terminate the loop. Basically I want to receive a set of 2 integers, so ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Question Terminating loop properly?

    Hmm need some help trying to terminate the loop. Basically I want to receive a set of 2 integers, so when the user wants to terminate he/she should just enter nothing and press another enter.

    So for example, input would be:
    5 50
    7 70
    <enter> (this should terminate the input process and it should process the data)

    Any input would be appreciated - my C may be a bit rusty since I haven't coded in a while so bare with it
    Here's what I got so far:

    Code:
    /* The 3n+1 Problem */
    
    #include <stdio.h>
    
    /* Gets the cycle length for an input n */
    int get_cycle_length(int n);
    /* Returns the maximum cycle length between i and j */
    int maximum_cycle_length(int i, int j);
    struct node* insert_data(struct node**, int, int);
    void print_list(struct node*);
    
    /* Structure for storing info */
    struct node {
    	int i;
    	int j;
    	int result;
    	struct node *next;
    };
    
    int main(int argc, char **argv)
    {
    	int i, j, k=0, res=0;
    	int max_cycle = 0;
    	struct node* empty = NULL;
    	
    	while(1){
    		if (scanf("%d %d",&i, &j) != 2)
    		{
    			/* Checking Exceptions here */
                            /* Need help here!! */
    			if ((k = getchar()) == '\n')
    				break;
    			break;
    		} else
    		{
    			insert_data(&empty,i,j);
    		}			
    	}
    	print_list(empty);	
    	printf("\n");
    
    	return 0;
    }
    
    int get_cycle_length(int n)
    {
    	int cycles = 0;
    	/* Implement algorithm */
    	while (n != 1)
    	{
    		if (n % 2 == 0){
    			n /= 2;
    		} else {
    			n = 3*n + 1;
    		}
    	cycles++;
    	}
    	cycles++; 
    	return cycles;
    	
    }
    
    int maximum_cycle_length(int i, int j)
    {
    	int k, res, max_cycle = 0;
    
    	for (k = i; k <= j; k++)
    	{
    		res = get_cycle_length(k);
    		if (res >= max_cycle)
    			max_cycle = res;
    	}
    
    	return max_cycle;
    }
    
    struct node* insert_data(struct node** list, int i, int j)
    {
    	if (*list == NULL)
    	{
    		*list = malloc(sizeof(struct node));
    		(*list)->i = i;
    		(*list)->j = j;
    		(*list)->result = maximum_cycle_length(i,j);
    		(*list)->next = NULL;
    	}
    	else
    	{
    		/* Traverse until reach insertion point */
    	struct node* current = *list;
    	while (current->next != NULL)
    	{
    		current = current->next;
    	}
    	struct node *new = (struct node*)malloc(sizeof(struct node));
    	new->i = i;
    	new->j = j;
    	new->result = maximum_cycle_length(i, j);
    	new->next = NULL;
    	current->next = new;
    	}	
    		/* Finish up by returning head */
    	return *list;
    }
    
    void print_list(struct node* list)
    {
    	if (list == NULL)
    	{
    		printf("Empty list - nothing printed\n");
    		return;
    	} else {
    		while (list != NULL){
    		printf("%d %d %d",list->i, list->j, list->result);
    		printf("\n");
    		list = list->next;
    		}	
    	}
    	return;
    }

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    how about using fgets/sscanf combination for input instead of scanf. then if sscanf does not return 2 break the loop.
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  3. is my loop badly set up?
    By mabufo in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2006, 02:40 PM
  4. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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