When I use acouple scanf()s or structure arrays and fill them with information then try to view that information it gets all messed up. Sometimes over written or new things get added and i can't figure out why??? Can someone please help? An example is this program I just started writing. Im only half done with it but in just testing it im confused why this happens.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
void view(int file);
void add(int file);
void del(int file);
void edit(int file);
struct employees
{
char fname[10];
char lname[10];
char dob[10];
char city[2];
} user[10];
int main()
{
int choice, file;
char temp[5], done;
do {
printf("\t\tView=1\tAdd=2\tDel=3\tEdit4\tExit=5\n\n:");
choice=atoi(gets(temp));
if(choice==5)
exit(0);
printf("File#:");
file=atoi(gets(temp));
printf("\n");
if(choice==1)
view(file);
else if(choice==2)
add(file);
else if(choice==3)
del(file);
else if(choice==4)
edit(file);
else {
printf("Error!");
exit(0); }
printf("Are you finished? (y or n):");
done=getche();
printf("\n");
}
while(toupper(done)=='N');
return 0;
}
void view(int file)
{
printf("%s %s\n%s\n%s\n", user[file].fname, user[file].lname, user[file].dob, user[file].city);
printf("\n");
}
void add(int file)
{
printf("First Name:");
gets(user[file].fname);
printf("Last Name:");
gets(user[file].lname);
printf("DOB:");
gets(user[file].dob);
printf("City:");
gets(user[file].city);
printf("User Entered!\n");
}
void del(int file)
{
strcpy(user[file].fname, " ");
strcpy(user[file].lname, " ");
strcpy(user[file].dob, " ");
strcpy(user[file].city, " ");
printf("File deleted!\n");
}
void edit(int file)
{ }