Thread: reading from a txt file to a linked list:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    reading from a txt file to a linked list:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    #include "contacts.h"
    
    using namespace std;
    
    int main()
    {
    
        addressBook MyaddressBook;
        
        ofstream outfile;
        outfile.open("/Users/peterjocham/Desktop/textoutput.txt",ios::app);
        ifstream infile("/Users/peterjocham/Desktop/textoutput.txt",ios::in);
        
        if (outfile.fail()) {
            cerr<<"File couldn't be opened";
            exit(EXIT_FAILURE);
        }
        
        int userInput=0;
        
        nodePtr temp=new node;
        string NAME;
        MyaddressBook.userPromptStatement();
        
        while (!outfile.eof()) {
            infile>>temp->name>>temp->last>>temp->age;
            
        }
        
        
        while(1)
        {
      cin>>userInput;
            
            if (!cin || (userInput!=1 && userInput!=2 && userInput!=3 && userInput!=4 &&userInput!=5))
            {
                cin.ignore();
                cout<<"Only enter a 1,2,3,4, or 5"<<endl;
                cout<<"You have entered an incorrect value."<<endl;
                cout<<endl<<endl;
                MyaddressBook.userPromptStatement();
                continue;
            }
                switch (userInput) {
                    case 1:
                        
                            cout<<"NAME: ";
                            cin.ignore();
                            getline(cin,temp->name,'\n');
                    
                            cout<<"\nLAST NAME: ";
                            getline(cin,temp->last,'\n');
                            
                            cout<<"\nAGE: ";
                            cin>>temp->age;
                        
                        outfile<<
                        "FIRST NAME: "<<setw(2)<<temp->name<<"\n"
                        <<setw(2)<<"LAST NAME: "<<temp->last<<"\n"
                        <<setw(2)<<"AGE: "<<temp->age<<"\n\n";
                        
                            MyaddressBook.addContact(temp);
                            MyaddressBook.userPromptStatement();
                            cout<<"\n"<<"\n";
                            break;
                        
                    case 2:
                        cout<<"Which contact would you like to delete?\n";
                        cin>>NAME;
                        
                        MyaddressBook.deleteName(NAME);
                        MyaddressBook.userPromptStatement();
                        cout<<"\n"<<"\n";
                        break;
                    case 3:
                        MyaddressBook.editName(NAME);
                        break;
                    case 4:
                        MyaddressBook.printContactList();
                        MyaddressBook.userPromptStatement();
                        cout<<"\n"<<"\n";
                        break;
                    case 5:
                        outfile.close();
                        exit(EXIT_SUCCESS);
                        
                    default:
                        break;
                }
            }
    
        return 0;
    }
    Lines 29-32 I have a good feeling very wrong. Now I have never learned how to do this and my book covers nothing over this. I just took my final in C++ so this is not homework. I am trying to get better before Data Structures start next month.

    Can I please have a pointer?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's an okay start. The infile>> line needs to be the bit inside the while() parentheses (since an eof check is not right, even if you were checking the right file). Then inside the curly braces you need to build an item and add it to your list.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Please..... I know this is not a place for answers but google is leaving me with more questions then answers. Do you have any code references online I can read on this matter?

    I seen a few pieces of code on this but again I had more questions and I do not want to just start plugging in code and seeing what happens and not understand what the hell I am doing...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well are you supposed to be building your own list class or using the one that comes with C++? If the former, you don't seem to have started yet; if the latter, look up std::list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list and file reading.
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 03-22-2012, 07:11 PM
  2. reading in file to linked list?
    By rickyson49 in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 09:07 AM
  3. Reading from a file into a linked list.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 01-23-2006, 08:24 AM
  4. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 07:08 AM
  5. reading from a file to linked list
    By opacity in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2002, 09:56 AM