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: 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.