| | |
Adding double quotes to a string problem.
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 41
Reputation:
Solved Threads: 0
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) :
main.cpp :
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
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) :
c++ Syntax (Toggle Plain Text)
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 :
c++ Syntax (Toggle Plain Text)
#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
-7
#2 Oct 29th, 2009
If you want to do it as a function then pass the string by reference
C++ Syntax (Toggle Plain Text)
void addDoubleQuotes(std::string& str) { str = '\"' + str + '\"'; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2009
Posts: 41
Reputation:
Solved Threads: 0
0
#3 Oct 30th, 2009
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.
Again, thanks very much
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.
c++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Double quotes prob in mysql query (PHP)
- Escape Sequences or Accepting Double Quotes in String Constants (Visual Basic 4 / 5 / 6)
- convert string to double, and result is string again (Java)
- Odd string concatenation behavior? (Perl)
- Export from Excel help needed (Mac Software)
- how to code single/double quotes inside the quotes. (PHP)
- ANSI string problem (C++)
- Looking for _gcvt(double,int,string) (C++)
Other Threads in the C++ Forum
- Previous Thread: SIGSEV in array declaration inside a switch
- Next Thread: Setting my loop for random numbers
Views: 408 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






