I'm writing code for an address book that the information will be output to a text file in alphabetical order. I have most of the code, and I have done this kind of thing before, but it has been over a year since coding. So, just a start would be greatly appreciated.
#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
//#pragma warning(disable : 4996) to negate the 17 warnings I was getting from
// buliding this code.
/*Create phone book entry structure.*/
typedef struct addbentry {
char lastname[25];
char firstname[25];
char address[30];
char city[30];
char state[2];
char zipcode[7];
char number[11];
char email[45];
} Entry;
/*Create tree node structure.*/
struct tree_node {
Entry data;
struct tree_node *left;
struct tree_node *right;
};
/*Necessary functions.*/
struct tree_node * insert(struct tree_node *p, Entry e);
struct tree_node * create_node (struct tree_node *q, struct tree_node *r, Entry e);
void print();
int main(void)
{
#pragma warning(disable : 4996)
int option = 0; /*Variable for option selection.*/
Entry e; /*Basic nume book entry*/
struct tree_node *p = NULL; /*Basic tree node*/
/*Return to menu after each instruction until the user quits.*/
while (option != 6) {
/*Show user the option menu.*/
printf("** Address Book Menu **\n");
printf("1. Add\n");
printf("2. List\n");
printf("3. Quit\n");
/*Get option from the user.*/
printf("\nPlease select an option: ");
scanf("%d", &option);
/*If option is 1 (Add):*/
if (option == 1) {
/*Take in subject data from the user.*/
printf("Please enter the last name: ");
scanf("%s", &e.lastname);
printf("Please enter the first name: ");
scanf("%s", &e.firstname);
printf("Please enter the address: ");
scanf("%s", &e.address);
printf("Please enter the city: ");
scanf("%s", &e.city);
printf("Please enter the state, Abbreviation please: ");
scanf("%s", &e.state);
printf("Please enter the zip code: ");
scanf("%s", &e.zipcode);
printf("Please enter the phone: ");
scanf("%s", &e.number);
printf("Please enter the email: ");
scanf("%s", &e.email);
/*Create a new node.*/
p = insert(p, e);
/*Confirm node creation.*/
printf("Record added successfully.\n\n");
}
/*If option is 2 (List):*/
else if (option == 2) {
print();
}
/*If option is 3 (Quit):*/
else if (option == 3) {
/*End the program.*/
break;
}
/*If the user does not select an existing option.*/
else {
/*Print error message.*/
printf("That option does not exist. Please try again.\n\n");
}
}
return 0;
}
/*Adds a node to the tree.*/
struct tree_node * insert(struct tree_node *p, Entry e) {
/*If there is no root:*/
if (p == NULL) {
/*Create a root.*/
p = create_node(NULL, NULL, e);
}
/*If there is a root, and the entry belongs before the root:*/
else if (strcmp(e.lastname, p->data.lastname) < 0) {
/*Add before root.*/
p->left = insert(p->left, e);
}
/*If there is a root, and the entry belongs after the root:*/
else if (strcmp(e.lastname, p->data.lastname) > 0) {
/*Add after root.*/
p->right = insert(p->right, e);
}
/*Return revised tree.*/
return p;
}
/*Creates a new node.*/
struct tree_node * create_node (struct tree_node *q, struct tree_node *r, Entry e) {
struct tree_node* newnode;
newnode = (struct tree_node*)(malloc(sizeof(struct tree_node)));
newnode->data = e;
newnode->left = q;
newnode->right = r;
return newnode;
}
/*Prints contents of tree.*/
void print()
{
ofstream fp_out;
fp_out.open("contact.txt", ios::out);
if (fp_out.is_open())
{
fp_out << endl;
fp_out.close();
}
else cout << "Unable to open file";
}