Question: // File: record.h struct record { int accountno; char name[25]; char address[80]; struct record* next; }; // This semicolon is important!!! // Prototypes int addRecord
// File: record.h struct record { int accountno; char name[25]; char address[80]; struct record* next; }; // This semicolon is important!!!
// Prototypes int addRecord (struct record **, int, char [ ],char [ ]); int printRecord (struct record *, int); int modifyRecord (struct record *, int, char [ ]); void printAllRecords(struct record *); int deleteRecord(struct record **, int); int readfile(struct record **, char []); void writefile(struct record *, char []);
// Define as a local variable within the main function: struct record *start = NULL;
Requirements:
User interface
You must use a while or do-while loop for the menu.
For each menu option, collect the appropriate information from the user.
Name arrays must allow spaces
Address arrays should be capable of storing multiple-line addresses
Write your own getaddress function to obtain the address information since the address field may have multiple lines of address and must be stored as one character array. You cannot ask the user how many lines of address they are typing.
Prototype for the above function is void getaddress (char [ ], int);
You may create any other functions you like or add to the menu in the interface.
Database functions
addRecord will create a record even if one already exists with the same accountno
addRecord must add the record to the end of the list
modifyRecord will modify all records with the matching accountno
printRecord will print all records with the matching accountno
printAll will print the contents of the entire database
deleteRecord will delete all records, including duplicates based on the accountno.
Readfile should be called once at the start of the main function
Writefile should be called once at the end of the main function
You cannot change the definition of the record datatype
You cannot change the arguments used by the functions shown above.
You cannot create any additional database functions without my consent.
Debug mode
A global variable debugmode must be defined. No other global variables may be used.
Your program must use command-line arguments for debugging.
This program may be invoked by typing the executable name(for example, hw3) or with the option "debug". (e.g. hw3 debug). Anything else such as "hw3 debug test" or "hw3 test" should give an error. The format of the error must be similar to the one you get when you type "cp" in UNIX.
When the program is called using the debug option, additional output will be printed on the screen such as name of function called and parameters passed. This is in addition to everything the program does when called without the debug argument. (You may ignore the printing of pointer arguments)
Every function definition, including the stubs must have output identifying the function called and the parameters passed, when the "debug" option is invoked.
Source files
There must be a minimum of two files, one for the user-interface functions and one file for the database functions.
I basically just need help writing the database functions and implementing them
This is what I have so far:
Main.c
-------------------------
#include
#include
#include
#include "record.h"
void addMenu(struct record **start);
void modifyMenu(struct record *start);
void deleteMenu(struct record **start);
void printRecordMenu(struct record *start);
void getAddress(char address[], int max);
int main(int argc, char *argv[]) {
struct record *start = NULL;
int choice = 0;
int error = 0;
if(argc > 1)
{
if(argc == 2)
{
if(strcasecmp("debug", argv[1]) == 0)
{
DEBUG_MODE = 1;
}
else
error = 1;
}
else
error = 1;
}
if(error)
{
printf(" usage: %s [debug] ", argv[0]);
exit(1);
}
while(choice != 6)
{
printf(" 1. Add a record"); printf(" 2. Modify a record"); printf(" 3. Delete a record"); printf(" 4. Print a record"); printf(" 5. Print all records"); printf(" 6. Quit"); printf(" Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: addMenu(&start);
break;
case 2: modifyMenu(start);
break;
case 3: deleteMenu(&start);
break;
case 4: printRecordMenu(start); break;
case 5: printAllRecords(start);
break;
case 6:
break;
default:
printf(" Invalid menu choice!");
break;
}
}
}
void addMenu(struct record **start)
{
int accno;
char name[25], addr[80];
printf(" Adding a record...");
printf(" Enter account number: ");
scanf("%d", &accno);
getchar();
printf(" Enter name: ");
fgets(name, 25, stdin);
printf(" Enter address lines (empty line to finish): ");
getAddress(addr, 80); addRecord(start,accno,name,addr);
if(addRecord(start, accno, name, addr) == 1)
{
printf(" Record added succefully.");
}
else
{
printf(" Record was not added!");
}
}
void modifyMenu(struct record *start)
{
int accno;
char addr[80];
printf(" Modify a record...");
printf(" Enter account number: ");
scanf("%d", &accno);
getchar();
printf(" Enter new address lines (empty line to finish): ");
getAddress(addr, 80);
if(modifyRecord(start, accno, addr) == 1)
{
printf(" Record modified succefully.");
}
else
{
printf(" Record was not modified!");
}
}
void deleteMenu(struct record **start)
{
int accno;
printf(" Delete a record...");
printf(" Enter account number: ");
scanf("%d", &accno);
if(deleteRecord(start, accno) == 1)
{
printf(" Record deleted succefully.");
}
else
{
printf(" Record was not deleted!");
}
}
void printRecordMenu(struct record *start)
{
int accno;
printf(" Print a record...");
printf(" Enter account number: ");
scanf("%d", &accno);
if(printRecord(start, accno) == 0)
printf(" Record was not found!");
}
void getAddress(char address[], int max)
{
int len = 0;
while(fgets(address+len, max, stdin)) {
if(strlen(address+len) == 1)
break;
len = strlen(address);
max = max - len;
if(max <= 0)
break;
}
if(DEBUG_MODE)
{
printf(" DEBUG: scanned address lines : %s ", address);
}
}
---------------------------------
record.h
----------------------------------
#include
int DEBUG_MODE = 0;
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};
int addRecord (struct record **start, int accno, char name[ ],char address[ ])
{
if(DEBUG_MODE)
{
printf(" DEBUG: addRecord function called with name=%s , address=%s", name, address);
}
return 0;
}
int printRecord (struct record *rec, int accno)
{
if(DEBUG_MODE)
{
printf(" DEBUG: printRecord function called with accno=%d ", accno);
}
return 0;
}
int modifyRecord (struct record *rec, int accno, char address[ ]) \
{
if(DEBUG_MODE)
{
printf(" DEBUG: modifyRecord function called with accno=%d , address=%s", accno, address);
}
return 0;
}
void printAllRecords(struct record *start)
{
if(DEBUG_MODE)
{
printf(" DEBUG: printAllRecords function called");
}
}
int deleteRecord(struct record **start, int accno)
{
if(DEBUG_MODE)
{
printf(" DEBUG: deleteRecord function called with accno=%d", accno );
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
