Hi there,

I was hoping for some advice on how best to solve my problem.

I'm creating a .csv file to store objects in, but the datamembers which are strings, require the ability to have commas within the strings.

I have found out that I therefore need to enclose the fields in the .csv file within double quotes.

So I am trying to write a function to add double quotes to the beginning and the end of each string.

This is what I have come up with so far:

I have enclosed the relevant parts with comments like this:
//XXXXXXXXXXXXXXXXX

Header file (diary.h) :

using namespace std;
#include <string>

class Diary
{
public:

	Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);

	//Getters (Getters are used to access private or protected datamembers) :
	string getName() const { return m_Name; }
	string getAddress() const { return m_Address; }
	string getPostCode() const { return m_PostCode; }
	string getTelNo() const { return m_TelNo; }
	string getDetails() const { return m_Details; }

	//Pure Virtual getter functions for Leads class
	virtual string getDate() = 0;
	virtual string getTime() = 0;

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	//Pure virtual function to add the double quotes to the .csv files fields
	//virtual string addDoubleQuotes() = 0;
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

private:

protected:

	string m_Name;
	string m_Address;
	string m_PostCode;
	string m_TelNo;
	string m_Details;
};

Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
{
	m_Name = mainname;
	m_Address = mainaddress;
	m_PostCode = mainpostcode;
	m_TelNo = maintelno;
	m_Details = maindetails;

}

//----------------------------------------------------------------

class Leads : public Diary
{
public:
	Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);

	//Getters:
	string getTime() { return m_Time; }
	string getDate() { return m_Date; }


private:

protected:

	string m_Date;
	string m_Time;
};

Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
{
	m_Date = maindate;
	m_Time = maintime;

	//add the double quotes to the object here so that the programs 
	//other funtionality is possible, without the double quotes it may not be

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	//add double quotes to m_Name:
	m_Name = addDoubleQuotes(m_Name);
	//add double quotes to m_Address:
	m_Address = addDoubleQuotes(m_Address);
	//add double quotes to m_PostCode:
	m_PostCode = addDoubleQuotes(m_PostCode);
	//add double quotes to m_TelNo:
	m_TelNo = addDoubleQuotes(m_TelNo);
	//add double quotes to m_Details:
	m_Details = addDoubleQuotes(m_Details);
	//add double quotes to m_Date:
	m_Date = addDoubleQuotes(m_Date);
	//add double quotes to m_Time:
	m_Time = addDoubleQuotes(m_Time);
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

}

Leads::addDoubleQuotes()
{
	//XXXXXXXXXXXXXXXXXXXXXXXXXX
	//add the double quotes here
	//XXXXXXXXXXXXXXXXXXXXXXXXXX
	return 0;

}

main.cpp :

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "diary.h"

using namespace std;

int main ()
{
	vector<Diary *>vectorname;

	do{
		int choice = 0;
		cout << "Choose one of the options below:" << endl;
		cout << "1) Enter a lead" << endl;
		cout << "2) Exit the program" << endl;
		cin >> choice;
		cin.ignore();

		switch(choice)
		{
		case 1:
			{
				cout << "You chose to enter a lead" << endl;
				//---------------------
				//Collect the leads details
				//---------------------

				string mainname;
				cout << "Name? " << endl;
				getline (cin, mainname);

				string mainaddress;
				cout << "Address? " << endl;
				getline (cin, mainaddress);

				string mainpostcode;
				cout << "Post Code? " << endl;
				getline (cin, mainpostcode);

				string maintelno;
				cout << "Telephone Number? " << endl;
				getline (cin, maintelno);

				string maindetails;
				cout << "Details? " << endl;
				getline (cin, maindetails);

				string maindate;
				cout << "Date ? DDMMYY " << endl;
				getline (cin, maindate);

				string maintime;
				cout << "Time? 24hr HHMM " << endl;
				getline (cin, maintime);

				cout << "All questions asked" << endl;

				try
				{
					Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
					vectorname.push_back( ptr );
				}
				catch (bad_alloc& ba)
				{
					cerr << "bad_alloc caught: " << ba.what() << endl;
				}

				cout << "past push Lead to list and before deletion" << endl;
				break;
			}

		case 2:

			{
				cout << "You chose to exit the program" << endl;

				ofstream streamname("database.csv");
				if(!streamname)
				{
					cout << "Cannot open file" << endl;
					return 1;
				}

				for(unsigned int a = 0; a<vectorname.size(); a++)
				{
					streamname << vectorname[a]->getName() << "," << vectorname[a]->getAddress() << "," << vectorname[a]->getPostCode() << "," << vectorname[a]->getTelNo() << "," << vectorname[a]->getDetails() << "," << vectorname[a]->getDate() << "," << vectorname[a]->getTime() << endl;
				}


				for(unsigned int a = 0; a<vectorname.size(); a++)
				{
					delete vectorname[a];
				}

				streamname.close();

				exit(0);

				break;
			}
		}
	}
	while(true);

	system ("PAUSE");

	return 0;
}

Sorry for the large block of code, but I hoped it may help explain what it is I'm trying to do.

I was wondering if I'm going in the right direction with this.

My first problem, which is why I'm here, is that I plan to pass each field (m_Name, m_Address etc) to the function, but am unsure of how to pass this string to the function without having to write a separate function for each string modification (ie: add the double quotes to the field).

Maybe I should be approaching the problem another way.?

As you may gather, I'm a little lost with this right now, so any help is really most appreciated!

Many thanks.

Carrots :)

If you want to do it as a function then pass the string by reference

void addDoubleQuotes(std::string& str)
{
   str = '\"' + str + '\"';
}
Comments
Really appreciate the help

Thanks a lot for the help Ancient Dragon.

Really appreciate the assistance.

Below is the solution I came up with. It's working fine, but would welcome any criticism on the solution in case it's more complex than it need to be.

using namespace std;
#include <string>

class Diary
{
public:

	Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);

	//Getters (Getters are used to access private or protected datamembers) :
	string getName() const { return m_Name; }
	string getAddress() const { return m_Address; }
	string getPostCode() const { return m_PostCode; }
	string getTelNo() const { return m_TelNo; }
	string getDetails() const { return m_Details; }

	//Pure Virtual getter functions for Leads class
	virtual string getDate() = 0;
	virtual string getTime() = 0;

	//Virtul function for adding double quotes to strings for the .csv file:
	virtual void addDoubleQuotes(string& m_Name,string& m_Address,string& m_PostCode,string& m_TelNo,string& m_Details)
	{
		m_Name = '\"' + m_Name + '\"';
		m_Address = '\"' + m_Address + '\"';
		m_PostCode = '\"' + m_PostCode + '\"';
		m_TelNo = '\"' + m_TelNo + '\"';
		m_Details = '\"' + m_Details + '\"';
	}

private:

protected:

	string m_Name;
	string m_Address;
	string m_PostCode;
	string m_TelNo;
	string m_Details;
};

Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
{
	m_Name = mainname;
	m_Address = mainaddress;
	m_PostCode = mainpostcode;
	m_TelNo = maintelno;
	m_Details = maindetails;

	addDoubleQuotes(m_Name, m_Address, m_PostCode, m_TelNo, m_Details);
}

//----------------------------------------------------------------

class Leads : public Diary
{
public:
	Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);

	//Getters:
	string getTime() { return m_Time; }
	string getDate() { return m_Date; }

	//Virtual function for adding double quotes to strings for the .csv file:
	void addDoubleQuotes(string& m_Date, string& m_Time)
	{
		m_Date = '\"' + m_Date + '\"';
		m_Time = '\"' + m_Time + '\"';
	}

private:

protected:

	string m_Date;
	string m_Time;
};

Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
{
	m_Date = maindate;
	m_Time = maintime;

	addDoubleQuotes(m_Date, m_Time);
}

Again, thanks very much :)

This article has been dead for over six months. Start a new discussion instead.