Question

(Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

(Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.
           **fall.sort(compare_DOB); //Here lies your error**

#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <cctype>
using namespace std;
const char THIS_TERM[] = "fall.txt";
const string HEADER = "\nWhat do you want to do ?\n\n"
"\t. Add a new friend name and DOB ? \n"
"\t. Edit/Erase a friend record ? \n"
"\t. Sort a Friend's record ? \n"
"\t. View all in memory at present ? \n"
"\t. Update file with data currently in memory ? \n"
"\t. Load in data from file ? \n"
"\t. exit program ? ";
class Friend
{public:
   Friend(string nam, string num)
   {setName(nam);
       setDOB(num);}
   void setName(string nam) { name = nam; }
   void setDOB(string num) { DOB = num; }
   string getName() { return name; }
   string getDOB() { return DOB; }
private:
   string name, DOB;};
list< Friend >::iterator existDOB(list < Friend >& term, string& DOB)
{list< Friend >::iterator it;
   for (it = term.begin(); it != term.end(); ++it)
   {if (it->getDOB() == DOB)
           return it;}
   return term.end();}
int newFriend(list< Friend >& term)
{cout << "\nEnter an empty record to exit this 'Input Loop' ..." << endl;
   int count, reply;
   string nam, num;
   for (;; )
   {cout << "\nDOB : ";
       getline(cin, num);
       cout << "Name : ";
       getline(cin, nam);
       if (nam == "" || num == "")
           break;
       cout << "Add or Redo (a/r) ? ";
       reply = cin.get();
       cin.sync();
       if (toupper(reply) != 'A')
       {cout << "Aborted ..." << endl;
           continue;}
       term.push_back(Friend(nam, num));
       ++count;
       cout << "Added ..." << endl;}
   return count;}
void viewFriend(list< Friend >& term)
{list< Friend >::iterator it;
   int i = 0;
   for (it = term.begin(); it != term.end(); ++it)
   {cout << ++i << ".\n"
           << "Name : " << it->getName() << endl
           << "Date of Birth : " << it->getDOB() << endl;}}
int fileFriend(list< Friend>& term)
{ofstream fout(THIS_TERM);
   if (!fout)
   {cout << "\nError attempting to open file ..." << endl;
       return -1;}
   list< Friend >::iterator it;
   int i = 0;
   for (it = term.begin(); it != term.end(); ++it)
   {fout << it->getName() << "," << it->getDOB() << endl;
       ++i;}
   fout.close();
   if (i == (int)term.size())
       cout << "\nAll " << i << " records filed ok." << endl;
   else
       cout << "\nOnly " << i << " of " << term.size()
       << " records were written ..." << endl;
   return i;}
int readFriend(list < Friend >& term)
{ifstream fin(THIS_TERM);
   if (!fin)
   {cout << "Error attempting to open file ... "
           << "(Perhaps it dosen't exist yet?)" << endl;
       return -1;}
   if (term.size()) // i.e. if not == ...
   {cout << "\nDo you want over-write the " << term.size()
           << " records in memory (y/n) ? " << flush;
       int reply = toupper(cin.get());
       cin.sync();
       if (reply != 'Y')
       {cout << "Aborted ... " << flush;
           return 0;}}
   list< Friend>temp;
   term = temp;
   string nam, num;
   int i;
   for (i = 0; getline(fin, nam, ','); ++i)
   {getline(fin, num, '\n');
       term.push_back(Friend(nam, num));}
   fin.close();
   return i;}
bool editFriend(list< Friend>& term)
{cout << "\nEnter an empty record to exit this 'Edit/Erase Function' ..."
       << "\nEnter the DOB of the Friend's record to edit ? " << flush;
   string DOBStr, nam;
   getline(cin, DOBStr);
   list< Friend >::iterator i;
   list< Friend >::iterator index;
   i = existDOB(term, DOBStr);
   if (i == term.end())
   {cout << "This '" << DOBStr << "' does not exist." << endl;
       return false;}
   cout << "Name : " << i->getName() << endl
       << "DOB : " << i->getDOB() << endl
       << "Ok to edit/erase (y/n) ? " << flush;
   int reply = toupper(cin.get());
   cin.sync();
   if (reply != 'Y')
   {cout << "Aborted ... " << endl;
       return false;}
   cout << "\nDo you want to erase this record (y/n) ? " << flush;
   reply = toupper(cin.get());
   cin.sync();
   if (reply == 'Y')
   {   term.erase(i);
       cout << "Erased ..." << endl;
       return true;   }
   cout << "\nNew DOB : ";
   getline(cin, DOBStr);
   index = existDOB(term, DOBStr);
   if (index != term.end() && index != i)
   {   cout << "\nThat " << DOBStr << " already exits ... " << endl;
       return false;}
   cout << "New Name : ";
   getline(cin, nam);
   if (nam == "" || DOBStr == "")
       return false;
   cout << "Ok or Redo (o/r) ? ";
   reply = cin.get();
   cin.sync();
   if (toupper(reply) != 'O')
   {   cout << "Aborted ..." << endl;
       return false;}
   i->setName(nam);
   i->setDOB(DOBStr);
   cout << "Edited ..." << endl;
   return true;}
void pauseForEnter()
{cout << "\nPress 'Enter' to continue ... " << flush;
   cin.sync();
   cin.get();}
int main()
{list <Friend > fall;
   int count = readFriend(fall);
   if (count >= 0)
       cout << count << " Friend's record(s) read into memory ..." << endl;
   else
       cout << "(The file will be created when some Friend's records exist.)"
       << endl;
   bool changes = false;
   int reply;
   for (;; )
   {cout << HEADER;
       reply = toupper(cin.get());
       cin.sync();
       if (reply == ' ' || reply == 'A')
       {int new_Friend = newFriend(fall);
           cout << endl << new_Friend << " Friend's record(s) added ..."
               << " The total number of Friend's records now is "
               << fall.size() << endl;
           if (new_Friend)
               changes = true;   }
       else if (reply == ' ' || reply == 'E')
       {if (editFriend(fall))
               changes = true;   }
       else if (reply == ' ' || reply == 'S')
       {cout << "\nSort by DOB? " << flush;
           reply = toupper(cin.get());
           cin.sync();
           cout << "Now sorted in memory by ";
           if (reply == 'I')
           {   fall.sort(compare_DOB); //Here lies your error
               cout << "DOB. ";   }
           cout << "Update file (y/n) ? " << flush;
           reply = toupper(cin.get());
           cin.sync();
           if (reply == 'Y')
           {changes = true;
               cout << "File to be updated ...\n";   }       }
       else if (reply == ' ' || reply == 'V')
           newFriend(fall);
       else if (reply == ' ' || reply == 'U')
       {   if (!changes)
               cout << "\nNo changes to file ..." << endl;
           else
           {   cout << "Are you sure you want to update the file (y/n) ? "
                   << flush;
               reply = toupper(cin.get());
               cin.sync();
               if (reply == 'Y')
               {if (fileFriend(fall) != (int)fall.size())
                       cout << "\nERROR! NOT all records were filed!" << endl;
                   else
                   {   changes = false;
                       cout << "File write operation confirmed." << endl;   }               }           }       }
       else if (reply == ' ' || reply == 'L')
       {   int condition = readFriend(fall);
           if (condition >= 0)
               cout << "\nThere were " << condition
               << " Friend's records read into memory from file." << endl;}
       else if (reply == ' ' || reply == 'X')
       {   if (changes)
               fileFriend(fall);
           break;   }
       else
       {cout << "\nThis choice not implemented yet ... " << endl;}}
   string tmpSee = " ";
   tmpSee = "notepad" + tmpSee + THIS_TERM;
   system(tmpSee.c_str());}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Edited Code that works without errors is as follows-
Edited Code has been hightlightd.

#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <cctype>
using namespace std;
const char THIS_TERM[] = "fall.txt";
const string HEADER = "\nWhat do you want to do ?\n\n"
"\t. Add a new friend name and DOB ? \n"
"\t. Edit/Erase a friend record ? \n"
"\t. Sort a Friend's record ? \n"
"\t. View all in memory at present ? \n"
"\t. Update file with data currently in memory ? \n"
"\t. Load in data from file ? \n"
"\t. exit program ? ";
class Friend
{public:
Friend(string nam, string num)
{setName(nam);
setDOB(num);}
void setName(string nam) { name = nam; }
void setDOB(string num) { DOB = num; }
string getName() { return name; }
string getDOB() { return DOB; }
private:
string name, DOB;};
list< Friend >::iterator existDOB(list < Friend >& term, string& DOB)
{list< Friend >::iterator it;
for (it = term.begin(); it != term.end(); ++it)
{if (it->getDOB() == DOB)
return it;}
return term.end();}
int newFriend(list< Friend >& term)
{cout << "\nEnter an empty record to exit this 'Input Loop' ..." << endl;
int count, reply;
string nam, num;
for (;; )
{cout << "\nDOB : ";
getline(cin, num);
cout << "Name : ";
getline(cin, nam);
if (nam == "" || num == "")
break;
cout << "Add or Redo (a/r) ? ";
reply = cin.get();
cin.sync();
if (toupper(reply) != 'A')
{cout << "Aborted ..." << endl;
continue;}
term.push_back(Friend(nam, num));
++count;
cout << "Added ..." << endl;}
return count;}
void viewFriend(list< Friend >& term)
{list< Friend >::iterator it;
int i = 0;
for (it = term.begin(); it != term.end(); ++it)
{cout << ++i << ".\n"
<< "Name : " << it->getName() << endl
<< "Date of Birth : " << it->getDOB() << endl;}}
int fileFriend(list< Friend>& term)
{ofstream fout(THIS_TERM);
if (!fout)
{cout << "\nError attempting to open file ..." << endl;
return -1;}
list< Friend >::iterator it;
int i = 0;
for (it = term.begin(); it != term.end(); ++it)
{fout << it->getName() << "," << it->getDOB() << endl;
++i;}
fout.close();
if (i == (int)term.size())
cout << "\nAll " << i << " records filed ok." << endl;
else
cout << "\nOnly " << i << " of " << term.size()
<< " records were written ..." << endl;
return i;}
int readFriend(list < Friend >& term)
{ifstream fin(THIS_TERM);
if (!fin)
{cout << "Error attempting to open file ... "
<< "(Perhaps it dosen't exist yet?)" << endl;
return -1;}
if (term.size()) // i.e. if not == ...
{cout << "\nDo you want over-write the " << term.size()
<< " records in memory (y/n) ? " << flush;
int reply = toupper(cin.get());
cin.sync();
if (reply != 'Y')
{cout << "Aborted ... " << flush;
return 0;}}
list< Friend>temp;
term = temp;
string nam, num;
int i;
for (i = 0; getline(fin, nam, ','); ++i)
{getline(fin, num, '\n');
term.push_back(Friend(nam, num));}
fin.close();
return i;}
bool editFriend(list< Friend>& term)
{cout << "\nEnter an empty record to exit this 'Edit/Erase Function' ..."
<< "\nEnter the DOB of the Friend's record to edit ? " << flush;
string DOBStr, nam;
getline(cin, DOBStr);
list< Friend >::iterator i;
list< Friend >::iterator index;
i = existDOB(term, DOBStr);
if (i == term.end())
{cout << "This '" << DOBStr << "' does not exist." << endl;
return false;}
cout << "Name : " << i->getName() << endl
<< "DOB : " << i->getDOB() << endl
<< "Ok to edit/erase (y/n) ? " << flush;
int reply = toupper(cin.get());
cin.sync();
if (reply != 'Y')
{cout << "Aborted ... " << endl;
return false;}
cout << "\nDo you want to erase this record (y/n) ? " << flush;
reply = toupper(cin.get());
cin.sync();
if (reply == 'Y')
{ term.erase(i);
cout << "Erased ..." << endl;
return true; }
cout << "\nNew DOB : ";
getline(cin, DOBStr);
index = existDOB(term, DOBStr);
if (index != term.end() && index != i)
{ cout << "\nThat " << DOBStr << " already exits ... " << endl;
return false;}
cout << "New Name : ";
getline(cin, nam);
if (nam == "" || DOBStr == "")
return false;
cout << "Ok or Redo (o/r) ? ";
reply = cin.get();
cin.sync();
if (toupper(reply) != 'O')
{ cout << "Aborted ..." << endl;
return false;}
i->setName(nam);
i->setDOB(DOBStr);
cout << "Edited ..." << endl;
return true;}
void pauseForEnter()
{cout << "\nPress 'Enter' to continue ... " << flush;
cin.sync();
cin.get();}

bool compare_DOB(Friend a, Friend b){
   return(a.getDOB()<b.getDOB());
}

int main()
{list <Friend > fall;
int count = readFriend(fall);
if (count >= 0)
cout << count << " Friend's record(s) read into memory ..." << endl;
else
cout << "(The file will be created when some Friend's records exist.)"
<< endl;
bool changes = false;
int reply;
for (;; )
{cout << HEADER;
reply = toupper(cin.get());
cin.sync();
if (reply == ' ' || reply == 'A')
{int new_Friend = newFriend(fall);
cout << endl << new_Friend << " Friend's record(s) added ..."
<< " The total number of Friend's records now is "
<< fall.size() << endl;
if (new_Friend)
changes = true; }
else if (reply == ' ' || reply == 'E')
{if (editFriend(fall))
changes = true; }
else if (reply == ' ' || reply == 'S')
{cout << "\nSort by DOB? " << flush;
reply = toupper(cin.get());
cin.sync();
cout << "Now sorted in memory by ";
if (reply == 'I')
{ fall.sort(compare_DOB); //Here lies your error
cout << "DOB. "; }
cout << "Update file (y/n) ? " << flush;
reply = toupper(cin.get());
cin.sync();
if (reply == 'Y')
{changes = true;
cout << "File to be updated ...\n"; } }
else if (reply == ' ' || reply == 'V')
newFriend(fall);
else if (reply == ' ' || reply == 'U')
{ if (!changes)
cout << "\nNo changes to file ..." << endl;
else
{ cout << "Are you sure you want to update the file (y/n) ? "
<< flush;
reply = toupper(cin.get());
cin.sync();
if (reply == 'Y')
{if (fileFriend(fall) != (int)fall.size())
cout << "\nERROR! NOT all records were filed!" << endl;
else
{ changes = false;
cout << "File write operation confirmed." << endl; } } } }
else if (reply == ' ' || reply == 'L')
{ int condition = readFriend(fall);
if (condition >= 0)
cout << "\nThere were " << condition
<< " Friend's records read into memory from file." << endl;}
else if (reply == ' ' || reply == 'X')
{ if (changes)
fileFriend(fall);
break; }
else
{cout << "\nThis choice not implemented yet ... " << endl;}}
string tmpSee = " ";
tmpSee = "notepad" + tmpSee + THIS_TERM;
system(tmpSee.c_str());}

If the answer helped then please upvote, it means a lot.
And for any queries feel free to comment.

Add a comment
Know the answer?
Add Answer to:
(Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT