C++ Class getting input - c++

I wanna ask you quick question. How can get the name, surname, id and age as an input? These are must be taken as input and variables must be private. How should I code about it?
#include <iostream>
using namespace std;
class Employee{
private:
string Name;
string Surname;
int IdNumber;
int age;
public:
Employee(string isim,string soyisim,int idnumarasi,int yas){
Name = isim;
Surname = soyisim;
IdNumber = idnumarasi;
age = yas;
}
void printEmployee(){
cout << Name << endl;
cout << Surname << endl;
cout << IdNumber <<endl;
cout << age << endl;
}
};
int main() {
Employee employee("John","Lares",12,25);
employee.printEmployee();
return 0;
}
Edited version. I used getline but still cant reach the private variables. How can reach the private variables. Is the only way getter setter functions?
#include <iostream>
using namespace std;
class Employee{
private:
string Name;
string Surname;
int IdNumber;
int age;
public:
Employee(){
Name = isim;
Surname = soyisim;
IdNumber = idnumarasi;
age = yas;
}
void printEmployee(){
cout << Name << endl;
cout << Surname << endl;
cout << IdNumber <<endl;
cout << age << endl;
}
};
int main() {
Employee employee();
getline(cin,employee.Name);
getline(cin,employee.Surname);
getline(cin,employee.IdNumber);
getline(cin,employee.age);
employee.printEmployee();
return 0;
}

One simple way is to use the constructor you have defined
int main()
{
string name, surname;
int id, age;
cin >> name >> surname >> id >> age; // read user input
Employee someone(name, surname, id, age); // create employee from user input
...
}
Using a constructor is the normal way to give class member variables their initial values.
Another more complicated way is to overload operator>>

Related

How to update the members of objects in the array constructed from another class?

I have 2 separate classes in C++,which are Algorithms class and Student class, each of have the following members and methods.
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
int quiz_scores[4];
public:
Student()
{
ID = 0;
name = "" ;
surname = "";
quiz_scores[4] = {0};
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
void setQuizScores(int* quizscores);
const int* getQuizScores() const;
};
and Algorithms class as follows:
class Algorithms{
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Algorithms()
{
students[num] = {};
weightQ = 0.3;
weightHW = 0.3;
weightF = 0.4;
}
int getNum(); // Returns how many students are in the course
void addNewStudent(Student new_student);
And here is the methods declerations of Student class and Algorithms class respectively.
// Method declerations for the class Student
void Student :: setID(int ID_set){
ID = ID_set;
}
int Student :: getID(){
return ID;
}
void Student :: setName(string name_set){
name = name_set;
}
string Student :: getName(){
return name;
}
void Student :: setSurName(string surname_set){
surname = surname_set;
}
string Student :: getSurName(){
return surname;
}
void Student :: setQuizScores(int* quizscores){
for(int i = 0; i<4; i++){
quiz_scores[i] = quizscores[i];
}
}
const int* Student :: getQuizScores() const {
return quiz_scores; }
// Method declerations for the class Algorithms
int Algorithms:: getNum(){
return num;
}
void Algorithms :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Algorithms :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Algorithms :: getStudentInfo(int ID_given, Algorithms &algorithms){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
cout << "Quiz results :" << students[i].getQuizScores();
}
}
}
I have also user-interface in the main. It calls the following functions
void addNewStudent(int ID, string name, string surname, Algorithms &algorithms){
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
algorithms.addNewStudent(student);
}
void showStudent(int ID, Algorithms &algorithms){
algorithms.getStudentInfo(ID, algorithms);
}
Both work properly with the code in main as below
int main(){
Algorithms ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
string option_3 = "3) Change a student’s score";
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
cin >> x;
do {
if (x == 1) {
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname, ECE101);
}
else if (x==2){
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
else {
int quiz_grades[4];
cout << "Enter the student ID";
cin >> ID;
cout << endl;
cout << "Enter the quiz grades" << endl;
for (int i = 0 ; i<4 ; i++) {
cin >> quiz_grades[i];
}
changeStudentScores(ID, quiz_grades);
}
The problem here comes from changeStudentScores(ID, quiz_grades, ECE101)
What I want to do is the program should take the array of 4 numbers(corresponds to the quiz grades) and set to the student, whose ID's given by the user. (Of course, firstly the student should be added to the course by the option 1) BUT, I could not pass the array to the changeStudentScores , where it is implemented by
void changeStudentScores(int ID, int* quizscores ){
// Problem occurs here, creating new students object is not so logical, but I tried.
Student student;
student.setID(ID);
student.setQuizScores();
// I just try to pass the quiz grades writing by the user to the set method, which set quiz grades to the students.
After choose the option 2 which shows the information about the students given the ID, I see the correct name and surname, but I could not update the quiz grades.
RESTRICTIONS:
There should be no other method and data members other than the written methods and data members.
But we can implement additional methods and functions (but not class data
members)
getStudentInfo(int ID_given, Algorithms &algorithms) function must return student information given ID, but my implementation I could use with void
QUESTIONS:
How can I implement correctly the changeStudentScores function so that the user can update the existing student quiz score?
My getStudentInfo(int ID_given, Algorithms &algorithms) function returns nothing that I can use for getting information. So need to be returned the student information, so the return type must not be void.
What is the general idea behind updating the members of the objects, which are used in another class with some data structure(here is array.)

Exception has occured, unknown signal error when using class object again inside each function

I'm trying to write a C++ code for a course I'm enrolled in, where I keep the information of the students enrolled in the course.
I should be able to add a student to the classrrom in the user interface written in main , by calling the function void addNewStudent(int ID, string name, string surname), where I create my object instances, Student, and Course inside the function.
I should also be able to search by given ID by calling the function void showStudent(int ID) in the main, where the function uses the getStudent(ID) method of the object of the classCourse
I did not write all the methods, but when I try to debug this code, I got the error " Exception has occured, unknown signal error."
My questions are:
What is the reason of this error? How can I fix it?
Suppose that the user interface in the main is necessary to use as well as the functions it calls. Do I have to create a class object again inside each function as I wrote?
Can a more effective implementation be made in accordance with the object oriented principles I have defined above?
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
public:
Student()
{
ID = 0;
string name = "" ;
string surname = "";
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
};
class Course {
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Course()
{
students[num] = {};
weightQ = 0.3;
weightHW = 0.3;
weightF = 0.4;
}
int getNum(); // Returns how many students are in the course
void addNewStudent(Student new_student);
void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
void getStudent(int ID_given);
};
// Method declerations for the class Student
void Student :: setID(int ID_set){
ID = ID_set;
}
int Student :: getID(){
return ID;
}
void Student :: setName(string name_set){
name = name_set;
}
string Student :: getName(){
return name;
}
void Student :: setSurName(string surname_set){
surname = surname_set;
}
string Student :: getSurName(){
return surname;
}
// Method declerations for the class Course
int Course :: getNum(){
return num;
}
void Course :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Course :: getStudent(int ID_given){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
void addNewStudent(int ID, string name, string surname){
Student student;
Course ECE101;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
ECE101.addNewStudent(student);
}
void showStudent(int ID){
Course ECE101;
ECE101.getStudent(ID);
}
int main(){
Course ECE101;
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
cout << "Enter your option: ";
int x;
int ID;
string name, surname;
cin >> x;
if (x == 1)
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname);
return 0;
}
 To make the menu more interactive you could add a do while statement that would accept 3 options:
register
show data
exit
int main(){
Course ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student\n";
string option_2 = "2) Search a student by ID\n";
cout << "Welcome to the ECE101 Classroom Interface\n";
cout << "Choose your option\n";
cout << option_1 << option_2;
cin >> x;
do {
if (x == 1) {
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname, ECE101);
}
else {
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
cout << "Choose your option\n";
cin >> x;
} while(x != 3);
return 0;
}
addnewStudent() and showStudent() methods now accepts an instance of Course as an argument to be able to add students.
void addNewStudent(int ID, string name, string surname, Course &course) {
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
course.addNewStudent(student);
}
void showStudent(int ID, Course &course) {
course.getStudent(ID, course);
}
the function is modified from the same class as well.
void Course::getStudent(int ID_given, Course &course) {
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
Demo
Your addNewStudent function creates a new course everytime it is called. You could pass a reference to the course as a parameter into the function and call Course.addNewStudent(student). You'll want to make sure you specify it's a reference though when you define your function or you'll just create a copy of the course.

How to accept user input in a class when using private variables?

I have created a class called User:
has 4 private variables
a get_input() and print_output() function
a main function where I am accepting user input, where I have to initialize a new set of variables to pass the user input to the get_input() function
My question is: Do I have to define a new set of variables? Or is there any way to name, DOB, etc. directly to the get_input() function?
Here's my code:
#include <iostream>
#include <vector>
#include <string>
class User
{
private:
std::string name;
std::string DOB;
std::string telephone_no;
std::string add;
public:
int main();
User()
{
name = "";
DOB = "";
telephone_no = "";
add = "";
}
void get_input(std::string &name , std::string &DOB , std::string &telephone_no , std::string &add)
{
this->name = name;
this->DOB = DOB;
this->telephone_no;
this->add = add;
}
void print_output()
{
std::cout << name;
std::cout << DOB;
std::cout << telephone_no;
std::cout << add;
}
};
int main()
{
std::string name;
std::string DOB;
std::string telephone_no;
std::string add;
std::vector<std::string> ;
User obj ;
std::cout << "Enter the name of the user : ";
std::cin >> name;
std::cout << "Enter the date of birth of the user : ";
std::cin >> DOB;
std::cout << "Enter the telephone number of the user : ";
std::cin >> telephone_no;
std::cout << "Enter the address of the user : ";
std::cin >> add;
obj.get_input(name , DOB , telephone_no , add);
obj.print_output();
}
Followed the advice of Sam Varshachik
and changed my program accordingly:
You should input the variables first, then pass them to the class's constructor, when creating an instance of the class, which will use them to construct its private members
#include <iostream>
#include <vector>
#include <string>
class User
{
private :
std::string name;
std::string DOB;
std::string telephone_no;
std::string add;
public :
User(std::string &name , std::string &DOB , std::string &telephone_no , std::string &add)
{
this->name = name;
this->DOB = DOB;
this->telephone_no;
this->add = add;
}
void print_output()
{
std::cout << name;
std::cout << DOB;
std::cout << telephone_no;
std::cout << add;
}
};
int main()
{
//initializing local variables
std::string name;
std::string DOB;
std::string telephone_no;
std::string add;
std::vector<std::string> v;
//prompting the user to get essential details
std::cout << "Enter the name of the user : ";
std::cin >> name;
std::cout << "Enter the date of birth of the user : ";
std::cin >> DOB;
std::cout << "Enter the telephone number of the user : ";
std::cin >> telephone_no;
std::cout << "Enter the address of the user : ";
std::cin >> add;
//passing the entered_input into the object variables
User obj(name , DOB , telephone_no , add);
obj.print_output();
}

c++ can not input tada from keyboard

I am new in programming and i wanted to know how can I input data from keyboard from class. Anyone?
#include <iostream>
#include <string>
using namespace std;
class Human{
private:
string *name;
int *age;
public:
Human(string iname, int iage){
name = new string;
age = new int;
*name = iname;
*age = iage;
}
void display(){
cout << "Hi I am " << *name << " and I am " << *age << " years old" << endl;
}
~Human(){
delete name;
delete age;
cout << "Destructor!";
}
void input(string, int)
{
string name;
int age;
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
}
};
int main()
{
Human *d1 = new Human(Human::input(?????????????????));
d1->display();
delete d1;
return 0;
}
EDIT:
I understand what I can do this:
int main()
{
Human *d1 = new Human("David",24);
d1->display();
return 0;
}
And this:
int main()
{
string name;
int age;
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
Human *d1 = new Human(name,age);
d1->display();
return 0;
}
But I want to know how can I put the data from keyboard with an input function.
Zygis you need to read a tutorial for the basics in C++. Pointers, the *, is a powerful thing programmers can use, but only when they need to. In this case, you do not need to. When do you need them? I think you should leave that for later and focus on my example below. When you understand that, you can read about Pointers in the internet.
#include <iostream>
#include <string>
using namespace std;
class Human {
private:
// data members of class
string name;
int age;
public:
// constructor without arguments
// useful for initializing the data members
// by an input function
Human() {
name = ""; // empty string
age = -1; // "empty" age
}
// constructor with arguments
// useful when you know the values
// of your arguments
Human(string arg_name, int arg_age) {
name = arg_name;
age = arg_age;
}
void display() {
cout << "Hi I am " << name << " and I am " << age << " years old"
<< endl;
}
// the data members will go out of scope automatically
// since we haven't used new anywhere
~Human() {
cout << "Destructor, but the default one would be OK too!\n";
}
// prompt the user to giving values for name and age
void input() {
cout << "Please input name: ";
cin >> name;
cout << "Please input age: ";
cin >> age;
}
};
int main() {
// Let the user initialize the human
Human human_obj_i;
human_obj_i.input();
human_obj_i.display();
cout << "Now I am going to auto-initialize a human\n";
// Let the program itseld initialize the human
Human human_obj("Samaras", 23);
human_obj.display();
return 0;
}
Example run:
Please input name: Foo
Please input age: 4
Hi I am Foo and I am 4 years old
Now I am going to auto-initialize a human
Hi I am Samaras and I am 23 years old
Destructor, but the default one would be OK too!
Destructor, but the default one would be OK too!
Hope that helps! ;)

Calling out the public member variables

This is my class function
class Employee
{
private:
string ename;
double esalary;
public:
Employee(string nm = "", double sal = 0.0)
{
ename = nm;
esalary = sal;
}
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
};
#endif
and now my incomplete body...
#include "employee.h"
using namespace std;
Employee read_employee()
{
string name;
cout << "Please enter the name: ";
getline(cin, name);
double salary;
cout << "Please enter the salary: ";
cin >> salary;
Employee r(name, salary);
return r;
}
int main()
{
Employee emp(string name,double salary);
read_employee();
}
I am wondering how do i call the "getName or getSalary" functions from the class. I am used to the class objects without parameters.
Try this:
Employee emp = read_employee();
Instead of:
Employee emp(string name,double salary);
read_employee();
And then you can say:
emp.getName() and emp.getSalary()
Your problem is that your read_employee() function is a global function. That is, it's not necessarily attached to any specific Employee instance. If you added it to your Employee class, things might work a little better:
class Employee
{
private:
string ename;
double esalary;
public:
Employee(string nm = "", double sal = 0.0)
{
ename = nm;
esalary = sal;
}
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
void read_employee()
{
cout << "Please enter the name: ";
getline(cin, ename);
cout << "Please enter the salary: ";
cin >> esalary;
}
};
And then in main:
#include "employee.h"
using namespace std;
int main()
{
Employee emp;
emp.read_employee();
cout << emp.getName() << endl;
cout << emp.getSalary() << endl;
}
Disclaimer: I'm not doing any error checking at all, so we just have to assume the user will play nice with his/her inputs.
As Chris mentions, you are not assigning the return values of your functions to anything. You want something like:
Employee emp = read_employee("Johnny", 45000);
Which will create an employee object in your function, then return it. That returned object is then assigned to emp.