Thread: need help with char array and fgets

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    need help with char array and fgets

    alright, well ive been having a problem trying to get this code to work,
    i have my three arrays

    char first[80];
    char second[80];
    char third[160];

    First i have to use the fgets() function to get a string from the user and store the data in the array first.. then do the same for the second array.

    next, i have to use the strcmp() function to determine which string is last alphabetically.

    then the strlen() function to determine which string is longest.

    and last use the strcpy() and strcat() functions to take first join it to second and store the result in the array named third.

    ....
    Code:
    int main(){
    char first[80];
    FILE *file;
    {
    printf("Enter a city name:");
    fgets(first, 80, file);
    }
    {
    printf("%s", first[80]);
    }
    system("pause");
    return 0;}
    iam completely hopeless with this.... i dont understand how to use "char *fgets (char *s, int size, FILE *stream);"

    if somone could help me that would be awesome, post here, e-mail me something, anytihng would be great..

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >fgets(first, 80, file);
    You want to read from the standard input stream, not a file, so it would be:
    Code:
    fgets(first, sizeof first, stdin);
    FILE *file is normally used when reading from a file.
    Code:
    >printf("%s", first[80]);
    This should be:
    Code:
    printf("%s", first);

  3. #3
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    int main(){
    char first[80];
    
    printf("Enter a city name:");
    fflush( stdout );
    
    if ( fgets(first, 80, stdin ) != NULL )
    {
        printf( "%s", first );
    }
    system("pause");
    return 0;
    }
    Last edited by Aia; 12-06-2007 at 07:27 PM. Reason: extra { left behind

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    holy ........, it works.... i....love...you?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    alright this is starting to make alot of sense, i got up to
    Code:
    int main(){
    char first[80];
    char second[80];
    char third[160];
    
    printf("Enter a city name:");
    fflush( stdout );
    
    if ( fgets(first, 80, stdin ) != NULL )	
    	
    printf("Enter another city name:");
    fflush( stdout );
    
    if ( fgets(second, 80, stdin ) != NULL )	
    		
    {
    	
    if(strcmp(first,second)>0){
    	printf("Last alphabetically: %s\n",first);
    	}
    else{
    	printf("Last alphabetically: %s\n",second);}    	
    }
    
    system("pause");
    return 0;
    }

    but strcpy() and strcat() functions to take first join it to second and store the result in the array named third is just confusing me

  6. #6
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
       #include <string.h> /* needs this include header */
    
        char original[] = "Hello";
        char attachment[] = "World";
        char classic[12]; /* big enough to hold the comming strings */
        
        strcpy( classic, original ); /* copy first string */
        strcat( classic, " " ); /* space between strings */
        strcat( classic, attachment ); /* join the second string */
        
        printf( "%s\n", classic );

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    it works but i need them to add together so that if i type in "hamilton" for the first, and then "halifax" for the second, they are printed on the same line without a space.... i cant get it to work

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    ok this is the code... and the last part to add the two arrays doesnt work proberly....

    Code:
    int main(){
    char first[80];
    char second[80];
    char third[160];
    
    printf("Enter a city name:");
    fflush( stdout );
    
    if ( fgets(first, 80, stdin ) != NULL )	
    	
    printf("Enter another city name:");
    fflush( stdout );
    
    if ( fgets(second, 80, stdin ) != NULL )	
    		
    {	
    if(strcmp(first,second)>0){
    	printf("Last alphabetically: %s",first);
    	}
    else{
    	printf("Last alphabetically: %s",second);}    	
    }
    
    {	
    if(strlen(first,second)>0){
    	printf("Longest string: %s",first);
    	}
    else{
    	printf("Longest string: %s",second);}    	
    }
    
    {
      strcpy( third, first );
      strcat( third, second );
      printf( "Joined together: %s\n", third );
    }
    
    system("pause");
    return 0;
    }
    i need the first and second to add on one line, without a space

  9. #9
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    >i need the first and second to add on one line, without a space

    Do you mean without a new line? Not like hamilton
    halifax?
    You need to take away the '\n' ( new line ) that is attached to the first string by virtue of the user pressing ENTER after the writing the answer.
    if(strlen(first,second)>0){
    strlen doesn't work like that.
    Declare two int variables
    Code:
    a = strlen( first );
    b = strlen( second);
    if( a > b)
    {
        <do something>
    }
    else
    {
        < do something else>
    }
    Code:
    {
      strcpy( third, first );
      strcat( third, second );
      printf( "Joined together: &#37;s\n", third );
    }
    You are adding unnecessary {} everywhere in your code. Those blocks are needed only after a control statement. Something like if/else or loops, switch, etc..


    if ( fgets(second, 80, stdin ) != NULL )
    The if in front of fgets() is for checking the return of the function. That's why the != NULL inside. Now if you are going to ignore the return you don't need to write it.
    fgets( second, 80, stdin ); would be sufficient.
    The 80 part can be substitute for something more generic in case you change the size of the array: That something would be sizeof second.
    The new call would be:

    Code:
    fgets( second, sizeof second, stdin );

  10. #10
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    A popular way of checking for a new line character is:

    Code:
        len = strlen( user_input ) - 1; /* get the lenght of string before '\0' */
        if ( user_input[len] == '\n' )/* if there's a new line before '\0' */
        {
             user_input[len] = '\0'; /* eliminate the '\n' */
        }
    Where len is a variable of type int.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I cannot stress this enough - indent your code! It helps you find bugs; it's not supposed to create bugs!
    Secondly, don't flush stdout in your app. Totally unnecessary and generally a bad practice unless you really need.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Secondly, don't flush stdout in your app. Totally unnecessary and generally a bad practice unless you really need.
    His calls to fflush() are perfectly acceptable.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But is it necessary?
    I/O are buffered for a reason.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In his case, it could be necessary. Many implementations do not flush the output buffer until a newline character is found. His printf() statements lack a newline, so to ensure the text is visible he used a fflush().

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That strikes me odd. Never had such a problem, not anyone else who posted their samples in C here, from what I can tell (unless they didn't run their samples?).
    Anyway then, remove that fflush and test if it works. IF it doesn't, THEN add the fflush.
    Flushing when not needed is not a good thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find '&' character in char array?
    By tidemann in forum C Programming
    Replies: 7
    Last Post: 10-19-2006, 05:04 AM
  2. How do you strip newline from char array?
    By pityocamptes in forum C Programming
    Replies: 7
    Last Post: 04-21-2006, 03:03 AM
  3. char array size question, please help!
    By Ash1981 in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 02:30 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM