Thread: C++ Address database program help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    C++ Address database program help

    I have a program here that I am getting errors on and need some guidance on what I need to fix. When I run the program the compiler will not let me append or show a record.



    Code:
     
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
     
    void menu(void);
    void writeData(void);
    void readData(void);
    string * split(string, char);
     
    const char FileName[] = "c:/TestAddress.txt";
     
    int main ()
    {
    	menu();
    	return 0;
    } //end main
     
    void menu(void)
    {	
    	char choice = ' ';
    	cout << "\nWhat Would You Like To Do With These Records: \n\n";
    	cout << "Append Records (A), Show Records (S), or Exit (E)\n"; 
    	 cin >> choice;
     
    	while(choice == 'A' || choice == 'S');
    	{
    		switch(choice)
    		{
    			case 'a':
    			case 'A':
    				writeData();
    				break;
    			case 's':
    			case 'S':
    				readData();
    				break;
    		}
    		cout << "What Else Would You Like To Do?\n";
    		cout << "Append Records (A), Show Records (S), or Exit (E)\n"; 
    		 cin >> choice;
    	}
    }//end menu
     
    void writeData(void)
    {
    	char choice = ' ';
    	string name = "";
    	string street = "";
    	string city = "";
    	string state = "";
    	string zipCode = "";
    	ofstream outMyStream(FileName, ios::app);
     
    	do
    	{
    		cout << "\nEnter The Name: ";
    		getline(cin, name);
    		cout << "\nEnter The Street: ";
    		getline(cin, street);
    		cout << "\nEnter The City: "; 
    		getline(cin, city); 
    		cout << "\nEnter The State: "; 
    		getline(cin, state);
    		cout << "\nEnter The Zip Code: "; 
    		getline(cin, zipCode);
     
    		outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
     
    		cout << "\nEnter another Record? (Y/N) ";
    		cin >> choice;
    	}
    	while (choice == 'Y' || choice == 'Y' ); 
    	outMyStream.close();
    }//end write data
     
    void readData(void)
    {
    	ifstream inMyStream (FileName);
    	string lineBuffer;
    	while (!inMyStream.eof() )
    	{
    		getline (inMyStream, lineBuffer, '\n');
    		string *theFields = split(lineBuffer, ',');
    		cout << "Name...... " << theFields[0] << endl;
    		cout << "Street.... " << theFields[1] << endl;
    		cout << "City...... " << theFields[2] << endl;
    		cout << "State..... " << theFields[3] << endl;
    		cout << "Zip code.. " << theFields[4] << endl;
    	}
    }//end read data
     
    string * split(string theLine, char theDeliminator)
    {
    	//Break theline into fields and save the fields to an array.
    	//Each field will occupy one element in a character array.
    	//theLine is a string with fields separated with theDeliminator character.
    	//Assumes the last field in the string is terminated with a newline.
    	//Useage: string *theFields = split(lineBuffer, ',');
     
    	//determine how many splits there will be so we can size our array
    	int splitCount = 0;
    	for(int i = 0; i < theLine.size(); i++)
    	{
    		if (theLine[i] == theDeliminator)
    		{
    			splitCount++;
    		}
    	}
    	splitCount++; //add one more to the count because there is not an ending comma
    	//create an array to hold the fields
    	string* theFieldArray;
    	theFieldArray = new string[splitCount];
    	//split the string into seperate fields
    	string theField = "";
    	int commaCount = 0;
     
    	for(int i = 0; i < theLine.size(); i++)//read each character and look for the deliminator
    	{
    		if(theLine[i] != theDeliminator) 
    		{
    			theField += theLine[i]; //build the field
    		}
    		else //the deliminator was hit so save to the field to the array
    		{
    			theFieldArray[commaCount] = theField; //save the field to the array
    			theField = "";
    			commaCount++;
    		}
    	}
    	theFieldArray[commaCount] = theField; //the last field is not marked with a comma...
    	return theFieldArray;
    } //end split

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while(choice == 'A' || choice == 'S');
    Watch the ; at the end of the line very carefully.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database Program
    By lukesowersby in forum C Programming
    Replies: 5
    Last Post: 05-17-2009, 03:22 PM
  2. program for a database
    By cluez in forum C Programming
    Replies: 1
    Last Post: 03-29-2009, 09:36 PM
  3. Get the program's address
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 08-23-2006, 12:37 AM
  4. Address program
    By Siggy in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2004, 01:03 AM
  5. database program help
    By lethal_defekt in forum C Programming
    Replies: 3
    Last Post: 05-17-2002, 09:11 PM