Thread: File not behaving that way I want it too:

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

    Thumbs down File not behaving that way I want it too:

    Let say the finds the correct last name in the list, it fixes it and when I print it does something funny. If I correct it with the name Jones when I print while the program is running it outputs "ones" but if I hit the space bar and type it "jones" the list will output "jones". For some reason it is dropping the first character.

    Next issue is when I update the file like I said above it appends at the end of the file and prints a whole new node leaving the incorrect one in place and just adding a new correct one. Now I know
    Code:
    ios::app
    does this but if i use anything else the whole file is deleted.

    Code:
    void addressbook::updateFile(MyaddressBook& entry)
    {
        //sending new contacts to out file
        std::ofstream out("addressBookFile.txt", std::ios::app);
        
        if (!out)
        {
      std::cout << "File could not be opened or found." << std::endl;
        }
        else
        {
      out <<
            std::setw(2) << entry.firstName << "\n" <<
            std::setw(2) << entry.lastName << "\n" <<
            std::setw(2) << entry.address << "\n" <<
            std::setw(2) << entry.DOB << "\n" <<
            std::setw(2) << entry.aniversary;
        }
        out.close();
        
    }
    Code:
    void addressbook::readfile(std::ifstream& infile, MyaddressBook& entry)
    {
    	if (!infile) {
      std::cout << "File could not be opened or found." << std::endl;
    	}
        
    	//reading from file and pushing it into a list
    	while (
               getline(infile, entry.firstName, '\n') &&
               getline(infile, entry.lastName, '\n') &&
               getline(infile, entry.address, '\n') &&
               getline(infile, entry.DOB, '\n') &&
               getline(infile, entry.aniversary, '\n')
               )
    	{
      Mylist.push_back(entry);
    	}
        
        
    }
    Code:
    case 2:
            {
                if (Mylist.empty())
                {
                    std::cout << "The list is empty" << std::endl;
                }
                
                std::cout << "Enter the last name of the contact would you like to edit?\n";
                std::string lname;
                std::cin.ignore();
                std::getline(std::cin,lname);
                
                
                for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it != Mylist.end(); ++it)
                {
                    if (it->lastName == lname) //looking for the last name
                    {
                        std::cout << "Enter the new last name?\n";
    
                        std::string Newlast;
                        std::cin.ignore();
                        std::getline(std::cin,Newlast);
                        found=true;
                        it->lastName = Newlast;
                        std::cout << "Last Name updated." << "\n";
                    }
                    
                }
                if (!found)
                {
                    //name could not be found
                    std::cout << "The last name " << lname << " was not found." << std::endl;
                }
                
            }
                break;
    Last edited by jocdrew21; 01-17-2014 at 02:12 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    istream::ignore - C++ Reference
    ignore() without any parameters burns one character of input.

    If you're only using getline() to read input, you don't need ANY cin.ignore calls, unless you're deliberately trying to skip a character.
    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.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    File not behaving? Put it in the naughty corner for some quiet time and if it's still not behaving continue to do so until it does

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Consider watching this video, Bjarne covers a really neat and safe way to handle file open and close in there that would really benefit your code.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    If you're only using getline() to read input, you don't need ANY cin.ignore calls, unless you're deliberately trying to skip a character.
    I didn't use any ignore statements in the read input function.

    Consider watching this video, Bjarne covers a really neat and safe way to handle file open and close in there that would really benefit your code.
    I will watch watch it, thank you. Hopefully it helps.

    When it updates it is repeating the last nodes information too and sometimes in the wrong format. It seems to happen when after I have 5 nodes of information.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    OK this is what I did and it seemed to be doing the trick. I am just wondering if I am making it do more then it needs to. Basically to update the file, the file is opened and therefore erased the current list is iterated and sending the new list to the file all over again.

    Code:
    void addressbook::updateFile(MyaddressBook& entry)
    {
    	//sending new contacts to out file
    	std::ofstream out("addressBookFile.txt");
        
    	if (!out)
    	{
      std::cout << "File could not be opened or found." << std::endl;
    	}
    	else
    	{
            for (auto it = Mylist.begin(); it!=Mylist.end(); ++it) {
                entry.firstName = it->firstName;
                entry.lastName = it->lastName;
                entry.address = it->address;
                entry.DOB = it->DOB;
                entry.aniversary = it->aniversary;
                
                out <<
                std::setw(1) << entry.firstName << "\n" <<
                std::setw(1) << entry.lastName << "\n" <<
                std::setw(1) << entry.address << "\n" <<
                std::setw(1) << entry.DOB << "\n" <<
                std::setw(1) << entry.aniversary<<"\n";
                
            }
      
    	}
    }

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @Salem I am picking up what you are putting down now. I read the link and continuing to search about this issue. The link more or less told me what it did. But I want it to ignore one char, the Enter button then cin the variable. I never had this issue before.....

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @Salem.. Thank you for the get line advice... I didn't know I could so it this way... Thanks a ton

    Code:
    for (std::list<MyaddressBook>::iterator it = Mylist.begin(); it != Mylist.end(); ++it)
                {
                    if (it->firstName == fname) //looking for the first name
                    {
                        std::cout<<"Enter the new name:"<<"\n";
                        char newname[256];
                        std::cin.getline(newname,256);
                        
                        it->firstName = newname;
                        found = true;
                        std::cout << "First Name updated." << "\n";
                    }
                    
                }
                if (!found)
                {
                    //name could not be found
                    std::cout << "The name " << fname << " was not found." << std::endl;
                }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf behaving abnormally
    By greendragons in forum C Programming
    Replies: 2
    Last Post: 06-13-2012, 04:11 PM
  2. waitpid behaving strange...
    By mattholm in forum C Programming
    Replies: 6
    Last Post: 11-28-2011, 01:12 PM
  3. fprintf behaving strangely
    By cwmccart in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2008, 09:56 AM
  4. Printf... behaving Strange...
    By vsriharsha in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 02:38 AM
  5. while loop not behaving properly.
    By Dreamerv3 in forum C++ Programming
    Replies: 20
    Last Post: 01-08-2002, 05:51 PM