i am writing a character pointer to a file, but my program always crashes at name when i do it and i am unable to figure out why
#include <iostream>
#include <iomanip>
#include <string>
#include "AmaProduct.h"
using namespace std;
namespace sict{
AmaProduct::AmaProduct(char file){
fileTag_ = file;
}
const char* AmaProduct::unit()const{
return unit_;
}
void AmaProduct::unit(const char* value){
for (int i = 0; i != 9; i++){
unit_[i] = value[i];
}
unit_[11] = 0;
}
fstream& AmaProduct::store(fstream& file, bool addNewLine)const{
file.open("file.txt");
if (file.is_open()){
file << fileTag_ << "," << sku() << ",";
file<< name() << ",";//here
file<< price() << "," << taxed() << "," << quantity() << "," << unit_ << "," << qtyNeeded();
if (addNewLine){
file << endl;
}
}
file.close();
return file;
}
header file
#ifndef SICT_AMAPRODUCT_H__
#define SICT_AMAPRODUCT_H__
#include "Streamable.h"
#include "Product.h"
#include "Date.h"
#include "ErrorMessage.h"
#include "general.h"
#include <iostream>
#include <fstream>
namespace sict{
class AmaProduct : public Product{
private:
char fileTag_;
char unit_[11];
protected:
ErrorMessage err_;
public:
AmaProduct(char file='N');
const char* unit()const;
void unit(const char* value);
fstream& store(fstream& file, bool addNewLine = true)const;
fstream& load(std::fstream& file);
ostream& write(ostream& os, bool linear)const;
istream& read(istream& is);
};
}
name()
const char* Product::name()const{
return name_;
}
char* name_;
void Product::name(char* name){
delete[] name_;
name_= new char[strlen(name)+1];
strcpy(name_,name);
}
if anyone is interested in the other files i will upload them too
There are several possibilities, but if cout<<name() causes a segmentation fault, the most probable cases are:
name_ is still nullptr
name_ is an invalid pointer (for example if you copied your structure, which doesn't respect the rule of 3)
To make your code more reliable, you could change all char*, their tedious memory allocations and the c-style string operations with string.
If you don't, make sure to respect the rule of 3 to avoid shallow copy and pointer issues when copying or copy constructing your struct, and make sure that when you use a fixed size char array, you're sure not to overflow the buffer
Note also that doing this, might overflow the buffer:
file.getline(n, ',');
name(n);
because istream::getline(), when it has two arguments, takes as second argument the size (here 44, the ascii value of the comma). file.getline(n, 7, ',') would be the correct form.
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();
}
I'm trying to write a program that prompts a user for a file and its file path and then reads the file. It then takes the text in the file and stores it into a vector after parsing the data and separating it into a few categories (department, item code, quantity, and cost).
I'm having a few issues knowing how to use getline() correctly, and I'm unsure of a few aspects of it. Overall I'm trying to understand how to assign each element to the appropriate object variable, currently I'm lacking in experience with doing this sort of thing as well as using OOP.
The text contained in the file will look like the following.
21 Music 64679-701 487 28.77
22 Outdoors 63739-141 195 83.23
23 Books 0268-1154 976 65.17
After I am able to get that portion working correctly I'll be working on checking for duplicates and ignoring any that are found.
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <libgen.h>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include "Record.h"
using namespace std;
int main() {
vector<Record> records; //vector of type Records to hold each "Line" of input file
string filename; // File name and path stored as string
/**
* Prompts user for the name of input file and stores in string variable filename
*
*/
cout << "please enter the name of your file with file path:" << endl;
cin >> filename;
ifstream ifs { filename.c_str() };
if (!ifs) {
cerr << " Can't open file " << filename << endl;
return 1;
}
string path = filename.substr(0, filename.find_last_of("\\/"));
string file = filename.substr(filename.find_last_of("\\/") + 1,
filename.length());
if (path.compare(file) == 0) {
path == "";
}
//test for file and file path
cout << "Path portion of " << filename << " is " << path << endl; //
cout << "File portion of " << filename << " is " << file << endl; // path + "new_" + file + ".cvs", make new file with new path
/**
* Put each line of input file in to the records vector
*/
string line; //strings for each parameter of the vector object
while (getline(ifs, line)) {
Record newRecord(line);
//Here is where I'm having trouble with using get line to parse and store the information.
//It is incorrect at the moment, and I have the start of how I think I should be going about it.
// check if this record exists in the vector, if not add, else ignore
records.push_back(newRecord);
}
ifs.close(); //closes the stream
return 0;
}
Record.h
#ifndef RECORD_H_
#define RECORD_H_
#include <iostream>
#include <string>
class Record {
public:
//Constructor
Record(std::string s); //pass this string to our Record class
//De-constructor
virtual ~Record();
//overloaded "==" and "<" comparison operators
friend bool operator ==(const Record &a, const Record &b);
//friend bool operator <(const Record &a, const Record &b); //Do not need at this time.
//Overloaded "<<" operator
friend std::ostream& operator <<(std::ostream&, const Record&);
private:
std::string department;
std::string item_code;
int quantity;
double cost;
};
#endif /* RECORD_H_ */
Record.cpp
#include <string>
#include "Record.h"
using namespace std;
Record::Record(string s) {
/**
* Create a string stream from 's' and use getline(stringStream, line, ",") to
* read each element from the string using the "," as the delimiter. Assign
* each element to the appropriate object variable
*/
//getline(s, line.cost, line.department, line.item_code, line.quantity, ",");
}
Record::~Record() {
// TODO Auto-generated destructor stub
}
//overloaded "==" and "<" comparison operators
bool operator ==(const Record &lhs, const Record &rhs){
return (lhs.cost == rhs.cost && lhs.department == rhs.department &&
lhs.item_code == rhs.item_code && lhs.quantity == rhs.quantity);
}
/**bool operator <(const Record &a, const Record &b){ //do not need at this time
}
**/
//Overloaded "<<" operator
std::ostream& operator <<(std::ostream& os, const Record& r){
os << r.department << ',' << r.item_code << ',' << r.quantity << ',' << r.cost;
return os;
}
I am new to c++ and I'm having a bit of trouble implementing my first program. I need to create a Line class which simply contains an array of char (c-string) as well as a length and a max capacity. The linePtr member variable is of type char*. Here is what I have:
Line.h:
#pragma once
#ifndef LINE_H
#define LINE_H
#include <iostream>
using namespace std;
class Line {
private:
char* linePtr{nullptr};
int lineLength;
int lineCapacity;
public:
Line(); //default ctor
Line(char);
~Line();
friend ostream& operator<<(ostream& output, const Line& l);
};
#endif // !LINE_H
Line.cpp:
#include <iostream>
#include <cstring>
#include "Line.h"
using std::cout;
using std::endl;
using std::strcpy;
using std::strlen;
const int LINE_CAPACITY = 5000; //arbitrarily set
Line::Line() {
cout << "Default ctor" << endl;
linePtr = new char[1]{ '\0' };
lineCapacity = LINE_CAPACITY;
lineLength = 0;
}
Line::Line(char cstr) {
cout << "ctor Line(char cstr)" << endl;
linePtr = new char[2];
lineCapacity = LINE_CAPACITY;
lineLength = 1;
linePtr[0] = cstr;
}
ostream& operator<<(ostream& out, const Line& l) {
return out << l.linePtr;
}
Main.cpp:
#include <iostream>
#include "Line.h"
using namespace::std;
int main() {
Line l1;
cout << l1 << endl;
Line l2('x');
cout << l2 << endl;
system("pause");
return 0;
}
When I run with debugging, when the linePtr field it is written I get the message: "Error reading characters of string". I'm sure I'm doing something stupid but I can't figure it out.
You're not null-terminating the character array in the second constructor. Add this line at the end of the method:
linePtr[1] = '\0';
just a beginner student learning basic C++. I'm trying to figure out the best way to:
Turn a char array Name of 20 into a string that can be printed.
I found in other Stack Overflow topics to use "str()" such as "str(Name)", but it always comes up 'identifier not found'.
cout << "Name:" << str(Name) << endl;
Set a char array of 20 characters. For some reason, the following gives me errors when declaring. I've tweaked it so many times, but I cannot get why it won't give.
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
Full code I have so far:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
//Step 1
struct StudentRecord
{
char Name[20];
//Accessor
void printInfo() const;
};
void StudentRecord::printInfo() const
{
cout << "Name:" << str(Name) << endl;
}
int main()
{
//Step 2
StudentRecord TESCStudent;
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
//Step 3
TESCStudent.printInfo();
_getch();
return 0;
}
Given that you are at a very beginner level, just use std::string:
#include <iostream>
#include <conio.h>
#include <string>
struct StudentRecord {
std::string Name;
void printInfo() const {
std::cout << "Name:" << Name << '\n';
}
};
int main() {
StudentRecord TESCStudent;
TESCStudent.Name = "SuperProgrammer";
TESCStudent.printInfo();
_getch();
}
Live demo
The syntax like this:
char Name[20] = {'S','u','p','e','r','\0'};
is used to initialize a variable when you define it. However, in your case,
StudentRecord TESCStudent;
TESCStudent.Name[20] = ...;
You've already defined it on the line before, so you can't "initialize", you have to "assign" it.
This is pretty much why you use std:string instead of char[].
Im trying to create a very simple VCard but im getting a non-const lvalue reference to type cannot bind error in my main.cpp and can't figure this out. the problem line is.....
vc->createVCard("JasonSteindorf.vcf", &p1);
//Person.h
#ifndef JASONSTEINDORF_PERSON_H
#define JASONSTEINDORF_PERSON_H
#include <string>
using std::string;
namespace JasonSteindorf{
class Person{
public:
Person();
Person(string firstName,string lastName,string phoneNumber,string email)
: firstName(firstName), lastName(lastName), phoneNumber(phoneNumber), email(email){}
inline string getFirstName(){ return firstName; }
inline string getLastName(){ return lastName; }
inline string getPhoneNumber(){ return phoneNumber; }
inline string getEmail(){ return email; }
private:
string firstName, lastName, phoneNumber, email;
};
}
#endif
//VCard.h
#ifndef JASONSTEINDORF_VCARD_H
#define JASONSTEINDORF_VCARD_H
#include "Person.h"
#include <string>
using std::string;
namespace JasonSteindorf{
class VCard{
public:
void createVCard(string fileName, Person &p);
string getVCard(Person &p);
};
}
#endif
//VCard.cpp
#include "VCard.h"
#include <fstream>
using std::ofstream;
#include <string>
using std::string;
#include <sstream>
#include <iostream>
using std::ostringstream;
using namespace JasonSteindorf;
//Writes the VCard to a file
string getVCard(Person &p){
ostringstream os;
os << "BEGIN:VCARD\n"
<< "VERSION:3.0\n"
<< "N:" << p.getLastName() << ";" << p.getFirstName() << "\n"
<< "FN:" << p.getFirstName() <<" " << p.getLastName() << "\n"
<< "TEL:TYPE=CELL:" << p.getPhoneNumber() << "\n"
<< "EMAIL:" << p.getEmail() << "\n"
<< "URL:" << "http://sorcerer.ucsd.edu/html/people/jason.html" << "\n"
<< "REV:20110719T195243Z" << "\n"
<< "END:VCARD\n";
return os.str();
}
//Returns a string containing the VCard format
void createVCard(string fileName, Person &p){
string vCard = getVCard(p);
ofstream outputFile("/Users/jsteindorf/Desktop/" + fileName);
outputFile << vCard;
}
//main.cpp
#include "Person.h"
#include "VCard.h"
#include <iostream>
using namespace JasonSteindorf;
int main(int argc, const char * argv[])
{
VCard *vc = new VCard();
Person *p1 = new Person ("Jason", "S", "858-555-5555", "js#ucsd.edu");
vc->createVCard("JS.vcf", &p1);
return 0;
}
You haven't defined the functions createVCard and getCard as member functions of VCard class.
Those are global functions. Use the scope resolution operator :: to define them as member functions of the class like
void Vcard::createVCard(string fileName,Person &p)
{
....
....
}
string Vcard::getVCard(Person &p)
{
....
....
}
And also your createVCard function accepts a reference to Person hence you will have to pass the object to the person not the address of pointer to the object (&p) nor address of the object (p) instead pass the object by de-referencing it like *p, hence the call would look like vc->createVCard("JS.vcf", *p1)