Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I had the below C++ question in the recent interview but could not do. Please help.
Given a company structure where every employee reports to a superior, all the way up to the CEO, how would you print out all the employees that a particular individual oversees?
Write a method that implements this, given the below:
// Employee object contains a string denoting the.
// employee name, and an array containing
// employees who report to this employee
Employee {
String name;
Employee[] employees;
}
I have seen and understand the recursive function. But I have not encounter such a recursive object/structure like this one.
My new question is how can an object is created and initialized from this class/structure since it is recursive?
Thank you very much again.
With the information given it is very hard to answer (and question should probably be set on hold). Anyway...
I think a recursive approach is the answer. You need a function that can take a name, search the full employee list for that name and then call the function again for every employee in the local array. Something like:
void doit(Employee& e)
{
// Print e.name
// For each employee tmp in e.employees (i.e. local array)
doit(tmp);
}
Note - this requires that there are no loops in manager-employee arrays. If there is this will be an endless loop. So this is a dangerous approach.
EDIT:
This is added due to the comment from OP.
As indicated above the question is vague and doesn't really give sufficient information to provide a good answer. The struct given in the question is not valid C++ and there is no description of how the company wide list of employees are maintained.
However to keep it simple, the printing could look like this:
struct Employee
{
void PrintAllReportingEmployee(int level)
{
cout << string(4*level, ' ') << name << endl;
level++;
for (auto& e : employeesDirectlyReportingToThisEmployee)
{
e.PrintAllReportingEmployee(level);
}
}
string name;
vector<Employee> employeesDirectlyReportingToThisEmployee;
};
// Code in some print function:
// Step 1 - find the relevant employee
Employee tmp = SomeFunctionReturningAnEmployeeBasedOnName("NameOfPerson");
// Step 2 - print all employees reporting directly and indirectly
tmp.PrintAllReportingEmployee(0);
This assumes a single top-level Employee (e.g. director) with a vector of employees directly reporting to the director. Each of these would then have a vector of employees reporting to them and so. So it is kind of an tree structure.
Note, if I should design a employee db, I would not go with such a solution.
Who ever asked the question was looking for an answer with something to do with class inheritance. So a class Persion is extended by Employee where Person is also extended by Manager etc etc where they all share some similar properties but not everything.
This means that your code can be expanded upon by other programmers and one change can fix many different classes!
Although this code does not demonstrate class inheritance, it will work.
/*
THE OUTPUT:
Jacobs-MacBook-Pro:~ jacob$ ./employee
Foo McGoo
Bar Too
Jacobs-MacBook-Pro:~ jacob$
*/
#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::cout;
using std::endl;
/* define class (this should be in a header file) */
class Employee{
public:
//Variables
string Name;
//Functions
Employee(const string&); //constructor
void AddCoWorker(const Employee&);
void ShowCoWorkers();
private:
vector<Employee> employees;
}; //Note the semicolon
//Entry point
int main(int argc, char **argv){
Employee foo("Foo McGoo");
Employee bar("Bar Too");
Employee me("Bevis");
me.AddCoWorker(foo);
me.AddCoWorker(bar);
me.ShowCoWorkers();
return 0;
}
//Class functions (should be in a separate cpp file)
Employee::Employee(const string& name){
this->Name = name;
}
void Employee::AddCoWorker(const Employee &e){
this->employees.push_back(e);
}
void Employee::ShowCoWorkers(){
int count = this->employees.size();
for(int i = 0; i < count; i++){
//Print the names of each co worker on separate lines
cout << this->employees[i].Name << endl;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to retrieve each element from the list of object in cpp and we have classes like below:
class User{
public:
bool isAvailable;
string value;
};
class Users{
public:
User dateFrom;
User dateTo;
User add1;
User add2;
};
Now somewhere else I have created a list of objects of User like std:: list<User> user-list, stored data and then pushback to the list of the object now I want to get that particular data like dateFrom,dateTo, etc.
user-list.push_back(dateFrom);
user-list.push_back(dateTo);
Now I want to access the element of user-list like what we access in list l1 by index like.
This is just a guess of what you might want:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class User {
public:
bool isAvailable;
string value;
};
int main()
{
User someuser;
someuser.value = "Some user";
someuser.isAvailable = true;
User someotheruser;
someotheruser.value = "Some other user";
someotheruser.isAvailable = true;
std::vector<User> user_list; // <<<< using std::vector here
user_list.push_back(someotheruser);
user_list.push_back(someuser);
cout << user_list[0].value << "\n";
cout << user_list[1].value << "\n";
}
We use a std::vector here instead of a std::list because you mention you wanted to access the elements of the list via an index.
Output is:
Some other user
Some user
Disclaimer: This code is still very poor, for example there are no constructors whatsoever.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have class
class Item
{
private:
string name;
public:
void set_data()
{
getline(cin, name);
}
}
From the main function I am setting the value of the name once, but when I want to give it another value, I can't. I mean that the second time when I call the function from the same object, it does nothing.
First of all, in C++ your implementation should be separate from the declaration unless you are using templates which you are not. This is not Java.
Second, your sample code is missing a closing brace. I have submitted an edit to add it and improve your code formatting.
That aside, the following implementation works for me:
Item.h
class Item
{
private:
std::string name;
public:
void set_data();
void printName();
};
Item.cpp
void Item::set_data()
{
std::cout << "Type name and hit return: " << std::endl;
getline(std::cin, name);
}
void Item::printName()
{
std::cout << "Name is : " << name << std::endl;
}
main.cpp
// Entry point
int main(int argc, char** argv)
{
// Yes I thought I would mix things up and use new instead of Item i; So shoot me.
Item * i = new Item();
i->set_data();
i->printName();
i->set_data();
i->printName();
delete i;
return 0;
}
The application will wait at both calls to set_data() for me to type something in and hit return. Then it continues as normal. I added a text prompt so it is less confusing to see in the console. I get this:
Does this in some way answer your question? If you are doing something else in main() then try stripping it out back to just this simple action then add the other stuff back in until you find the bit that introduces the problem.
UPDATE:
As prompted by the comments below, if you put std::cin >> before another call to getline() it will read the first word from the stream and leave the rest of your string and the \n character in there which getline() uses for its delimiter. So next time you call getline() it will automatically extract the rest of the string from the stream without requesting user input. I guess this is probably what you are seeing.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
For my assignment I have to make a program to read student data from standard input, sort it by last name / first name, and print out the result to standard output. Students consists of a last name, first name, and a grade point average. It says to there are no more than 50 students.
It also says you should not rely on an external library function to do the sort.
Here is the example:
Ware Henry 87.2
Dantes Edmond 91.4
Earhart Amelia 92.6
Here is how it should be sorted:
Dantes Edmond 91.4
Earhart Amelia 92.6
Ware Henry 87.2
Here is the code I have so far that is not working properly:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
student();
void input();
void output();
void sort();
private:
string Fname;
string Lname;
float gpa;
}
student::student()
{
Fname = "";
Lname = "";
gpa = 0;
}
void student::input()
{
cout << "Enter first name, last name, and gpa"<<endl;
cin >> Fname >> Lname >> gpa;
}
void student::sort()
{
char temp;
int count = 0;
int i = 1;
while (i < word.size()) {
if (word[i - 1] > word[i]) {
temp = word[i];
word[i] = word[i + 1];
word[i + 1] = temp;
i++;
if (i >= word.size())
{
alpha(word);
}
}
else
{
count++;
}
}
return word;
}
void main()
{
student.input();
}
Any advice on where I went wrong and any possible solutions?
your student class has member variables to hold only one student, you need 50 instances of class student.
then you should hold these instances in an array/vector (whatever for container you are allowed to use) I assume you need to use a raw array so something like this will do.
student* students = new student[50];
what you then need is a compare function in your class to be able to sort the array, the compare function knows the internals of your class and you can decide how you want to sort the list e.g. after surname.
the sort function could be inside your class declared as a static function or maybe more logically an external function since student is not a container of student instances.
don't forget to delete the array when done
delete [] students;
in real world problems you would use std containers and e.g. algorithm sort for this kind of work.
Your compiler must have told you that word and alpha used in sort method are undeclared identifiers and count is assigned but never used.
Solution: Never ever steal code from wild wild web without understanding what it does. You may take others' code as inspiration, but blind copy-paste is a big no-no.
To give you a next step: you haven't even stored your students, you just input one, how would you sort just one entry? Think (and code) what you should use to store them and start working from there on how to sort them.
Good luck.
There are several reasons why your code is not working, first reason is that your class definition needs semicolon ';' at the end to close it.
class student
{
public:
student();
void input();
void output();
void sort();
private:
string Fname;
string Lname;
float gpa;
}; //semicolon here
In your sort() method the variable word is used but never declared, coding is not magic, the variable must declared and initialized before it is used like in your code.
while (i < word.size()) { //word must be declared somewhere
Also, in the sort method, you call a function alpha(word), this function does not exist in your program.
The sort method is of type void, which means it will not return anything, yet you are trying to return a string.
Another problem with the sort method is that it is never called from anywhere and will never be run.
There is also a big problem here with the way you are trying to use your class. To use the methods of the class you first have to instantiate an object of that class, you can then access the methods of the class through the object like this:
int main()
{
student theStudent;
theStudent;
theStudent.input();
return 0;
}
Another fundamental problem is that you are only going to get data for one object like this, you need to store several objects in something like an array and then sort the objects in the array:
int main()
{
student students[5];
for (int i = 0; i < 5; i++)
{
student newStudent;
students[i] = newStudent;
newStudent.input();
}
sort(students);
delete[] students;
return 0;
}
Which brings us to the sort method. It would be wise to not do the sorting as a method of the student class, but as a function called from main instead (like in the example above), that way it is easy to sort the array of student objects from main.
These are a few advice to start with, i am not going to do your complete homework for you though, if you use google and some of your precious energy, i believe that you will succeed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to use vector to store all Record. The Record class contains student and their age. It supposes to get the command and then call the specific method. However, when I compile it, it said "t is not declared". However, I have already declared as table t. How can I access the private vector without changing the visibility.
class student{
public:
int id;
string name;
student();
student(int,string);
};
class Record{
public:
student student;
int age;
};
class table{
public:
void Insert(student x,int y);
void Print(table t)
private:
vector <Record> records;
};
void Insert(student x,int y){
Record r;
r.student=x;
r.grade=y;
t.records.push_back(r);
}
void Print(table t){
sort( t.record.begin() , t.record.end() );
vector<Record>::iterator itr;
for( itr = t.record.begin() ; itr != t.record.end() ; itr++ )
cout << (*itr).student.id << '\t' << (*itr).student.name << '\t' << (*itr).age << endl;
}
int main (){
student x;
table t;
string command,name;
int id,age;
while ( cin >> command ){
if (command=="Insert"){
cin >> id >> name>> grade;
student s(id,name);
t.InsertStudent(s,grade);
}else if (command == "Print"){
t.Print(t);
}else{return 0;
}
}
The error message is:
t was not declared in this scope in t.records.push_back(r);
I have capitalized the class name and the problem still exist.
There are a significant number of problems with this code. So we'll address the 3 mistakes most closely related to your question: How can I access the private vector without changing the visibility?
You are calling: t.InsertStudent(s,grade). Since you declare table t, that will try to call class table's InsertStudent method. Which there isn't one. You probably intended to call the Insert method.
You define the function void Insert(student x,int y) which was likely intended as the method void table::Insert(student, int y). Note the class scoping on the definition. Alternatively, you could remove the declaration, and just use the definition directly in class scope.
You are trying to call t.records.push_back(r) where t is not a global object that this function would have access to. But presuming from 2 that you intended to define this as a method you would not use an object name to access member variables, instead you could directly access the member variables: records.push_back(r)
I've tried to briefly explain how to fix stuff, but there are some underlying conceptual problems here that need to be addressed, which probably can't be addressed in a couple sentences. Please at least read through: http://www.cplusplus.com/doc/tutorial/classes/ before asking follow up questions. If any of my answer remains unclear after reading through that, feel free to comment below.
As far as other errors in the code start by looking over the line that the compiler issues the waning on. If you can't solve it using that feel free to open a new question posting the code and error.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I wrote a code that works fine. I just want extra eyes to highlight every thing that should/could be improved. I have to create a student.dat file, and write data (name, age, gpa of each student) given by a user, then close it, and reopen it for reading then, displaying the gpa of students. The Student is a Class object. I just want to check where I am with the notion of OOP (at least with that problem). I am using Dev-C++. The code is below:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <iomanip>
#define W setw
using namespace std;
class Student
{
private:
char name[40];
int age;
double GPA;
public:
Student(){};
void read();
void show(char* ,int ,double );
int writefile(ofstream &OS);
double getgpa(double*, int );
void readfile();
};
void Student::read(void)
{
int nbrSt=0;
cout<<"Please enter the information of the student: "<<endl;
cout<<"'y' to Continue, Ctrl+Z to exit! "<<endl;
cout<<"Name, Age and GPA:\n";
ofstream OS("student.dat", ios::out);
while (cin>>name>>age>>GPA)
{
//writing in the file
writefile(OS);
nbrSt++; //increment the number of students
cout<<"Name, Age and GPA:\n";
}
OS.close();
}
int Student::writefile(ofstream & OS)
{
OS<<'\n'<<W(10)<<name<<W(6)<<age<<W(10)<<GPA;
return 0;
}
void Student::show(char* Name, int Age, double gpa)
{
cout<<'\n'<<W(10)<<Name<<W(6)<<Age<<W(8)<<gpa<<endl;
}
double Student::getgpa(double* allGPA, int nbrgpa)
{
double sum=0;
int i =0;
for ( i=0;i<nbrgpa; i++)
sum+=allGPA[i];
if(nbrgpa>0)
return sum/nbrgpa;
}
void Student::readfile()
{
char Name[30];
int Age;
double aveGPA, gpa;
int nbrgpa=0;
double allGPA[50];
int i=0;
ifstream IS("Student.dat", ios::in);
cout<<"reading from Binary file"<<endl;
if (IS.is_open())
while(IS>>Name>>Age>>gpa)
{
nbrgpa++;
show(Name, Age, gpa);
allGPA[i]=gpa;
i++;
}
IS.close();
aveGPA=getgpa( allGPA, nbrgpa);
cout<<"Average GPA of students: "<<aveGPA<<endl;
}
int main(void)
{
Student S;
S.read();
S.readfile();
return 0;
}
Student(){};
is illegal syntax
Student(){}
is correct.
void Student::read(void)
is badly designed. Student::read should be a method which reads one student. The reading of multiple stduents and the writing of students should be in other functions or methods.
int Student::writefile(ofstream & OS)
should be
int Student::writefile(ostream & OS)
so it works with all kinds of streams (not just file streams). Obviously you should then rename the method. Just call it write for instance.
double Student::getgpa(double* allGPA, int nbrgpa)
should not be a member of Student, it should be a global function.
Your main issue is object-orientated design. You shouldn't just add everything to the Student class without thinking about what you are doing. Methods in the Student class should be about one student, methods that read or write one Student for instance. Everything else should be in global functions (or in other classes if you add a second class to your program).
I just want extra eyes to highlight every thing that should/could be improved.
#define W setw
Don't do that. You may think it makes code using setw look simpler, but other people looking at your code will have to search for what W resolves to.
using namespace std;
Don't declare using namespace std globally. It's not a big problem in small projects, but it makes the code more difficult to reuse.
The interface of your class is non-standard. Consider reading by creating ostream << student operator and reading by creating 'istream >> student'. This respects the rule of least surprise and enables you to (for example) read a sequence of students using iteration.
You need much better function names:
Your read function writes to a file. It would be a big WTF moment for me to see that in production code. Either change the name, or change the functionality.
Your read function is called on a student instance (Student s; s.read();) but it doesn't work on an instance. It in fact transfers/stores a set of records from cin to a file, and sets the instance it is called on, to the last record. If the reading fails half way through the data (i.e. cin>>name>>age>>GPA gets the name correctly but not the age or GPA) it leaves the instance it is called on, in an invalid state.
Consider moving your code from char name[40]; and double* allGPA, int nbrgpa to std::string and std::vector<double> respectively. Using raw data is error-prone and unnecessarily complicated.
There's a lot more to be said, but I think I've given you enough :-).