Thread: getline problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    getline problem

    hey guys i m srry for asking to much but here i know the problem
    x will be alawys = to longest i m trying to make a program to get longest line and if user wanna quit and see the longest line he did it will post it but i dunno like whats best choice so where longest will maintain its length and not follow x thanks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int getline_program(char *buffer,int num)
    {
    	fgets(buffer,num,stdin);
    	return strlen(buffer)-1;
    }
    void save_it(char *buffer,char *buffer1)
    {
    	int i,x;
    	for(i=0,x=0 ; buffer[i]=buffer1[x] ; i++ ,x++);
    }
    int main(void)
    {
    	char name[100];
    	char *buffer;
    	int x,longest,swap;
    	buffer=malloc(sizeof (char)*100);
    	for(;;){
    		x=getline_program(name,sizeof name);
    		save_it(buffer,name);
    		longest=x;
    		swap=longest;
    		printf("x=%d\t longest=%d\n %d\n",x,longest,swap);
    		if(x>longest)
    		{
    			puts("its longer: preforming functions\n");
    			free(buffer);
    			buffer=malloc(sizeof (char)*100);
    			longest=x;
    			save_it(buffer,name);
    			printf("Your longest name is %s and length is %d",buffer,longest);
    		}
    	}
    	free(buffer);
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char longestline[ BUFSIZ ] = {0};
    size_t longestlinelength = 0;
    Start with that.
    Code:
    while they don't feel like quitting
        Read a line.
        Get its length.
        Compare that length to the longestlinelength.
            store the longest line
    Personally, I'd get it working with an array first, rather than allocating memory. Throw that in later if you really want to get a handle on dynamic memory allocation.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    thanks man i got it working now i was just using dynamic memory to get the hang of it and see how it works etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM