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";
}

what compiler r u using for these code?
i've tried many times to get student id and their name then sort in a file
using borland.
can u help me?

here r the code

#include <iostream>
#include <conio.h>
#include <iomanip.h>
#include <string>
#include <fstream>

int main()
{

      //declare file stream
     ifstream inFile;
     ofstream outFile;

     //open the files

     inFile.open ("D:\\notepad\\stulist.txt");//open the file input
     outFile.open ("D:\\notepad\\outstulist.txt");//open the file output

    //declare variables
    int i,Id,test1,test2,test3,ave;
   string name;

   //looping
   for (i=1;i<=20;i++)
   {

        inFile>>Id>>name>>test1>>test2>>test3>>ave;
    outFile<<Id<<name<<test1<<test2<<test3<<ave;
    cout<<i<<"\t"<<Id<<"\t"<<name<<"\t"<<test1<<"\t"<<test2<<"\t"<<test3;
      cout<<"\t"<<ave;
    cout<<endl;
    //display output of file
   }

   if (inFile)
   {
      ave=(test1+test2+test3)/3;
   }
   //file cannot read
   if(!inFile)
   {
   cout<<"Cannot open the file!"<<endl;
   }
   //close file
    inFile.close();
    outFile.close();

   getche();


}

im newbie in C++.i dont know how to sort this

tq.great appreciation

Edited 3 Years Ago by Dani: Formatting fixed

My initial problem is fixed and now I have another, when using char and strcmp, my code does not allow for the user to input more than one word per line. Like address cannot have spaces in it. How can I alleviate the situation with changing the char to strings and not have an error with strcmp?? Otherwise if I change the char's to strings I get this error.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' i:\312 2\binary search tree\binary search tree\menu.cpp 119

Here is my code:

#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 address 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;
};

tree_node *root;

/*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_inorder();
void inorder(tree_node* p);

int main()
{
	#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 != 3) 
	{
		/*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, 100);
			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_inorder();
		}
		/*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_inorder()
{
	/* ofstream constructor opens file */
	ofstream outTreeFile;
	outTreeFile.open("contacts.txt");
	inorder(root);
	if (!outTreeFile)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}// end if
	string lastname;
	string firstname;
	string address;
	string city;
	string state;
	string zipcode;
	string number;
	string email;

	while( cin >> lastname >> firstname >> address >> city >> state >> zipcode >> number >> email)
	{
	outTreeFile << lastname << ' ' << firstname << ' ' << address << ' ' << city << ' ' << state << ' ' << zipcode 
		<< ' ' << number << ' ' << email << ' ' << endl;
	}// end while

}

void inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<' ' << endl;
        if(p->right) inorder(p->right);
    }
    else return;
}

My initial problem is fixed and now I have another, when using char and strcmp, my code does not allow for the user to input more than one word per line. Like address cannot have spaces in it. How can I alleviate the situation with changing the char to strings and not have an error with strcmp?? Otherwise if I change the char's to strings I get this error.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' i:\312 2\binary search tree\binary search tree\menu.cpp 119

Here is my code:

#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 address 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;
};

tree_node *root;

/*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_inorder();
void inorder(tree_node* p);

int main()
{
	#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 != 3) 
	{
		/*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, 100);
			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_inorder();
		}
		/*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_inorder()
{
	/* ofstream constructor opens file */
	ofstream outTreeFile;
	outTreeFile.open("contacts.txt");
	inorder(root);
	if (!outTreeFile)
	{
		cerr << "File could not be opened" << endl;
		exit(1);
	}// end if
	string lastname;
	string firstname;
	string address;
	string city;
	string state;
	string zipcode;
	string number;
	string email;

	while( cin >> lastname >> firstname >> address >> city >> state >> zipcode >> number >> email)
	{
	outTreeFile << lastname << ' ' << firstname << ' ' << address << ' ' << city << ' ' << state << ' ' << zipcode 
		<< ' ' << number << ' ' << email << ' ' << endl;
	}// end while

}

void inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<' ' << endl;
        if(p->right) inorder(p->right);
    }
    else return;
}

hello, can i have its running example.i dont have that MS visio...and how can install it.?

This article has been dead for over six months. Start a new discussion instead.