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