Sorry for all the posts but...

This is a discussion on Sorry for all the posts but... within the C Programming forums, part of the General Programming Boards category; Well thanks guys for the good tutorial i created my first program written with the basic knowledge of C but ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Sorry for all the posts but...

    Well thanks guys for the good tutorial i created my first program written with the basic knowledge of C but some bugs need to be worked out.

    Well heres my Program code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void strip_newline( char *str, int size )
    {
         int i;
         for (  i = 0; 1 < size; ++i )
         {
             if ( str[i] == '\n' )
             {
                  str[i] = '\0';
                  
                  }
                  return;
             }
         }
                  
                  
    int main()
    {
        FILE* Testfile;
        FILE *fp;
        char name[50];
        char lastname[50];
        char fullname[100];
        
        printf( " Please enter your first name name: " );
        fgets( name, 50, stdin );
        strip_newline( name, 50 );
        if ( strcmp ( name, "Alex" ) == 0 )
        {
             printf( "thats my name too.\n" );
             }
             else
             {
                 printf( "Thats not my name :(\n" );
                 }
                 
                 printf( "Your name is %d, letters long", strlen ( name ) );
                 printf( "Enter Your Last Name: " );
                 fgets( lastname, 50, stdin );
                 strip_newline( lastname, 50 );
                 fullname[0] = '\0';
                 strcat( fullname, name );
                 strcat( fullname, " " );
                 strcat( fullname, lastname );
                 fp=fopen("C:\\Test.txt", "w");
                 fprintf(fp,  "First Name: %s", fullname );
                 printf( "Your full name is %s\n",fullname );             
                 getchar();
                 return 0;
                 }
    I Combined the tutorials Code with the File input output protocol. Just need to get it to add the last name along with the first name

    P.S If your gonna try it it creates a file in you c drive named Test.txt Which has ur name.

    Thanks for the tutorial.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    NVM Fixed it

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Do not forget to do

    Code:
    fclose(fp);
    to close the file.

    Also on opening the file check to see if fopen is succesfull.

    Code:
    
    fp = fopen ("foo.txt","w");
    if (fp==NULL) {
      //exit program or retry fopen
    } else {
      //continue with program
    }
    
    
    Last edited by GanglyLamb; 08-19-2005 at 05:15 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Yeah i figured out what was wrong heres the finaly Version

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void strip_newline( char *str, int size )
    {
         int i;
         for (  i = 0; 1 < size; ++i )
         {
             if ( str[i] == '\n' )
             {
                  str[i] = '\0';
                  
                  }
                  return;
             }
         }
                  
                  
    int main()
    {
        FILE* Testfile;
        FILE *fp;
        char name[50];
        char lastname[50];
        char fullname[100];
        
        printf( " Please enter your first name name: " );
        fgets( name, 50, stdin );
        strip_newline( name, 50 );
        if ( strcmp ( name, "Alex" ) == 0 )
        {
             printf( "thats my name too.\n" );
             }
             else
             {
                 printf( "Thats not my name :(\n" );
                 }
                 
                 printf( "Your name is %d, letters long", strlen ( name ) );
                 printf( "Enter Your Last Name: " );
                 fgets( lastname, 50, stdin );
                 strip_newline( lastname, 50 );
                 fullname[0] = '\0';
                 strcat( fullname, name );
                 strcat( fullname, " " );
                 strcat( fullname, lastname );
                 strcat( lastname, " " );
                 fp=fopen("C:\\Test.txt", "w");
                 fprintf(fp,  "First Name: %s", name );
                 fprintf(fp, "LastName: %s", lastname  );
                 printf( "Your full name is %s\n",fullname );
                 fclose(fp);
                 getchar();
                 return 0;
                 }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > for ( i = 0; 1 < size; ++i )
    I wonder when this exits the loop?
    Seems to me 1 is always less than 50

    And what's happened to the indentation?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Just a tip, you can replace:
    Code:
    void strip_newline(char *str, int size )
    {
         int i;
         for (  i = 0; 1 < size; ++i )
         {
             if ( str[i] == '\n' )
             {
                  str[i] = '\0';
                  
                  }
                  return;
             }
         }
    with:
    Code:
    void strip_newline(char *str)
    {
      if((str = strchr(str, '\n')))
        *str = '\0';
    }
    I'm not sure what size was for in your original function. The 'proper' way to terminate an iteration on a string is to look for a '\0'. When you've reached that you've reached the end of the string. strchr() can do all the work for you though
    Last edited by itsme86; 08-19-2005 at 09:39 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ( posts per day)
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-05-2008, 12:53 AM
  2. Editing posts
    By pete212 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-25-2008, 06:59 PM
  3. Most Posts per Day
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-08-2001, 09:30 AM

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