R
Rob Somers
Hey all
I am writing a program to keep track of expenses and so on - it is not
a school project, I am learning C as a hobby - At any rate, I am new
to structs and reading and writing to files, two aspects which I want
to incorporate into my program eventually. That aside, my most
pressing problem right now is how to get rid of the newline in the
input when I use fgets(). Now I have looked around on the net, not so
much in this group as on another C board, and I have found several
good solutions for getting rid of the newline character, however I
cannot seem to make it work for me, and now my program is becoming
more unmanageable as I go along, trying to fix the problem. I will
post the code, and I will comment in the part that I am specifically
not understanding.
#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[MAX];
char purchase_description[MAX];
float purchase[MAX];
};
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];
char *p = NULL;
struct money money_log;
/* This next bit is supposed to ask the user for each piece of
information,and store the input in the struct - It is where my problem
lies I think */
printf ( "Enter category of purchase: " );
fgets( buffer, MAX, stdin );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
strcpy( buffer, money_log.category );
fflush( stdout );
printf( "Enter month (eg, Oct.): " );
fgets( buffer, MAX, stdin );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
strcpy( buffer, money_log.month );
fflush( stdout );
printf( "Enter day: " );
fgets( buffer, MAX, stdin );
money_log.day = atoi( buffer );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
fflush( stdout );
/* I have been experimenting here (above) with different things, and I
get a pile of warnings on my compiler which I will post also. */
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;
}
Here are warnings gcc gives me when I try to compile:
another.c: In function `add_record':
another.c:62: warning: assignment makes pointer from integer without a
cast
another.c:63: invalid operands to binary *
another.c:63: parse error before ';' token
another.c:70: warning: assignment makes pointer from integer without a
cast
another.c:71: invalid operands to binary *
another.c:71: parse error before ';' token
another.c:77: incompatible types in assignment
another.c:78: warning: assignment makes pointer from integer without a
cast
another.c:79: invalid operands to binary *
another.c:79: parse error before ';' token
another.c:88: incompatible types in assignment
another.c:92: warning: int format, pointer arg (arg 4)
another.c:92: warning: double format, pointer arg (arg 6)
The reason the newline is bothering me is that I would like the data
to print out in one line, instead of many. At any rate, if any of you
could set me straight on getting rid of the newline with fgets() in
this particular case, I would be grateful.
Rob Somers
BTW, I am not *as* worried about this program being portable (hence
the #define CLEAR statement ) as it is only meant to run on my own
machine here at home.
I am writing a program to keep track of expenses and so on - it is not
a school project, I am learning C as a hobby - At any rate, I am new
to structs and reading and writing to files, two aspects which I want
to incorporate into my program eventually. That aside, my most
pressing problem right now is how to get rid of the newline in the
input when I use fgets(). Now I have looked around on the net, not so
much in this group as on another C board, and I have found several
good solutions for getting rid of the newline character, however I
cannot seem to make it work for me, and now my program is becoming
more unmanageable as I go along, trying to fix the problem. I will
post the code, and I will comment in the part that I am specifically
not understanding.
#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[MAX];
char purchase_description[MAX];
float purchase[MAX];
};
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];
char *p = NULL;
struct money money_log;
/* This next bit is supposed to ask the user for each piece of
information,and store the input in the struct - It is where my problem
lies I think */
printf ( "Enter category of purchase: " );
fgets( buffer, MAX, stdin );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
strcpy( buffer, money_log.category );
fflush( stdout );
printf( "Enter month (eg, Oct.): " );
fgets( buffer, MAX, stdin );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
strcpy( buffer, money_log.month );
fflush( stdout );
printf( "Enter day: " );
fgets( buffer, MAX, stdin );
money_log.day = atoi( buffer );
if( ( p = strchr( buffer, '\n' ) != NULL )
*p = '\0';
fflush( stdout );
/* I have been experimenting here (above) with different things, and I
get a pile of warnings on my compiler which I will post also. */
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;
}
Here are warnings gcc gives me when I try to compile:
another.c: In function `add_record':
another.c:62: warning: assignment makes pointer from integer without a
cast
another.c:63: invalid operands to binary *
another.c:63: parse error before ';' token
another.c:70: warning: assignment makes pointer from integer without a
cast
another.c:71: invalid operands to binary *
another.c:71: parse error before ';' token
another.c:77: incompatible types in assignment
another.c:78: warning: assignment makes pointer from integer without a
cast
another.c:79: invalid operands to binary *
another.c:79: parse error before ';' token
another.c:88: incompatible types in assignment
another.c:92: warning: int format, pointer arg (arg 4)
another.c:92: warning: double format, pointer arg (arg 6)
The reason the newline is bothering me is that I would like the data
to print out in one line, instead of many. At any rate, if any of you
could set me straight on getting rid of the newline with fgets() in
this particular case, I would be grateful.
Rob Somers
BTW, I am not *as* worried about this program being portable (hence
the #define CLEAR statement ) as it is only meant to run on my own
machine here at home.