Can Someone Explain Why My Getter Function Doesn't Call? - c++

I'm writing some code to create a student class and a menu that allows the user to input and display information. Im running into some issues getting my GetName function to call. Here is my code:
#include <iostream>
using namespace std;
class Student {
string FirstName, LastName, Birthday;
int MNumber, Age;
float GPA;
public:
void GetName(){
cout << "Please enter your First Name." << endl;
cin >> FirstName;
cout << "Please enter your Last Name." << endl;
cin >> LastName;
cout << FirstName << ", " << LastName << endl;
}
};
int main () {
Student GetName;
return 0;
}
}

You're not calling the "Getname" method inside main program.
#include <iostream>
using namespace std;
class Student {
string FirstName, LastName, Birthday;
int MNumber, Age;
float GPA;
public:
void GetName(){
cout << "Please enter your First Name." << endl;
cin >> FirstName;
cout << "Please enter your Last Name." << endl;
cin >> LastName;
cout << FirstName << ", " << LastName << endl;
}
};
int main () {
Student getName;
getName.GetName();
return 0;
}

Related

How can I access elements of a vecotor in C++

Actually, I am rookie, and I started to learning this language a few months age. Now, I confronted with an insoluble issue. I devised a program to get information of numerous employee's specification and store it in a vector. In order to acquire better performance, I designed a class named "Employee", and defined a function to give some options to user, like adding new employee, promotion a specific employee's salary and so on.
I defined my matching criterion through operators, but there is an error in program, and that is when I want to display, the output is completely weird. it looks like a some kind of Chinese letter :-)
here is my code:
Thanks
My major problem is when it comes displaying employee's specification
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
using namespace std;
class Employee
{
public:
Employee(string FirstName, string LastName, int age , double Salary, bool Status ) ://constructor
fName(FirstName), lName(LastName), EmpAge(age), EmpSalary(Salary), RecruitmentStatus(Status) {};
void setSalary(const double&);
void SetRecruitmentStatus(const bool&);
void Promote(const double&);
//====================================//
operator const char* ()
{
ostringstream formattedOutput;
formattedOutput << this->fName.c_str() << " " << (this->lName.c_str()) << " | " << this->EmpAge << " | " << this->EmpSalary << ((RecruitmentStatus) ? "Working" : "Fired");
string output = formattedOutput.str();
return output.c_str();
}
bool operator == (const Employee& anotherEmployee)const
{
return (this->lName == anotherEmployee.lName);
}
bool operator < (const Employee& anotherEmployee)const
{
return (this->EmpSalary < anotherEmployee.EmpSalary);
}
private:
string fName, lName;
int EmpAge;
double EmpSalary;
bool RecruitmentStatus;
};
void Employee::setSalary(const double& income)
{
this->EmpSalary += income;
}
void Employee::SetRecruitmentStatus(const bool& Status)
{
this->RecruitmentStatus = Status;
}
void Employee::Promote(const double& Promotion)
{
this->EmpSalary += Promotion;
}
template <typename T>
void displayElements(T& input)
{
for (auto iterator = input.begin();
iterator != input.end(); iterator++)
{
cout << *iterator << endl;
}
}
void selection(int&);
Employee getNewEmp(void);
int main()
{
vector<Employee> Records;
int ID;
do
{
selection(ID);
switch (ID)
{
case 1:
{
Records.push_back(getNewEmp());
break;
}
case 2:
{
cout << "please enter last name of the employee you want to change his/her recruitment'status: ";
string lastName; cin >> lastName; cout << endl;
auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());
break;
}
case 3:
{
cout << "please enter last name of the employee you want to promote his/her recruitment'salary: ";
string lastName; cin >> lastName; cout << endl;
auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());
break;
}
default:
{
if (ID !=4)
{
cout << "Wrong selection!\nThe program will be closed after a few seconds\n";
exit(1);
}
}
}
} while (ID != 4);
displayElements(Records);
cout << "Thanks for using this program\n";
return 0;
}
void selection(int& input)
{
system("cls");
cout << "please Choose one of these option:\n"
"1. add a new employee\n"
"2. changing an employee status\n"
"3. Promote an employee's salary\n"
"4. exit : ";
cin >> input;
}
Employee getNewEmp(void)
{
system("cls");
string firstName, lastName;
cout << "first name:"; cin >> firstName; cout << endl;
cout << "last name:"; cin >> lastName; cout << endl;
int age;
cout << "Age: "; cin >> age; cout << endl;
double salary;
cout << "Offered Salary: "; cin >> salary; cout << endl;
bool Status;
cout << "Is he/she working(1) or got fired(0) : "; cin >> Status; cout << endl;
return Employee(firstName, lastName, age, salary, Status);
}

How should we add function inside a structure?

#include <iostream>
using namespace std;
struct teacher
{
int id;
char name[50];
int salary;
void input(teacher a)
{
cout << "Enter Name : ";
cin >> a.name;
cout << "Enter ID : ";
cin >> a.id;
cout << "Enter Salary : ";
cin >> a.salary;
}
void output(teacher b)
{
cout << "Your Name Is : " << b.name << endl;
cout << "Your ID Is : " << b.id << endl;
cout << "Your Salary Is : " << b.salary;
}
};
int main()
{
teacher t;
t.input(t);
t.output(t);
return 0;
}
Is there any problem? The output is random numbers, don't know what is it.
I tried writing the output function separately, but still same results.
This seems like a weird design, why don't your class methods directly operate on this?
struct teacher
{
int id;
std::string name;
int salary;
void input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter ID : ";
cin >> id;
cout << "Enter Salary : ";
cin >> salary;
}
void output() const
{
cout << "Your Name Is : " << name << endl;
cout << "Your ID Is : " << id << endl;
cout << "Your Salary Is : " << salary;
}
};
Then main would look like
int main()
{
teacher t;
t.input();
t.output();
return 0;
}
Also I'd prefer to use std::string instead of char[] when possible.
In input() you modify parameter a which goes out of scope when it reaches the end of the function.
Instead you should modify the class members variables themselves.
void input()
{
cout << "Enter Name : ";
cin >> name;
cout << "Enter ID : ";
cin >> id;
cout << "Enter Salary : ";
cin >> salary;
}

I'm trying to get the input for variables in private members of a class

this is my code
#include <iostream>
#include <string>
using namespace std;
class Hamoud{
private:
char name[50];
int yearbirthday;
public:
float tall;
int age;
void Getnameandyear(string name)
{
std::array<char, 50> ;
cout<<"enter your name: ";
getline(nama);
int year;
year = yearbirthday;
cout<<"enter your name: ";
getchar(name);
cout<<"Enter your year of birth";
cin>>year;
}
void display()
{
cout<<"your name is: "<<name;
cout<<"your year of birth is : "<<year;
}
};
int main ()
{
Hamoud info;
cout<<"enter your tall ";
cin>>info.tall;
cout<<"your tall is : "<<info.tall;
cout<<"enter your age: ";
cin>>info.age;
cout<<"your age is: "<<info.age;
info.Getnameandyear();
info.display();
}
but i got errors in the function getnameandyear also in display function as well...
i know that to access the private members of the class we have to create a function in public that will help us on access then indirectly....
However, I'm getting stuck on the last few steps..
Any idea of how to solve this problem ??
You probably want something like this:
#include <iostream>
#include <string>
using namespace std;
class Hamoud {
private:
string name;
int yearbirthday;
public:
float tall;
int age;
void Getnameandyear()
{
// no need for std::array<char, 50> or whatever here anyway
cout << "enter your name: ";
cin.ignore(); // this clears the input buffer. The exact reason why
// we need this is beyond your scope for the moment
getline(cin, name);
cout << "Enter your year of birth: ";
cin >> yearbirthday;
}
void display()
{
cout << "your name is: " << name << "\n";
cout << "your year of birth is: " << yearbirthday << "\n";
}
};
int main()
{
Hamoud info;
cout << "enter your tall ";
cin >> info.tall;
cout << "your tall is: " << info.tall << "\n";
cout << "enter your age: ";
cin >> info.age;
cout << "your age is: " << info.age << "\n";
info.Getnameandyear();
info.display();
}
But anyway, I think you should start reading good beginner's C++ text book.
About the cin.ignore(); before getline: the exact reason is a bit beyond your scope for the moment, but you can find detailed information about this issue in this SO article.

How to read first and last name with void function

I can easily create a void function to output something like a header, but I cannot get a program to read a user's input and then output it to them.
ie. Have a program say "Please enter your first name" and then output "You entered: (what they put)"
An example of what I have:
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<fstream>
using namespace std;
void namefn()//namefn prototype
int main(){
string firstName,lastName;
return 0;
}
void namefn(string firstName, string lastName){
cout<<"please enter your first and last name "<<endl;
}
Use cin to read, is in iostream.
i.e.
cin >> firstName;
cout << "enter your first and last name:";
cin >> firstName >> lastName;
cout << firstName << " " << lastName;
if you want the program to display the first name and last name separately then this:
cout << "enter you first name:";
cin >> firstName;
cout << firstName << endl;
cout << "Enter your last name:";
cin >> lastName;
cout << lastName << endl;
Try this:
#include <iostream>
#include <string>
void displayFirstAndLast(const std::string& firstName, const std::string& lastName)
{
std::cout << "You are " << firstName << " " << lastName << std::endl;
}
int main(){
std::string firstName;
std::string lastName;
std::cout << "Please enter your first and last name " << std::endl;
std::cin >> firstName >> lastName;
displayFirstAndLast(firstName, lastName);
return 0;
}
I think, that you want something like this.
Or with something like "header"
#include <iostream>
#include <string>
void displayFirstAndLast(const std::string& firstName, const std::string& lastName);
int main(){
std::string firstName;
std::string lastName;
std::cout << "Please enter your first and last name " << std::endl;
std::cin >> firstName >> lastName;
displayFirstAndLast(firstName, lastName);
return 0;
}
void displayFirstAndLast(const std::string& firstName, const std::string& lastName)
{
std::cout << "You are " << firstName << " " << lastName << std::endl;
}

Scope and accessor functions with second constructors?

So basically my professor gave us the class student and told us to implement all functions and write a main for the function. I am trying to do this but I am having trouble figuring out the errors that are arising, in all of my accessor functions it is telling me that nCredits, gpa, and s aren't declared in the scope. I assume this has something to do with the second constructor, but I am not sure what exactly the issue is. The second constructor and the displayStRec function were also provided to our class, I just don't know how to do this program with the variables in the member function being renamed as they have been.
#include <iostream>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits(int nCredits);
int getCredits();
void changeGPA(double gpa);
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student()
{
name = "No name yet";
GPA = 0;
credits = 0.0;
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits)
{
name = s;
GPA = gpa;
credits = nCredits;
cout << "Second constructor entered!" << endl;
}
void student::changeCredits(int nCredits){
cout << "Enter credits earned:" << endl;
cin >> nCredits;
}
int student::getCredits(){
return nCredits;
}
void student::changeGPA(double gpa){
cout << "Enter GPA: " << endl;
cin >> gpa;
}
double student::getGPA(){
return gpa;
}
void student::displayStRec()
{
cout << " Name: " << s <<endl;
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits(nCredits);
s2.getCredits();
s2.changeGPA(gpa);
s2.getGPA();
s2.displayStRec();
return 0;
}
You've got three variables that you're using that aren't declared. I'll hit them one by one.
Missing s
void student::displayStRec()
{
cout << " Name: " << s <<endl;
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
In this function, you are trying to output some variable s that isn't declared within the scope of displayStRec. You used s in the constructor to initialize the name, copying it to member variable name. Well, s is gone now! But name persists for the lifetime of the object. So, just use name instead.
void student::displayStRec()
{
cout << " Name: " << name <<endl; //Change it here
cout << " GPA: " << gpa <<endl;
cout << "Credits: " << nCredits <<endl;
}
Missing nCredits and gpa
In your main function you're using two variables that aren't declared.
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits(nCredits); //What nCredits?
s2.getCredits();
s2.changeGPA(gpa); //What gpa?
s2.getGPA();
s2.displayStRec();
return 0;
}
This leads to the question... what are you trying to change their credits to? What are you trying to change their gpa to? In other words, shouldn't your program have input parameters? Perhaps somewhere in your assignment your professor told you what values to provide. You will need constants of some kind, or you will need to gather user input from cin.
Oh and by the way, this makes zero sense
void student::changeGPA(double gpa){
cout << "Enter GPA: " << endl;
cin >> gpa;
}
This function will accept user input and update the variable gpa, which is declared in local scope and immediately discarded. Not very useful.
Also, it accepts an input value (double gpa) but also collects input from cin. Why do both? Can't update it to two values at the same time!
My guess is that is supposed to update the object's GPA, which is declared in member scope. Like this:
void student::changeGPA(){
cout << "Enter GPA: " << endl;
cin >> GPA;
}
or
void student::changeGPA(double gpa){
GPA = gpa;
}
Conclusion
One of the following must be true:
You copied the code examples incorrectly
The professor gave you a trick question
The professor is sort of clueless or perhaps a bit overworked
I've gone ahead and removed the sloppy second constructor functions my professor is trying to force on us and this program now works fine:
#include <iostream>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits();
int getCredits();
void changeGPA();
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student()
{
name = "No name yet";
GPA = 0;
credits = 0.0;
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits)
{
name = s;
GPA = gpa;
credits = nCredits;
cout << "Second constructor entered!" << endl;
}
void student::changeCredits(){
cout << "Enter credits earned: ";
cin >> credits;
}
int student::getCredits(){
return credits;
}
void student::changeGPA(){
cout << "Enter GPA: ";
cin >> GPA;
}
double student::getGPA(){
return GPA;
}
void student::displayStRec()
{
cout << " Name: " << name <<endl;
cout << " GPA: " << GPA <<endl;
cout << "Credits: " << credits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1,s2("John",4.0,42);
s2.changeCredits();
s2.getCredits();
s2.changeGPA();
s2.getGPA();
s2.displayStRec();
return 0;
}
here is a suggested solution however I don't know what your teacher orders:
!: add string to inclusion
2: use only member data not non members
3: either make changeCredits and changeGPA take 0 argument and inside it ask for inputs or input outside and and pass to it.
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
student();
student(string s, double gpa, int nCredits);
~student(); //destructor
void changeCredits();
int getCredits();
void changeGPA();
double getGPA();
void displayStRec();
private:
string name;
double GPA;
int credits;
};
student::student() :
name("No name yet"), GPA(0.0), credits(0)
{
cout << "Default constructor entered!" << endl;
}
student::student(string s, double gpa, int nCredits) :
name(s), GPA(gpa), credits(nCredits)
{
cout << "Second constructor entered!" << endl;
}
void student::changeCredits()
{
cout << "Enter credits earned:" << endl;
cin >> credits;
cout << endl;
}
int student::getCredits()
{
return credits;
}
void student::changeGPA()
{
cout << "Enter GPA: " << endl;
cin >> GPA;
cout << endl;
}
double student::getGPA()
{
return GPA;
}
void student::displayStRec()
{
cout << " Name: " << name <<endl;
cout << " GPA: " << GPA <<endl;
cout << "Credits: " << credits <<endl;
}
student::~student()
{
cout << "Destructor entered!" << endl;
}
int main()
{
student s1, s2("John", 4.0, 42);
s2.changeCredits();
s2.getCredits();
s2.changeGPA();
s2.getGPA();
s2.displayStRec();
return 0;
}