I have a class:
class Account {
private:
string name;
unsigned int age;
string username;
string password;
};
In my main program I need to make a bool usernameExsist(Account* userAccount, Account* acountList[], int numberOfElements) function that returns true if the name is found. But I have problems with the arrays and the implementation.I entered some information in the class: Account first=Account("Max", 18,"maxx34","12345adc");Can someone help me with the implementation?! Thanks.
You can use class vector which relieve you from the burden of arrays and size...
Here is an example that you could modify to meet your needs:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Account {
public:
Account(string, unsigned int, string, string);
//private: // in your example un-comment private and provide public getters instead.
string name;
unsigned int age;
string username;
string password;
};
Account::Account(string Name, unsigned int Age, string UserName, string Password) :
name(Name), age(Age), username(UserName), password(Password){
}
bool usernameExsist(string, vector<Account>);
int main(){
std::vector< Account > vec;
string name;
unsigned int age;
string username;
string password;
while(1){
cout << "name: ";
getline(cin, name);
cin.ignore();
cout << "Age: ";
cin >> age;
if(!age || age < 0)
break;
cin.ignore();
cout << endl;
cout << "username: ";
getline(cin, username);
cin.ignore();
cout << endl;
cout << "password: ";
getline(cin, password);
cout << endl;
cin.ignore();
Account a(name, age, username, password);
vec.push_back(a);
}
cout << usernameExsist("Max", vec) << endl;
cout << endl;
return 0;
}
bool usernameExsist(string name, vector<Account> vec){
const int size = vec.size();
for(int i(0); i != size; ++i)
if(name == vec[i].name){
cout << "Found at: " << i << endl;
return true;
}
return false;
}
Related
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 have recently tried to learn how to create an object of vectors in order to represent objects of students including their names and grades. but when I wrote my program I got some errors regarding using &. I do not know what is the problem with my errors. could you please help me to fix it?
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void printvector(const vector< student>&); // fill vector.fill in student information
void fillvector(vector< student>&); // print the information of all students
class student {
public:
student();
student(string, char);
~student();
string getName() ;
char getGrade() ;
void setName(string);
void setGrade(char);
private:
string newName;
char newGrade;
};
student::student() { newGrade = ' '; }
student::student(string name, char grade) {
newName = name;
newGrade = grade;
}
student::~student(){ }
string student::getName() { return newName; }
char student::getGrade() { return newGrade; }
void student::setName(string name) { newName = name; }
void student::setGrade(char grade) { newGrade = grade; }
int main() {
vector<student> myclass;
printvector(myclass);
fillvector(myclass);
return 0;
}
void fillvector(vector< student>& newmyclass) {
string name;
char grade;
int classsize;
cout << "how many students are in your class?";
cin >> classsize;
for (int i = 0; i < classsize; i++) {
cout << "enter student name";
cin >> name;
cout << "enter student grade";
cin >> grade;
student newstudent(name, grade);
newmyclass.push_back(newstudent);
cout << endl;
}
}
void printvector( vector< student>& newmyclass) {
unsigned int size = newmyclass.size();
for (unsigned int i = 0; i < size; i++) {
cout << "student name:" << newmyclass[i].getName() << endl;
cout << endl;
cout << "student grade" << newmyclass[i].getGrade() << endl;
cout << endl;
}
}
It seems you're printing your vector before filling it.. Is your problem fixed when you swap them around?
int main() {
vector<student> myclass;
printvector(myclass); // <--- These two lines should probably be swapped
fillvector(myclass); // <---
return 0;
}
So my job is to describe the functions and I'm working on the void erase() function that is supposed to remove the element of the vector given the string name. teacher only gave us prototypes to for a phonebook assignment. He also gave us a built main() and we are not allowed to change his main() or prototypes, only describe them.
Listed first is the prototypes, then my work, then the main.
PROTOYPES
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <string>
#include <vector>
using namespace std;
class Person
{
public:
Person();
Person(string new_name, int new_phone);
string get_name() const;
int get_phone() const;
bool operator < (Person p) const;
void print() const;
private:
string name;
int phone;
};
void add_people(vector<Person> &phone_book);
void erase(vector<Person> &phone_book, string name);
void sort(vector<Person> &phone_book);
void shuffle(vector<Person> &phone_book);
void reverse(vector<Person> &phone_book);
void print(vector<Person> &phone_book);
int lookup(const vector<Person> &phone_book, string name);
#endif
MY WORK
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"
Person::Person()
{
name = "NONE";
phone = 0000000;
}
Person::Person(string new_name, int new_phone)
{
name=new_name;
phone=new_phone;
}
string Person::get_name() const
{
return name;
}
int Person::get_phone() const
{
return phone;
}
bool Person::operator < (Person p) const
{
return name < p.name;
}
void Person::print() const
{
cout << endl << name << " " << phone;
}
void add_people(vector<Person> &phone_book)
{
cout << "Please enter the new name: ";
string s;
getline(cin, s);
cout << "Please enter new number: ";
int number;
cin >> number;
phone_book.push_back(Person(s,number));
}
void erase(vector<Person> &phone_book, string name)
{
}
void sort(vector<Person> &phone_book)
{
}
void shuffle(vector<Person> &phone_book)
{
}
void reverse(vector<Person> &phone_book)
{
}
void print(vector<Person> &phone_book)
{
for(int i=0; i < phone_book.size(); i++)
cout << phone_book[i] << " ";
}
int lookup(const vector<Person> &phone_book, string name)
{
int i = 0;
while (i < phone_book.size() && phone_book[i].get_name() != name)
{
i++;
return phone_book[i].get_phone();
}
}
THE MAIN
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"
using namespace std;
int main()
{
vector<Person> phone_book;
string name;
int number;
int answer;
srand((int)(time(0)));
phone_book.push_back(Person("Bruin, Joe", 5556456));
phone_book.push_back(Person("Simpson, Homer", 5557471));
phone_book.push_back(Person("Duffman, Barry", 5533331));
cout <<"\n";
cout << "Your phone book contains the following names and numbers: \n";
for (int i=0; i < phone_book.size(); i++)
{
phone_book[i].print();
cout << "\n";
}
cout <<"\n";
answer=0;
while (answer != 8)
{
cout << "\nChoose from the following options:\n\n";
cout << "1) Add people to the phone book.\n";
cout << "2) Erase a person from the phone book.\n";
cout << "3) Sort the phone book.\n";
cout << "4) Shuffle the phone book.\n";
cout << "5) Reverse the phone book.\n";
cout << "6) Print the phone book.\n";
cout << "7) Look up a person in the phone book.\n";
cout << "8) Quit.\n\n";
cin >> answer;
string clear;
getline(cin, clear);
if (answer == 1)
add_people(phone_book);
else if (answer == 2)
{
cout << "Enter a name: ";
getline(cin, name);
erase(phone_book, name);
}
else if (answer == 3)
sort(phone_book);
else if (answer == 4)
shuffle (phone_book);
else if (answer == 5)
reverse(phone_book);
else if (answer == 6)
{
cout <<"\n";
cout << "Your phone book contains the following names and numbers: \n";
print(phone_book);
}
else if (answer ==7)
{
cout << "Enter a name: ";
getline(cin, name);
int number = lookup(phone_book, name);
if (number > 0)
{
cout << "\n\nThe number for " << name << " is: " << number << "\n\n";
}
else
cout << name << " not found in the phone book.\n";
}
}
return 0;
}
Your lookup function finds the position of the Person with the given name in the vector. You can use this index, along with the vector erase function to remove the Person from the vector.
BTW, in your lookup function, you might also want to consider what happens in the case where there is no Person with the given name.