Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write this programming assignment in C++! (The directions are as followed) You\'

ID: 3777015 • Letter: W

Question

Write this programming assignment in C++!

(The directions are as followed)

You're a developer for a startup called Study Buddies that matches study buddies for classes depending on the experience of each user. For this Program you must:

create a class called User that contains the following:

name

classification

major

expert class list (what classes the user feels they are an expert in)

mutators and accessors for each private variable

printUsers function that prints all the users of the system

matchStudyBuddy function that matches users based on classes they have in common. (Meaning two or more classes that match)

read a set of users from a file userData.txt

read in a whole line from the file

parse the line, you will have to change the delimiter of the getline statement to be comma delimited.

Explanation / Answer

For this program to run use the following format for userData.txt

(name,classifciation,major, expert class list separated by spaces)

Ex:

stanley,2,3,m1 m2 m3

fustan,3,4,m4 m5

stanley,2,3,m1 m2 m3

Code:

#include<iostream>
#include<sstream>
#include<string>
#include<list>
#include<fstream>
using namespace std;
#include<stdlib.h>

class User
{
string name;
int classification;
int major;
list<string> expertise;

public:

void setName(string &n){name.assign(n);}
void setClassification(int c){classification=c;}
void setMajor(int m){major=m;}
void setExpertise(string &n){expertise.push_back(n);}

string & getName(){ return name;}
int getClassification(){return classification;}
int getMajor(){return major;}
list<string>::iterator getExpertise(){return expertise.begin();}

void printInfo(){
   cout<<getName()<<endl;
   cout<<getClassification()<<endl;
   cout<<getMajor()<<endl;
   list<string>::iterator iter=getExpertise();
   while(iter!=expertise.end()){
   cout<<*iter<<",";
   iter++;
   }

   cout<<endl;
}
};

class StudyBuddies
{
list<User> ll;

public:

   void printUsers(){

   for(list<User>::iterator itr=ll.begin();itr!=ll.end();itr++)
       itr->printInfo();
   }

   void addUser(User &u){ ll.push_back(u);}

   void readFile(string & fn){
   ifstream ifs;
   char buf[256];
   string tok;
   int c,m;
   list<string> myList;

   ifs.open(fn.c_str(),ifstream::in);

   while(ifs.good()){
       User u;
       ifs.getline(buf,256,' ');

       stringstream ss(buf);
      
       getline(ss,tok,',');
       u.setName(tok);

       getline(ss,tok,',');
       u.setClassification(atoi(tok.c_str()));

       getline(ss,tok,',');
       u.setMajor(atoi(tok.c_str()));  

       while(getline(ss,tok)){
           u.setExpertise(tok);
       }

       addUser(u);
      
   }
   }

void matchStudyBuddies(){
   for(list<User>::iterator itr=ll.begin();itr!=ll.end();itr++)
   {
       for(list<User>::iterator iter2=ll.begin();iter2!=ll.end();iter2++)
       {
           if(itr==iter2) continue;
           if((itr->getName()==iter2->getName()) && (itr->getClassification()==iter2->getClassification()) && (itr->getMajor()==iter2->getMajor())){
               cout<<"Objects having "<<itr->getName()<<" are equal"<<endl;
           }
      
       }
   }
}
};

int main(void)
{

string fN("userData.txt");
StudyBuddies sb;
sb.readFile(fN);
sb.printUsers();
sb.matchStudyBuddies();

return 0;
}