creating a new customer-endless compile errors using Visual Studio compile - c++

guys what am i doing wrong here i need to create a new customer using a int main to call it, code below.....visual studio compiler just has endless errors can anyone offer a fix? thanks so much. hopefully this is enough detail...
#include <iostream>
#include <iomanip>
#include <string>
#define CUSTOMER_H
#indef CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
int main ()
{customer* CreateCustomer(const string& name, const string& id, const string& pin);
return new customer{ name, id, pin };
cout << new customer << endl; }
{
customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
}
return customer;
}

I will but this will be the last time. aurisdante was correct you do need to get a book on C++ programming if you are going to pursue programming. My initial response was a) because I remember when I was starting out. And B) to give you something that would at least compile. So, from here on out it is up to you to go forth and conquer...
#include <iostream>
#include <iomanip>
#include <string>
#ifndef CUSTOMER_H //CUSTOMER_H not defined
#define CUSTOMER_H //define it
#endif // CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
//You have to do this after you prototype the object so the complier knows what the object is
customer* CustomerCreator();
customer* CustomerCreator(string name, string pin, string u_Id);
int main()
{
customer* Customer1 = new customer { "Mary Jones", "235718", "5074" };
cout << Customer1->name << Customer1->pin << endl;
customer* Customer2 = CustomerCreator();
Customer2->name = "Put name here";
Customer2->pin = "0U812";
Customer2->user_id = "AnotherNumberGoesHere";
customer* Customer3 = CustomerCreator("William Shatner", "UCC-1", "HMFIC");
//this creates a break point
_asm int 3;
return 1;
}
//this just creates and returns an object custmer
customer* CustomerCreator()
{
customer* Customer = new customer();
return Customer;
}
customer* CustomerCreator(string name, string pin, string u_Id)
{
customer* Customer = new customer{ name , pin, u_Id};
return Customer;
}

You might want to try something like this:
#include <iostream>
#include <iomanip>
#include <string>
#ifndef CUSTOMER_H //CUSTOMER_H not defined
#define CUSTOMER_H //define it
#endif // CUSTOMER_H
using namespace std;
struct customer
{
string name;
string pin;
string user_id;
};
int main()
{
customer* CreateCustomer = new customer { "Mary Jones", "235718", "5074" };
cout << CreateCustomer->name << CreateCustomer->pin << endl;
//this creates a break point
_asm int 3;
return 1;
}

Related

Storing Objects in an Array of a second Object

I have to create a small console application in C++ which will do the following:
Create class Subject which has next attributes: name of the subject, number of students and array of students who are attending that subject. After that createa class Student which has name and surname of the student as attributes. In Main file count how many duplicate names there are in each Subject.
I have few problems here. First one is that I don't know how to initialize an array in my Subject.h file. Second is how to actually put Student objects into Subject objects and compare the names in the end.
What I'd like output to look like:
Duplicate names in subjectA are: Michael.
Duplicate names in subjectB are: Nicholas, John.
Where subjectA and subjectB should be called C++ and C.
Here is my code so far (I googled for past hour or two about this problem of mine but I just couldn't find a proper answer/example).
NOTE: I'm including all these files for clarification.
Subject.h
#include <string>
#include <iostream>
using namespace std;
/*
* I should also have an array named `arrayOfStudents`
* which should store all students who are attending
* that Subject.
*/
class Subject
{
public:
Subject();
Subject(Subject &subject);
Subject(string nameOfSubject, int numberOfStudents);
~Subject();
const string getNameOfSubject();
const int getNumberOfStudents();
void setNameOfSubject(string nameOfSubject);
void setNumberOfStudents(int numberOfStudents);
void print();
private:
string nameOfSubject;
int numberOfStudents;
};
Subject.cpp
#include <iostream>
#include "Subject.h"
using namespace std;
Subject::Subject()
{
}
Subject::Subject(string nameOfSubject, int numberOfStudents)
{
this->nameOfSubject = nameOfSubject;
this->numberOfStudents = numberOfStudents;
}
Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{
}
Subject::~Subject()
{
cout << "Object is destroyed" << endl;
}
const string Subject::getNameOfSubject()
{
return nameOfSubject;
}
const int Subject::getNumberOfStudents()
{
return numberOfStudents;
}
void Subject::setNameOfSubject(string nameOfSubject)
{
nameOfSubject = this->nameOfSubject;
}
void Subject::setNumberOfStudents(int numberOfStudents)
{
numberOfStudents = this->numberOfStudents;
}
void Subject::print()
{
cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}
Student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(Student &student);
Student(string name, string surname);
~Student();
const string getName();
const string getSurname();
void setName(string name);
void setSurname(string surname);
void print();
private:
string name;
string surname;
};
Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student()
{
}
Student::Student(string name, string surname)
{
this->name = name;
this->surname = surname;
}
Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{
}
Student::~Student()
{
cout << "Object is destroyed" << endl;
}
const string Student::getName()
{
return name;
}
const string Student::getSurname()
{
return surname;
}
void Student::setName(string name)
{
name = this->name;
}
void Student::setSurname(string surname)
{
surname = this->surname;
}
void Student::print()
{
cout << "Student: " << name << " " << surname << endl;
}
Main.cpp
#include <iostream>
#include "Subject.h"
#include "Student.h"
using namespace std;
int main()
{
/*
* First three students should attend first Subject
* while other four the second Subject.
* Also note that only names matter and not surnames.
*/
Student stA("Michael", "Doe");
Student stB("Michael", "Doe");
Student stC("Thomas", "Doe");
Student stD("Nicholas", "Doe");
Student stE("Nicholas", "Doe");
Student stF("John", "Doe");
Student stG("John", "Doe");
Subject subjectA("C++", 3);
Subject subjectB("C", 4);
return 0;
}
1) Get an array of Students into your Subject object: you probably want to use vectors instead of arrays here:
in subject.h add
#include "Student.h"
public:
void addStudent(Student student);
private:
std::vector<Student> students_;
in subject.cpp add
void Subject::addStudent(Student student)
{
this->students_.push_back(student);
}
If you want to extract the student list somehow later, you need to write a function to access it (or make it public).
2) For finding duplicates, look here
Checking for duplicates in a vector
You have to pay attention: the Student objects are in your subject object, not the student names. You have to extract them first and e.g. put them in a vector.
Your task definition sais you should have an array of Students attribute of the Subject class, but i don't see this in your Subject class definition.
And maybe an add Student method and then iterate over the array.

Printing out the wrong value

I'm trying to write a program that takes the grades and prints out the following:
ID:123 NAME:John GRADE:78
but instead I'm getting:
ID:-842150451 NAME: GRADE: 78
Can you guys help me and give me some extra tips to make my code cleaner since I'm fairly new to C++.
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(int num, string text);
int getID();
void setExamGrade(int a, int b);
int getOverallGrade();
void display();
string getName();
string name;
int id;
int exams[3];
int sum;
int average;
};
#endif
Student.cpp
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student(int num, string text)
{
num = id;
text = name;
exams[0, 1, 2] = 0;
}
int Student::getID() {
return id;
}
string Student::getName() {
return name;
}
void Student::setExamGrade(int a, int b) {
exams[a] = b;
}
int Student::getOverallGrade() {
sum = exams[0] + exams[1] + exams[2];
average = sum / 3;
return average;
}
void Student::display() {
cout << "ID: " << getID();
cout << " NAME: " << getName();
cout << " GRADE: " << getOverallGrade() << endl;
}
#endif
gradebook.cpp
#ifndef GRADEBOOK_CPP
#define GRADEBOOK_CPP
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
Student *s = new Student(123, "John");
s->setExamGrade(0, 80);
s->setExamGrade(1, 60);
s->setExamGrade(2, 95);
s->display();
delete s;
return 0;
}
#endif
You never assign to id in the constructor, hence it's uninitialized and you will have undefined behavior when you print it.
Change
num = id;
to
id = num;
Same with the name.
Also, the statement
exams[0, 1, 2] = 0;
doesn't do what you expect it to do, it only initializes exams[2] to sero, and leaves the rest uninitialized. The expression 0, 1, 2 uses the comma operator.
Either assign to all members of the array separately, or use a constructor member initializer list (which I recommend for all the initialization).

char array outputting as weird symbols

So Ive been learning about classes, and in my main function when I run it, it shows character array members incorrectly.
main program:
#include <iostream>
#include "account.h"
#include "account.cpp"
using namespace std;
int main(){
char num [] = "2435457";
char name [] = "BOB JOE";
account bob(10000, num, name );
bob.show_account();
cout << num; // this should output correctly, but shows the '♦'
return 0;
}
output:
ACCOUNT INFO:
Account holder :
Account number :♦
Balance :10000
♦
Whats weird is that using cout<< directly on the char array num shows the '♦'.
When I copy the char num [] = "2435457" into a new program like:
#include <iostream> //including all the same headers does not affect
#include "account.h" //the output
#include "account.cpp"
using namespace std;
int main(){
char num [] = "2435457";
cout << num;
return 0;
}
It works correctly.
EDITED :
Here is the header "account.h"
#ifndef BANK_H_
#define BANK_H_
using namespace std;
class account {
static const int NMAX = 20, AMAX = 10;
private:
double balance;
char account_number [AMAX];
char account_holder [NMAX];
public:
account(double BAL, char acct [], char name []);
void show_account();
void deposit(double money);
void withdrawl(double money);
};
#endif
And "account.cpp"
#include "account.h"
#include <iostream>
#include <cstring>
using namespace std;
account::account(double BAL, char acct[], char name[]){
strcpy(name, account_holder);
strcpy(acct, account_number);
balance = BAL;
}
void account::show_account(){
cout << "ACCOUNT INFO :"<<endl;
cout << "\tAccount holder :";print(account_holder);
cout << "\n\tAccount number :";print(account_number);
cout << "\n\tBalance :"<<balance<<endl;
}
void account::deposit(double money){
balance += money;
}
void account::withdrawl(double money){
balance -= money;
}
Your problem is in the constructor like I thought.
account::account(double BAL, char acct[], char name[]){
strcpy(name, account_holder);
strcpy(acct, account_number);
balance = BAL;
}
strcpy's reference says:
char* strcpy( char* dest, const char* src );
so you got the 2 parameters switched, you're copying from the uninitialized member array into your char pointer. Since the member array is uninitialized at that point reading from it is undefined behavior. You should switch them:
account::account(double BAL, char acct[], char name[]){
strcpy(account_holder, name);
strcpy(account_number, acct);
balance = BAL;
}

no matching function for call to vector .. found C++

I have went over code about 5 times, i am learning c++ and according to me everything should work however that's not happening. Error makes no sense, i mean vector suppose to have "at" function..
I get error:
In function 'void lookup(class vector<Customer,_default_alloc_template<false,0> > &, class string)': findByZipCode.cpp:30: no matching function for call to 'vector<Customer,_default_alloc_template,0> >::at (int)
findByZipCode.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "customer.h"
using namespace std;
Name readRestOfName(istream &in,string f) {
string g;
getline(in,g);
Name abc (g,f);
return abc;
}
Address readAddress(istream &in) {
string street, city, state, zip;
getline(in, street);
getline(in, city);
getline(in, state);
getline(in, zip);
Address abc (street, city, state, zip);
return abc;
}
void lookup(vector<Customer>& v, string zip) {
// COMPLETE
vector <int> matches;
cout<<v.at(2).getName()<<endl; <<<---------------error here!!!
}
int main() {
// open customerdata file
ifstream in("customerdata");
vector<Customer> v;
string f;
while (getline(in,f)) {
Name n = readRestOfName(in,f);
Address a = readAddress(in);
Customer abc(n,a);
v.push_back(abc);
}
in.close();
string zip;
while(!cin.fail()){
cin>>zip;
lookup(v,zip);
}
return 0;
}
customer.cpp and customer.h
.cpp
#include <iostream>
#include "customer.h"
#include <string>
using namespace std;
string Customer::getName() {return n.getFamily() + ", " + n.getGiven();}
string Customer::getAddress() {return a.getStreet() + " " + a.getCity() + " "+ a.getState()+" "+a.getZip();}
string Customer::getZip() {return a.getZip();}
.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
#include <iostream>
#include "name.h"
#include "address.h"
class Customer {
private:
Name n;
Address a;
public:
Customer(Name b,Address c): n(b), a(c) {}
std::string getName();
std::string getAddress();
std::string getZip();
};
#endif

How to run below codes inside the Student.cpp through a class?

// main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "Student.h"
using namespace std;
void fillVector(vector<Student>&);
void printVector(const vector<Student>&);
int main()
{
vector<Student> myClass;
fillVector(myClass);
printVector(myClass);
return 0;
}
void fillVector(vector<Student>& newMyClass)
{
string name;
char grade;
cout << "How many you students are in your class? ";
int classSize;
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;
}
cout<<endl;
}
void printVector(const vector<Student>& newMyClass)
{
int size = newMyClass.size();
for ( int i=0; i<size; i++ )
{
cout<<"Student name: "<<newMyClass[i].getName()<<endl;
cout<<"Student grade: "<<newMyClass[i].getGrade()<<endl;
cout<<endl;
}
}
// Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student();
Student(string, char);
~Student();
string getName() const;
char getGrade() const;
void setName(string);
void setGrade(char);
private:
string newName;
char newGrade;
};
#endif // STUDENT_H_INCLUDED
// Student.cpp
#include "Student.h"
Student::Student()
{
newGrade = ' ';
}
Student::Student(string name, char grade)
{
newName = name;
newGrade = grade;
}
Student::~Student() {}
string Student::getName() const
{
return newName;
}
char Student::getGrade() const
{
return newGrade;
}
void Student::setName(string name)
{
newName = name;
}
void Student::setGrade(char grade)
{
newGrade = grade;
}
My aim is to declare a class called CreateGradeBook inside Student.hand define it in Student.cpp and put all the code of the main.cpp in it. In other words I want main.cpp to be still there but with no codes in it like below;
#include <iostream>
using namespace std;
int main()
{
}
Please be tolerant if my question is inappropriate or off topic as I'm fairly new with StackOverflow. I've read FAQ section but not all of it.
Well here's a start
class CreateGradeBook
{
public:
void fillVector();
void printVector() const;
private:
vector<Student> myClass;
};
Notice how the vector you declared in main has become a private data member myClass. The two functions you declared have become public methods of CreateGradeBook. Notice also that they have lost their parameters instead they'll now operate on the private data member myClass.
I'll leave you to do the rest.