a format problem

This is a discussion on a format problem within the C Programming forums, part of the General Programming Boards category; I have been messing about with structs - I get some user input, which seems to work fine, however when ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    a format problem

    I have been messing about with structs - I get some user input, which seems to work fine, however when I go to print it to the screen (eventually I want to write it to a file as well ) I cannot seem to get the format I want. Here is the code I am trying to use:
    Code:
    printf( "Category\tDate\tPurchase\tAmount\n\n" );
    printf( "%s   %s%d   %s  %.2f",  money_log.category, money_log.month, money_log.day, 
    money_log.purchase_description, money_log.purchase );
    
    /* In the original code, this 
    (above printf statement)  was all one line
    but I hate having horizontal scroll
    in my browser */
    
    }
    And here is what it produces:

    Category Date Purchase Amount

    bleh
    nov
    3 phone bill
    45.00

    Any ideas? And the last number is supposed to be a float number, but it is not printing the actual decimal numbers but makes it zero. In my struct definition, I have it as float type:

    Code:
    struct money{
         char category[MAX];
         char month[MAX];
         int day;
         char purchase_description[MAX];
         float purchase;
    };
    I know it is likely quite simple to fix, but I am not seeing it.

    ~/

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    You pobably used fgets(), which leaves a newline in the string.

    You can use something like:

    Code:
    char *
     get(char buffer[], int max, FILE * stream)
    {
     char * ret = fgets(buffer, max, stream);
     
        if(ret)
       {
        char * crlf = &ret[strlen(ret)-1];
        
           if(*crlf == '\n') *crlf = 0;
       }
        else buffer[0] = 0;
       
     return ret;    
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I take it your use of ret is a stylistic issue? Because it really isn't necessary:
    Code:
    char *
    get(char buffer[], int max, FILE * stream)
    {
      if(fgets(buffer, max, stream))
      {
        char * crlf = &buffer[strlen(buffer)-1];
    
        if(*crlf == '\n')
          *crlf = 0;
      }
      else
        buffer[0] = 0;
       
      return buffer;
    }
    My best code is written with the delete key.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    That was just to preserve the original return value in case fgets() returns null (ie: file input).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Ahh - you are correct, I am using fgets()

    Thanks for the tip

    [edit]

    Ok, I had a look at this and I am a little confused by the usage of the code. At first glance, I thought what you had written for me was a function - the line:
    Code:
    char *
    is not something I understand.

    Once I learned from you that I had to get rid of the newline, I did a search on the board to see if I could get some clarification on the code you had written for me (I figured this was likely a common problem) I only found this thread to be useful for the subject at hand, however none of the solutions suggested there really helped with my understanding of your solution, nor did they help me when i tried a few. Which leads me to believe that I have some problem with my code...

    Anyway, here is the code I am experimenting with lately -
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> 
    #include <ctype.h>
    
    #define CLEAR printf("\033[2J\033[0;0f");
    #define MAX 100
    
    /* structure for writing to and reading from file */
    
    struct money{
         char category[MAX];
         char month[MAX];
         int day;
         char purchase_description[MAX];
         float purchase;
    }; 
    
    void menu( void );
    int get_num( void );
    int validate( char *a );
    void add_record( void );
    int main( void )
    {
         menu();
         return 0;
    }
    
    /* menu() -  display a menu and return the choice of the user */
    void menu( void )
    {
      char choice; 
      printf( "\t\t\tMoney Log \n\n\n" );
      printf( "\t1. Update your record\n\n" );
      printf( "\tPlease enter your selection: " ); 
     
      choice = get_num();
      switch( choice ){
      case 1:
           CLEAR;
           add_record();
           break;
      default:
           break;
      }
     
    }
    
    /* add_record - Read from keyboard and add the record in the list */
    void add_record( void )
    {
         char buffer[MAX];
      
         struct money money_log;
         /*
          * This next bit asks the user for each piece of information,
          * and stores the input in the struct
          */
         printf ("Enter category of purchase: ");
         fgets(money_log.category, MAX, stdin);
         char *p = strchr( buffer, '\n' );  if ( p != NULL ) *p = '\0';
             
         printf ("Enter month (eg, Oct.): ");
         fgets(money_log.month, MAX, stdin);
     
         printf ("Enter day: ");
         fgets(buffer, MAX, stdin);
         money_log.day = atoi( buffer );
    
         printf ("Enter a description of the purchase (eg, phone bill): ");
         fgets(money_log.purchase_description, MAX, stdin);
         
         printf ("Enter the amount of the purchase : ");
         fgets(buffer, MAX, stdin);
         money_log.purchase = atoi( buffer );
        
         CLEAR;
         printf( "Category\tDate\tPurchase\tAmount\n\n" );
         printf( "%s   %s%d   %s  %.2f", money_log.category, money_log.month, money_log.day, 
    money_log.purchase_description, money_log.purchase );
    }
    
    /* get_num() - get a number from the user, and check it for validity */
    int get_num( void )
    {
         int choice;
         char buffer[BUFSIZ];
         
         if( fgets ( buffer, sizeof buffer, stdin ) != NULL ){
    	  buffer[strlen ( buffer ) - 1] = '\0';
    	  if( validate( buffer ) == 0 ){
    	       choice = atoi( buffer );
    	  }
    	  else
    	       printf( "\nPlease enter a numerical character." );
         }
         else
    	  printf( "Error reading input\n" ); 
         
         return choice;
    }     
    
    /* validate() - connected to get_num() */
    int validate( char *a )
    {
         unsigned x;
         for ( x = 0; x < strlen ( a ); x++ )
    	  if( !isdigit ( a[x] ) ) return 1;
         
         return 0;
    }
    Some of you might recognise some code snippets :P - I felt free to use some stuff I found on here, cause it works so nicely.
    Last edited by kermit; 11-22-2003 at 06:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me solve this format problem...
    By kas2002 in forum C Programming
    Replies: 12
    Last Post: 08-02-2006, 02:46 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 12:06 PM

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