Need Help writing Database

This is a description of what i need to do.

Write a C program implement a simple database. Your program will provide text-based menus for a user
interface. Create a “polling loop” to display a menu and prompt for user input. Switch inside the loop out
to various functions.
Keep track of a list of employees (first and last names) and their corresponding salaries. Store your data
in three arrays, one array for the first name, one array for the last name, and one array for the salary. The
program must allow addition of an employee, deletion of an employee, screen printout of the entire
employee list, modification of an employees salary, and saving of all records to a text file.

Now this is what i have done so far. My add function works, and my List function works as well, but the list function malfunctions a little bit and i cant find out why. It lists the names and salary's, but prints a bunch of black lines underneath. There is something going wrong. My Modify and delete functions work, but only for the first input, and i cant seem to modify or delete the second or third employee info. I am close, i just need a little help. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
 #include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"string.h"

void printmenu()
{
	printf("Welcome to database 1.0\n");
	printf("Choose (A)dd, (D)elete, (M)odify, (L)istPress, (F)ind, (S)ave\n\n");
}
void list();
void add();
void modify();
void del();
void save();

#define MAX 5

char lname[MAX][30];
char fname[MAX][30];
double sal[MAX];
void add()
{
	printf("\n");
	int row = 0;
	char buf[30];
	while (lname[row][0] != 0)
		row++;
	strcpy(lname[row], buf);
	printf("please enter last name: ");
	scanf("%s", lname[row]);
	printf("please enter First name: ");
	scanf("%s", fname[row]);
	gets(buf);
	printf("please enter Salary: ");
	scanf("%lf", &sal[row]);
	gets(buf); 
}
void list()
{
	printf("\n");
	for (int row = 0; row < MAX; row++)
	{
		if (lname[row][0] != 0)
		{
			printf("%s\n",lname[row]);
			printf("%s\n", fname[row]);
			printf("%10.2lf\n", sal[row]);
			printf("\n");
		}
	}
}
void del()
{
	char buf[30];
	printf("Please Enter Last name: ");
	gets(buf);
	for (int row = 0; row < MAX; row++)
	{
		if (!strcmp(buf, lname[row]))
			lname[row][0] = 0;
	}
}
void modify()
{
	int row;
	char buf[30];
	printf("Please Enter Last name: ");
	gets(buf);
	for (int row = 0; row < MAX; row++)
	{
		if (!strcmp(buf, lname[row]))
			printf("Please enter New Salary: ");
		scanf("%lf", &sal[row]);
		fflush(stdin);
		return;
	}
		
}
void save()
{
	FILE * f;
	f = fopen("DATABASE", "w");
	for (int row = 0; row < MAX; row++)
	{
		if (lname[row][0] != 0)
		{
			fprintf(f, "%s\n", lname[row]);
			fprintf(f, "%s\n", fname[row]);
			fprintf(f, "%10.2lf\n", sal[row]);
		}
	}
	fclose(f);
}
void main()
{
	printmenu();
	char ch;
	do{
		ch = getche();
		switch (ch)
		{
		case 'A':
			add();
			break;
		case 'L':
			list();
			break;
		case 'D':
			del();
			break;
		case 'M':
			modify();
			break;
		case'S':
			save();
			break;

		}
	} while (ch != 'Q' && ch != 'q');
}

Hi I saw your post while working on my own code and by the looks of it Im guessing your also in cisp 360 with Prof Ross. Heres my code , it works but not perfect. I hope it helps,
If you are in my class and would like to help each other in the future , my name is Jason I always sit in the front row always wear blue New England Patriots hat.
BTW the comments on Add() are Prof Ross , he helped me figure out what I was doing wrong.
-Good Luck


#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"string.h"

void list();
void add();
void modify();
void del();
void save();

#define CRT_SECURE_NO_WARNINGS
#define MAX 20

char lname[MAX][30];
char fname[MAX][30];
double sal[MAX];



void printmenu()
{
printf("\n\ nWelcome to database 1.0\n");
printf("\n Please choose from the following menu options.\n");
printf("\n (A)dd, (D)elete, (M)odify, (L)ist, (S)ave, (Q)uit\n\n");
}




void add()
{

int row = 0;
char buf[30];
// find first blank spot
while (lname[row][0] != 0)
row++;

// prompt user for input
printf("\n\nPlease enter last name: ");
// read user input into a buffer
scanf("%s", buf);
// copy the buffer to the lnames array
strcpy(lname[row], buf);
// prompt user for input
printf("\nPlease enter first name: ");
// read user input into a buffer
scanf("%s", buf);
// copy the buffer to the lnames array
strcpy(fname[row], buf);

// prompt user for input
printf("\nPlease enter salary: ");
// read user and write it to sal array in the same statement
scanf("%lfs", &sal[row]);

printmenu();
}

void list()
{ printf("\n\nFirst name"" ""Last name"" ""Salary");
printf("\n\***************************************************");

for (int row = 0; row < MAX; ++row)
{
if (lname[row][0] != 0)
{


printf(" ""\n%s %s $%.2lf\n", fname[row], lname[row], sal[row]);
printf("\n");



}
}printmenu();
}

void del()
{
char buf[30];
printf("\nPlease Enter Last name: ");
gets(buf);
for (int row = 0; row < MAX; row++)
{
if (!strcmp(buf, lname[row]))
lname[row][0] = 0;
}printmenu();
}

void modify()
{
char buf[30];
gets(buf);
int row = 0;


printf("\n\nPlease Enter Last name: ");
scanf("%s", buf);
if (strcmp(buf, lname[row]))
row++;
else;

printf("Please enter new salary: ");
scanf("%lfs", &sal[row]);
printf("\nYou have succesfully modified this entry");
printf("\n\***************************************************");

printf("\n\nFirst name"" ""Last name"" ""Salary");
printf("\n\***************************************************\n");
printf(" ""\n%s %s $%.2lf\n", fname[row], lname[row], sal[row]);
fflush(stdin);

printmenu();



}



void save ()
{
FILE * f;
f = fopen("DATABASE", "w");

for (int row = 0; row < MAX; row++)
{
if (lname[row][0] != 0)
{
fprintf(f, "%s\n", lname[row]);
fprintf(f, "%s\n", fname[row]);
fprintf(f, "%.2lf\n", sal[row]);
}
}fclose(f);

printmenu();
}

void main()
{
printmenu();
char ch;
do{
ch = getche();
switch (ch)
{
case 'A':
add();
break;
case 'L':
list();
break;
case 'D':
del();
break;
case 'M':
modify();
break;
case'S':
save();
break;


}
} while (ch != 'Q' && ch != 'q');
}
Last edited on
haha, yea im in that class. Thanks a lot for the reply man, it seems that i didnt have my add () function right and that was messing up my entire code. I was just about to email the teacher and ask him for help, your a life saver
Topic archived. No new replies allowed.