Dating service program in c++ using text file, and linked lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • weebies
    New Member
    • Jun 2013
    • 2

    #1

    Dating service program in c++ using text file, and linked lists

    Hello, I'm VERY new to programming, and I'm working on my second c++ program right now. Its a dating service where you read data from an input .txt file and store into a linked list, so then you can search, and modify the data. The text file is in this format:

    M Dr.Gregory House,237-8732 7 Vicodin,Guitar, Piano,Motorcycl es,Television,F ood,Whiteboards .endl; (all on a single line).

    First, is the sex (M or F), then the person's name, phone number, number of interests, then a list of their interests (with commas between each one, and a period at the end.) and then if they have a match you put their name there and put endl; after.

    The main problem I'm having is setting up the link list, I have no idea how to get it to read those as variables in the text, I know you have to use delimiters, but I can't quite figure out how to use them. You also have to keep two lists, one for males, and one for females in the output file. How do I do this? Is that a double linked list?

    Any help would really be appreciated. Thank you!

    Here is the code so far (I know it isn't much..)

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <limits>
    #include <cstring>
    
    using namespace std;
    
    //Functions
    void echoPrint();
    void newClient();
    void unMatch();
    void printMatch();
    void printFree();
    void quitAsking();
    void processing();
    
    //Initialize Variables
    string command = ""; // User command answer
    std::string clientSex;
    std::string clientName;
    std::string clientPhone;
    std::string clientNumInt;
    std::string clientInterests;
    std::string match;
    
    
    ifstream inputfile;
    char Clients[] = "Clients.mf.txt"; // Input file
    ofstream outputfile;
    char Dates[] = "Dates.out.txt"; // Output file
    
    
    int main(){
    
    inputfile.open(Clients, ios::in);
    //If input file can't open, read out this
    if (!inputfile) {
       cerr << "Can't open input file " << Clients << endl; 
       exit(1);
    }
    
    outputfile.open(Dates, ios::out);
     // If output file can't open, read out this
    if (!outputfile){
    	cerr << "Can't open output file " << Dates << endl;
    	exit(1);
    }
    
    	//Questions & command list
    	cout << "Hello, how may I help you today?" << endl;
    	cout << endl;
    	cout << "Here are a list of commands I can do:" << endl;
    	cout << endl;
    	cout << "NEWCLIENT- Add a new client to my database" << endl;
    	cout << "UNMATCH- Remove client's current match" << endl;
    	cout << "PRINTMATCH- Print a list of all matched clients" << endl;
    	cout << "PRINTFREE- Prints the names and numbers of all clients without matches" << endl;
    	cout << "QUIT- This will exit the program." << endl;
    	cout << endl;
    	cout << "Please choose a command from above." << endl;
    
    	//Sends program to processing function
    	processing(); 
    
    do{
    	cout << "What would you like to do now? Please enter another command." << endl;
    	// Sends program to processing function
    	processing();
    }while(command == "QUIT" || command == "quit");
    
    }// Closes main
    
    
    void processing(){
    do{
    	   cin >> command;
    
    	    if (command == "NEWCLIENT" || command == "newclient"){
    				newClient();
    	   }else if
    	   (command == "UNMATCH" || command == "unmatch"){
    				unMatch();
    	   }else if
    	   (command == "PRINTMATCH" || command == "printmatch"){
    				printMatch();
    	    }else if
    	    (command == "PRINTFREE" || command == "printfree"){
    				printFree();
    		}else if
    		(command == "QUIT" || command == "quit"){
    				quitAsking();
    		}else 
    		{
    			cout <<"****ERROR**** Please choose a command from above." << endl;
    			// Only allows error message to print once
    			cin.ignore( numeric_limits<int>::max() , '\n' );
    		}
    }while(!(command == "NEWCLIENT" || command == "newclient" || command == "UNMATCH" || command == "unmatch" ||
    		 command == "PRINTMATCH" || command == "printmatch" || command == "PRINTFREE" || command == "printfree" ||
    		 command == "QUIT" || command == "quit"));
    
    // ECHO PRINTING HERE
    	
    }// Closes processing
    
    			
    void newClient(){
    
    	//Client's Sex
    	cout << "Please enter client's sex, F for female, M for male." << endl; // Ask for client's sex
    	cin >> clientSex;
    	
    	//Client's Name
    	cout << "Please enter client's name, no more than 20 characters." << endl; // Ask for client's name
    	cin >> clientName;
    
    	//Client's Phone
    	cout << "Please enter client's phone number in XXX-XXXX format." << endl; // Ask for client's phone number
    	cin >> clientPhone;
    
    	//Client's Number of Interests
    	cout << "Please enter number of client's interests (Can have up to 10)." << endl;// Ask for number of interests
    	cin >> clientNumInt;
    
    	//Client Interests
    	cout << "Please enter client's interests. Place ',' between each (10 characters each) and a '.' after the last one." << endl;
    	cin >> clientInterests;
    
    }// Closes NEWCLIENT
    
    
    void unMatch(){
    char unClientName;
    
    
    	cout << "Please enter the client's name you would like to unmatch." << endl;
    	cin >> unClientName;
    
     
    
    }
    
    void printMatch(){}
    
    void printFree(){}
    
    void quitAsking(){}
    Last edited by weebies; Jun 16 '13, 08:43 PM. Reason: To add code
  • weebies
    New Member
    • Jun 2013
    • 2

    #2
    Please, any help at all?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Since you are using the C++ Standard Library, there is a linked list set up for you already. That means you don't need to design your own linked list.

      Read up on #include <list>.

      Next, the linked list does not read data. It just stores data in the order received.

      So, write a function to read a record from your text file and have the function return it as a std::string.

      Then, check the beginning of the string for M or F. Hint: std::string is implemented as an array, so [0] is the first element. If that element is M add the string to the male linked list. Otherwise, add it the female linked list.

      Do the above two steps in a loop until the file is processed.

      To add to a linked list:
      Code:
      //create your linked lists:
      list<string> Mlist;    //male list
      list<string> Flist;    /female list;
      string str;
      //code here to put a value in str;
      
      //then add the string to a linked list:
      
      if str[0] == "M"
           Mlist.push_back(str];     //adds string str to the end of the list
      OK. Enough hints...

      Comment

      Working...