I have three C++ class, InventoryItem, SalesPerson and Transaction. I am using composition to use InventoryItem and SalesPerson class in Transaction. I want to use user input instead of just passing values in constructor, but I am not being able to do it. Any help will be highly appreciated. Following is my C++ file.
#include<iostream>
#include<string>
using namespace std;
class InventoryItem {
private:
int stockNum; double price;
public:
InventoryItem(int, double);
void display(); };
InventoryItem::InventoryItem(int stkNum, double pr) {
stockNum = stkNum;
price = pr;
}
void InventoryItem::display() {
cout << "Item #" << stockNum << " costs $" << price << endl;
}
//SalesPerson class
class Salesperson {
private:
int idNum;
string name; public:
Salesperson(int, string); void display();
};
Salesperson::Salesperson(int id, string lastName) {
idNum = id;
name = lastName;
}
void Salesperson::display() {
cout << "Salesperson #" << idNum << " " << name << endl;
}
//Transaction(main class)
class Transaction {
private:
int transNum;
InventoryItem itemSold;
Salesperson seller;
public:
Transaction(int, int, double, int, string);
void display();
};
Transaction::Transaction(int num, int item, double pr,
int salesId, string name) : itemSold(item, pr),
seller(salesId, name) {
transNum = num;
}
void Transaction::display() {
cout << "Data for transaction #" << transNum << endl; itemSold.display();
seller.display();
}
int main() {
Transaction aSale(247, 782, 44.77, 512, "Richardson"); aSale.display();
return 0;
}
I am posting this as an answer instead of comment, because i dont have backticks on my phone. So you want to do something like
int main()
{
int x, y, z;
double f;
string name;
cin >> x >> y >> f >> z >> name;
Transaction(x, y, f, z, name);
return 0;
}
Of course you can divide the cin line into one cin per variable and cout instructions on what a user must input next. This would look like this
int main()
{
int x, y, z;
double f;
string name;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Enter f: ";
cin >> f;
cout << "Enter z: ";
cin >> z;
cout << "Enter name: ";
cin >> name;
Transaction(x, y, f, z, name);
return 0;
}
Substitute strings in cout lines with strings you want to prompt the user with.
Related
This question already has answers here:
Default constructor error no matching function for call to
(2 answers)
C++ array of a self-defined class, no matching function call
(3 answers)
no matching function to call for "constructor"
(1 answer)
Closed last year.
I'm a beginner in c++. Though the code is still incomplete, I would like to know why I'm not able to create an array to store my objects from the class. I have to store 5 bank accounts in an array and I was trying to do so by soring the objects but it keeps showing error.
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class Bank
{
string depositor;
int accno;
char type;
float balance;
public:
Bank(string depositor, int accno, char type, float balance); //to assign initial values
void deposit(); //to deposit amount
float withdraw(); //to withdraw amount
void show(); //to show name and balance
Bank(string depositor, int accno); //constructor function for name and account no.
Bank(float balance, int accno); //constructor function for balance and account no.
Bank(char type, int accno); //constructor function for type and account no.
Bank(const Bank&); //copy constructor
//getter and setter functions for all data members
void setname(string depositor);
void setacc(int accno);
void settype(char type);
void setbal(float balance);
string getname();
int getacc();
char gettype();
float getbal();
};
Bank::Bank(string depos, int acno, char typ, float bal)
{
depositor=depos;
accno = acno;
type = typ;
balance = bal ? bal : 0;
}
void Bank::deposit()
{
float damt1;
cout << "Enter deposit amount: ";
cin >> damt1;
if (damt1 < 0.0) {
cout << "Can't deposit negative amount." << endl;
damt1 = 0.0;
}
balance += damt1;
}
float Bank::withdraw()
{
int amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount < 0.0) {
cout << "Negative amount can't be withdrawn" << endl;
amount = 0;
}
if (amount > balance - 1000.0) {
cout << "Not enough balance.";
}
balance -= amount;
return amount;
}
Bank::Bank(string name, int no)
{
depositor = name;
accno = no;
}
Bank::Bank(float bal, int no)
{
balance = bal;
accno = no;
}
Bank::Bank(char ty, int no)
{
type = ty;
accno = no;
}
Bank::Bank(const Bank& p)
{
balance = p.balance;
accno = p.accno;
}
void Bank::setname(string name)
{
depositor = name;
}
void Bank::setacc(int n)
{
accno = n;
}
void Bank::settype(char ty)
{
type = ty;
}
void Bank::setbal(float bal)
{
balance = bal?bal:0;
}
string Bank::getname()
{
return depositor;
}
int Bank::getacc()
{
return accno;
}
char Bank::gettype()
{
return type;
}
float Bank::getbal()
{
return balance;
}
void Bank::show()
{
cout << "Name: " << depositor<<endl;
cout << "Account number: " << accno<<endl;
cout << "Type: " << type<<endl;
cout << "Balance: " << balance<<endl;
}
int main()
{
Bank acct[5];//This is the line with error.I am unable to complete the code bcoz of this
int acno,i;
char ty;
string name;
float bal;
for (i=0;i<5;i++){
cout << "Enter details: \n";
cout << "name: ";
cin >> name;
cout << "\nEnter accno: ";
cin >> acno;
cout << "\nEnter type: ";
cin >> ty;
cout << "\nEnter balance: ";
cin >> bal;
Bank b1(name, acno, ty, bal);
}
return 0;
}
Can someone help me with what corrections I should make?
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.
Throwing this out there first I'm a still learning how to program in school. I'm having an issue reading in to a dynamically created array with a pointer to one of my classes. The function readClassArray() isn't getting the variable back from student.getCreditNumber. The program complies fine in Visual Studio but when I get the the readClassArray it just skips over the function because s.getCreditNumber returns 0.
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Courses{
private:
int courseNumber;
double hours;
string courseName;
char grade;
public:
void setCourseNumber(int n){courseNumber = n; }
void setCreditHours(double c) { hours = c; }
void setCourseName(string n) { courseName = n; }
void setGrade(char g) { grade = g; }
int getCourseNumber() { return courseNumber; }
double getCreditHours() { return hours; }
string getCourseName() { return courseName; }
char getGrade() { return grade; }
};
class Student : public Courses{
private:
string firstName;
string lastName;
string studentNumber;
int creditNumber;
double gpa;
public:
Courses * courses;
Student() {
firstName = " ";
lastName = " ";
studentNumber = " ";
creditNumber = 0;
gpa = 0.0;
courses = NULL;
}
~Student() {
delete[] courses;
};
void setFirstName(string n) { firstName = n; }
void setLastName(string l) { lastName = l; }
void setStudentNumber(string a) { studentNumber = a; }
void setCreditNumber(int num) { creditNumber = num; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
string getStudentNumber() { return studentNumber; }
int getCreditNumber() { return creditNumber; }
};
#endif
Student.cpp
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
void readStudent();
void readCourseArray();
void computeGPA();
void printSummary();
void readStudent() {
Student a;
string number;
string firstName;
string lastName;
int courses;
cout << "Enter student number: ";
cin >> number;
a.setStudentNumber(number);
cout << "Enter student first name: ";
cin >> firstName;
a.setFirstName(firstName);
cout << "Enter student last name: ";
cin >> lastName;
a.setLastName(lastName);
cout << "Enter student number of courses: ";
cin >> courses;
a.setCreditNumber(courses);
cout << "\n"; }
void readCourseArray(){
Student s;
s.courses = new Courses[s.getCreditNumber()];
int num;
double cHours;
string cName;
char grade;
cout << "test" << endl;
for (int i = 0; i < s.getCreditNumber(); i++){
cout << "Enter class " << i + 1 << " number: ";
cin >> num;
s.courses[i].setCourseNumber(num);
cout << "Enter class " << i + 1 << " name: ";
cin >> cName;
s.courses[i].setCourseName(cName);
cout << "Enter class " << i + 1 << " hours: ";
cin >> cHours;
s.courses[i].setCreditHours(cHours);
cout << "Enter class " << i + 1 << " grade: ";
cin >> grade;
s.courses[i].setGrade(grade);
cout << "\n";
}
}
At the start of readCourseArray you've created s. When that happens the value of the creditNumber member is 0 as set by the default constructor. You need to do something to set it to a non-zero value. If you're expecting the value set in readStudent to carry over you need to plumb the two functions together. Either pass in a Student object as a reference to each function, or have readStudent return a Student object and pass that to readCourseArray.
I'm not really sure if the title is correct but i have the following piece of code:
#include <iostream>
#include <string>
using namespace std;
class department
{
private:
string name;
int budget;
public:
int NoT; //Number of teachers
int NoS; //Number of students
void setName(string nam);
void setBudget(int budg);
int getBudget ();
string getName();
};
class school
{
private:
int YoE;
string name;
public:
department *dpt;
int NoDpts;
school();
};
void department::setName(string nam)
{
name=nam;
}
void department::setBudget(int budg)
{
budget=budg;
}
int department::getBudget()
{
return budget;
}
string department::getName()
{
return name;
}
school::school()
{
YoE=2000; // Year of estabilishment
name="Somename";
NoDpts=37;
}
int main()
{
string name;
int budget;
int budg;
school *sch;
sch=new school;
if (!sch)
throw("Out of memory");
sch->dpt=new department[37];
if (!sch->dpt)
throw("Out of memory");
for (int i=0;i<sch->NoDpts;i++)
{
cout << "Insert name for department#" << i+1 << ": ";
cin >> name;
sch->dpt->setName(name);
cout << "Insert budget for department " << name << ": ";
cin >> budget;
sch->dpt->setBudget(budget);
cin.ignore();
}
cout << "Insert a budget for school: ";
cin >> budget;
for (int i=0;i<sch->NoDpts;i++)
{
if (budg=sch->dpt->getBudget()>=budget)
{
cout << "Name of department is: " << sch->dpt->getName() << endl;
cout << "Budget of department amounts to: " << sch->dpt->getBudget() << endl;
cout << endl;
}
}
return 0;
}
As you can see, i allocated 37 departments, however i don't know how to access them.I mean that in the following line:
sch->dpt->setName(name);
I merely access the name of the first department (indirectly by using an accessor function),thus overwriting it and getting different results than expected.This happens to other members too such as
sch->dpt->setBudget(budget);
So i am simply asking how i can access the members of the rest of the departments.
I wish to output different string for reading variables. For example, below, I wish to print Enter english marks before reading english marks using eng.setmarks(). Please suggest a way to implement this.
Here is my code: (look at for loop below)
#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
class student {
char name[20];
int age;
class marks {
int marks;
public:
void setmarks( int x) {
marks = x;
}
int getmarks() {
return marks;
}
};
public:
marks eng, math, phy, chem, cs; // nested objects are public
void setname( char* n) {
strncpy( name, n, 20);
}
char* getname() {
return name;
}
void setage( int a) {
age = a;
}
float total() {
size_t total = eng.getmarks() + math.getmarks() +
phy.getmarks() + chem.getmarks() + cs.getmarks();
return total/500.0;
}
};
int main() {a
student new_stud;
char temp[20];
cout << "Enter name: ";
cin >> temp;
cin.get( temp, sizeof(temp));
new_stud.setname(temp);
int age;
cout << "Enter age: ";
cin >> age;
new_stud.setage( age);
for( size_t i = 0; i < 5; ++i) {
// I wish to output: "Enter marks in" + subject_name, but harcoding it seems tedious
}
cout << "\nTotal Percentage: " << new_stud.total();
return 0;
}
So if I understand correctly, you would like to print out the name of the variable which you are about to read into. Now this can't be done on the way you want it. The best thing you can do is make an array of subject names, and an array of marks.
string[5] Subjects = {"Maths", "English", "Chemistry", "Physiscs", "Computer Sciences"};
marks[5] Marks;
for(int i=0;i<5;i++) {
cout << "Please enter marks in " << Subjects[i] << ":" << endl;
int a;
cin >> a;
Marks[i].setmarks(a);
}
You could also make the marks class have a field subject name, and give it a function inputfromuser(), like this:
class marks {
int marks;
string subjectName;
public:
void setmarks( int x) {
marks = x;
}
int getmarks() {
return marks;
}
void inputfromuser() {
cout << "Please enter marks in " << subjectName << ":" << endl;
cin >> marks;
}
};
Sorry for me using the std::string type, I am not very comfortable with the raw char[] way to handle texts.