![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
I don't know, maybe you should give all you energy into learning JAVA first. I don't think that it's a good idea learning many different prog languages in the same time.
|
|
|
|
|
|
#42 |
|
Newbie
Join Date: Oct 2005
Location: London
Posts: 25
Rep Power: 0
![]() |
Ok. If that is your advice that is what I shall do for now certainly. Thanks, theres lots of java programmers on here to help me learn that better?
__________________
----------------------------------------------------- ________||| Tom |||_______ ----------------------------------------------------- |
|
|
|
|
|
#43 |
|
Professional Programmer
|
If it's C programming you've decided to go with, I'm in. peaceofmind53@gmail is me addy. I'm gonna have a mod move this to the C forum, since it's not really assembly related anymore.
EDIT: add a .com to that address, of course :p
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#44 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
Yes it makes sence to move in the C forum. I will send you my current source code or you can copy/paste a little older version in another thread started by myself.
|
|
|
|
|
|
#45 |
|
Professional Programmer
|
ivan, sorry I didn't get back to you sooner. Here's my email: prm753@operamail.com
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#46 | |
|
Professional Programmer
|
Quote:
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
|
#47 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
I posted here the code for everyone:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /* max number of records */
#define ENTRY 32 /* max number of chars for name, surname and phone */
#define ONE 49
#define TWO 50
#define THREE 51
#define FOUR 52
#define FIVE 53
#define SIX 54
#define SEVEN 55
#define EIGHT 56 /* define ASCII values for numbers 1-8 */
void addRecord();
void delRecord();
int findFree();
void findRecord();
void listPeople();
void save();
void eraseAll();
void menu();
void showHelp();
void showRecord();
int freeSpace();
struct data
{
char name[ENTRY];
char surname[ENTRY];
char phone[ENTRY];
};
struct data People[MAX];
int main()
{
/* Error checking for reading and creating the database file */
FILE *fp;
if ( (fp=fopen("phonebook.dat" , "r")) == NULL)
{
if( (fp=fopen("phonebook.dat" , "w")) == NULL )
{
puts( "Can not create the file! The program will exit\n" );
system( "PAUSE" );
exit(0);
} /* if */
save();
fclose( fp );
} /* if */
fp=fopen("phonebook.dat" , "r");
fread(People , sizeof People , 1 , fp);
if (ferror(fp))
{
puts("An error occurred while reading file! The program will exit\n");
system( "PAUSE" );
fclose(fp);
exit(0);
} /* if */
puts( "**************************" );
puts( "Phonebook 1.0 Beta by Ivan" );
puts( "korhner5@neobee.net" );
puts( "**************************\n\n" );
showHelp();
while (1)
{
rewind( stdin ); /* clears the STDIN */
menu();
} /* while */
} /* main */
void addRecord()
{
int position;
position = findFree(); /* find index of a free space in the array */
if( position == -1 ) /* if there is not any space say that to user */
{
puts( "The list is full!" );
return;
} /* if */
rewind(stdin); /* clears the STDIN */
puts( "Please enter name:" );
fgets( People[position].name, ENTRY, stdin );
rewind( stdin ); /* clears the STDIN */
if( strlen( People[position].name ) <= 1 ) return; /* if the user entered nothing, return */
if ( strlen( People[position].name ) < ENTRY ) /* if the user entered a name with chars < ENTRY, then delete the newline char */
{
People[position].name[strlen( People[position].name ) - 1] = '\0';
} /* if */
while( strlen( People[position].surname ) <= 1 )
{
puts( "Please enter surname:" );
fgets( People[position].surname, ENTRY, stdin );
} /* while */
rewind( stdin ); /* clears the STDIN */
if ( strlen( People[position].surname ) < ENTRY )
{
People[position].surname[strlen( People[position].surname ) - 1] = '\0';
} /* if */
while( strlen( People[position].phone ) <= 1 )
{
puts( "Please enter phone number:" );
fgets( People[position].phone, ENTRY, stdin );
} /* while */
rewind( stdin ); /* clears the STDIN */
if ( strlen( People[position].phone ) < ENTRY )
{
People[position].phone[strlen( People[position].phone ) - 1] = '\0';
} /* if */
} /* addRecord */
void delRecord()
{
char name[ENTRY ];
char surname[ENTRY];
int i;
rewind( stdin ); /* clears the STDIN */
puts( "Please enter name you want to delete: " );
fgets( name, ENTRY, stdin );
puts( "Please enter surname you want to delete: " );
fgets( surname, ENTRY, stdin );
for( i=0; i<MAX; i++ ) /* check for the name and surname the user entered */
{
if( strcmp( People[i].name, name ) == 0 )
{
if ( strcmp( People[i].surname, surname ) == 0 ) *People[i].name = '\0'; /* when found deleted it */
puts( "Deleted!\n" );
} /* if */
} /* for */
} /* delRecord */
int findFree() /* find free index of the array and return it */
{
int i;
for(i=0; i<MAX; i++)
{
if( !*People[i].name )
{
return i;
break;
} /* if */
} /* for */
return -1; /* if list is full return -1 */
} /* findFree */
void findRecord()
{
int i;
char input[30];
rewind( stdin ); /* clears the STDIN */
puts( "Enter what you want to search: " );
gets( input );
if( strlen( input ) == 0 )
{
rewind( stdin );
return;
} /* if */
for( i=0; i<MAX; i++) /* check for user input in names, surnames, and phones */
{
if( (strcmp( People[i].name, input ) == 0) || (strcmp( People[i].surname, input ) == 0) || (strcmp( People[i].phone, input ) == 0) )
{
showRecord(i);
} /* if */
} /* for */
} /* findRecord */
void listPeople() /* list all the records */
{
int i;
for( i=0; i<MAX; i++ )
{
if( *People[i].name ) /* do not show empy records */
{
showRecord(i);
} /* if */
} /* for */
} /* listPeople */
void menu()
{
char sel;
puts( "Enter your choice ( 8 for help ):" );
sel = fgetc( stdin ); /* read a char from STDIN */
rewind( stdin ); /* clears the STDIN */
if( sel<ONE || sel>EIGHT ) /* check if user entered a number between 1 and 8 */
{
puts( "Please enter a number between 1 and 8!\n" );
return;
} /* if */
switch (sel)
{
case ONE:
save();
break;
case TWO:
eraseAll();
break;
case THREE:
addRecord();
break;
case FOUR:
delRecord();
break;
case FIVE:
findRecord();
break;
case SIX:
listPeople();
printf( "\n--------------------------\n" );
printf( "Free space = %4d %3d%% \n", freeSpace(), (100 * freeSpace()) / MAX );
printf( "Used space = %4d %3d%% \n", MAX - freeSpace(), 100 - ((100 * freeSpace()) / MAX) );
printf( "--------------------------\n\n" );
break;
case SEVEN:
exit(0);
case EIGHT:
showHelp();
break;
default:
puts( "Please enter a number between 1 and 8!\n" );
break;
} /* switch */
} /* menu */
void save()
{
FILE *fp;
if ( (fp=fopen("phonebook.dat" , "w")) == NULL)
{ /* handle file errors */
printf("cannot open file\n");
return;
} /* if */
fwrite(People , sizeof People , 1 , fp); /* main instruction that writes the array */
if (ferror(fp)) /* check for file errors */
{
printf("An error occurred while writing file.\n");
fclose(fp);
return;
} /* if */
fclose(fp);
puts("File has been saved!\n");
} /* save */
void eraseAll() /* erase all records( only names ) */
{
int i;
for( i=0; i<MAX; i++)
{
*People[i].name = '\0'; /* clears every name */
} /* for */
puts( "All records have been deleted!\n" );
} /* eraseAll */
void showHelp()
{
puts( "Save...........................1\n" );
puts( "Reset..........................2\n" );
puts( "Add new entry..................3\n" );
puts( "Delete an entry................4\n" );
puts( "Find...........................5\n" );
puts( "List the file, view free space.6\n" );
puts( "Exit...........................7\n" );
} /* showHelp */
void showRecord(int index) /* prints the name, surname and phone of the People[index] */
{
printf("\nName: %s\n", People[index].name );
printf("Surname: %s\n", People[index].surname );
printf("Phone: %s\n\n", People[index].phone );
} /* showRecord */
int freeSpace() /* calulates free space of the array */
{
int i;
int count = 0;
for( i=0; i<MAX; i++)
{
if( !*People[i].name ) count++;
} /* for */
return count;
} /* freeSpace */ |
|
|
|
|
|
#48 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
wow Ivan, I am impressed at your people skills. I know this is sorta off topic, but you are looking like you are a really friendly person, unlike most of the people that have come by lately. Keep it up, you are doing a good job.
@tom: There are some differences, but if you are going to program at all for a living, I would definately suggest learning c and/or c++. It will be a little ackward at first, but once you get a hold of not encapsulating everything, you will find that C/C++ is really easy to work with. |
|
|
|
|
|
#49 |
|
Newbie
Join Date: Oct 2005
Location: London
Posts: 25
Rep Power: 0
![]() |
Hmm.. Maybe I'll try and learn C++ at the same time as java, given that their both object orietated - I can just transfer the techniques by translating the code...
.... (please warn me if this is a ridiculous idea)
__________________
----------------------------------------------------- ________||| Tom |||_______ ----------------------------------------------------- |
|
|
|
|
|
#50 | |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3
![]() |
Quote:
![]() @tomwdrake: You mean translating this to C++??? If so it could be done very easy. For java, I don't know. But I think it is not hard. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|