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;
}
Related
I'm trying to create a function below in my CreateReport class called load() that copies all the records (data) from my graduate.dat file into my static vector of Record pointers called primaryCollection. I created a Record class with variables that make up each Record, and in my load() function in createReport.cc I attempted to read each line in the file, create a Record object with each line, add it to my vector, and then print everything in primaryCollection.
The problem is every time I attempt to use primaryCollection, I keep getting the error:
CreateReport.o: In function `CreateReport::CreateReport()':
CreateReport.cc:(.text+0x43): undefined reference to `CreateReport::primaryCollection'
CreateReport.o: In function `CreateReport::load()':
CreateReport.cc:(.text+0x2ac): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x31d): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x32f): undefined reference to `CreateReport::primaryCollection'
I get 4 undefined references for the 4 times I mention primaryCollection in createReport.cc. I'm not sure if I'm initializing primaryCollection correctly and if that is whats causing these undefined references. I don't know if this is relevant to my problem, but CreateReport is also an abstract class and has a few subclasses called ReportOne, ReportTwo, etc.
primaryCollection is supposed to be a static vector of Record pointers and I'm also not allowed to use std::map for this task.
I would appreciate any help with this issue. I looked at this post Undefined reference to static variable c++ but I still don't really understand what to do. I'm not allowed to make global variables and I'm dealing with a collection rather than a single variable.
My graduate.dat file is formatted like below in the format < year province degree >
2000 AB Bachelor's
2005 AB Bachelor's
2005 MB College
Each line basically represents a Record. So the first record here is 2000 AB Bachelor's
EDIT: So I made changes to my code based on the comments by adding the line vector<Record*> CreateReport::primaryCollection; above my constructor, but it gives me the error:
CreateReport.cc:13:34: error: conflicting declaration ‘std::vector<Record*> CreateReport::primaryCollection’
vector<Record*> CreateReport::primaryCollection;
^~~~~~~~~~~~~~~~~
In file included from CreateReport.cc:5:0:
CreateReport.h:23:33: note: previous declaration as ‘std::vector<Record*>* CreateReport::primaryCollection’
static std::vector<Record*>* primaryCollection; //STL vector of record pointers
^~~~~~~~~~~~~~~~~
CreateReport.cc:13:34: error: declaration of ‘std::vector<Record*>* CreateReport::primaryCollection’ outside of class is not definition [-fpermissive]
vector<Record*> CreateReport::primaryCollection;
Any ideas how to fix this?
Record.h
#ifndef RECORD_H
#define RECORD_H
#include <iostream>
#include <string>
class Record{
public:
Record(int = 0, string = "", string = "");
~Record();
private:
int year;
string province;
string degree;
};
#endif
Record.cc
#include <iostream>
#include <string>
using namespace std;
#include "Record.h"
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
Record::~Record(){}
CreateReport.h
#ifndef CREATEREPORT_H
#define CREATEREPORT_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include "Record.h"
class CreateReport{
public:
CreateReport();
static void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
#endif
CreateReport.cc
#include <iostream>
using namespace std;
#include <string>
#include "CreateReport.h"
vector<Record*> CreateReport::primaryCollection;
CreateReport::CreateReport(){
}
void CreateReport::load(){
int year;
string province, degree;
ostream_iterator<Record*> outItr(cout);
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) { //as long as were not at end of file
Record* record = new Record(year, province, degree); //create Record object with this data
primaryCollection->push_back(record); //undefined reference
}
cout<<endl<<"List of Records:"<<endl;
copy(primaryCollection->begin(), primaryCollection->end(), outItr); //2 undefined references
}
Second version using `Record*` for `std::vector primaryCollection`.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
//***************** you need this line ********************
std::vector<Record*> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
Record *a = new Record(year, province, degree);
primaryCollection.push_back( a );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << *primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}
Three major problems:
Using std::vector<*Record> cause many un-necessary difficulties;
For static member vector, a extra definition outside the class is necessary.std::vector<Record> CreateReport::primaryCollection;. This erases the undefined error message.
Using copy to std::cout doesn't work, it provide no method of printing Record. Suggest to write a output overload.
Based on these, I provide a version as follows (mixed all headers together.)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record> primaryCollection;
};
//***************** you need this line ********************
vector<Record> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
primaryCollection.push_back( Record(year, province, degree) );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}
The question:
Why is the following error happening?
definition of implicity-declared 'Clothing::Clothing()
The context:
As an assignment I have to do constructors, destructors and methods in a class Clothing. I'm having a problem when I try to define the constructor in clothing.cpp. I have read that the problem is because I did not declare the constructor in clothing.h, but I think how I have done it, it's declared. I have no clue where the problem lies.
My code:
clothing.h:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing();
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
clothing.cpp:
#include <iostream>
#include "clothing.h"
#include <string>
#include <sstream>
using namespace std;
Clothing::Clothing() :
gender(1),
size(1),
name("outofstock") {
}
Clothing::Clothing(const Clothing& t) :
gender(t.gender),
size(t.size),
name(t.name) {
}
Clothing::Clothing(int gender, int size, string name) {
}
int Clothing::getGender() {
return gender;
}
int Clothing::getSize() {
return size;
}
string Clothing::getName() {
return name;
}
void Clothing::setGender(int gender1) {
gender = gender1;
}
void Clothing::setSize(int size1) {
size = size1;
}
void Clothing::setName(string name1) {
name = name1;
}
void Clothing::print() {
cout << name << " " << gender << " " << size << endl;
}
void Clothing::toString() {
stringstream ss;
ss << name << " " << gender << " " << size;
cout << ss.str();
}
Errors: \src\clothing.cpp:7:21: error: definition of implicitly-declared 'Clothing::Clothing()'
\src\clothing.cpp:14:37: error: definition of implicitly-declared 'Clothing::Clothing(const Clothing&)'
The error is: you declared a destructor but you didn't define it. Add a definition for the destructor or define it as default:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing() = default; // <-- add a default destructor
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
After fixing this your code snippet works: tio.run
If you have more problems with your code, the problems are outside of your provided code snippet.
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).
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;
}
// 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.