Adding double quotes to a string problem.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 41
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster

Adding double quotes to a string problem.

 
0
  #1
Oct 29th, 2009
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) :
  1. using namespace std;
  2. #include <string>
  3.  
  4. class Diary
  5. {
  6. public:
  7.  
  8. Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
  9.  
  10. //Getters (Getters are used to access private or protected datamembers) :
  11. string getName() const { return m_Name; }
  12. string getAddress() const { return m_Address; }
  13. string getPostCode() const { return m_PostCode; }
  14. string getTelNo() const { return m_TelNo; }
  15. string getDetails() const { return m_Details; }
  16.  
  17. //Pure Virtual getter functions for Leads class
  18. virtual string getDate() = 0;
  19. virtual string getTime() = 0;
  20.  
  21. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  22. //Pure virtual function to add the double quotes to the .csv files fields
  23. //virtual string addDoubleQuotes() = 0;
  24. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  25.  
  26. private:
  27.  
  28. protected:
  29.  
  30. string m_Name;
  31. string m_Address;
  32. string m_PostCode;
  33. string m_TelNo;
  34. string m_Details;
  35. };
  36.  
  37. Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
  38. {
  39. m_Name = mainname;
  40. m_Address = mainaddress;
  41. m_PostCode = mainpostcode;
  42. m_TelNo = maintelno;
  43. m_Details = maindetails;
  44.  
  45. }
  46.  
  47. //----------------------------------------------------------------
  48.  
  49. class Leads : public Diary
  50. {
  51. public:
  52. Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
  53.  
  54. //Getters:
  55. string getTime() { return m_Time; }
  56. string getDate() { return m_Date; }
  57.  
  58.  
  59. private:
  60.  
  61. protected:
  62.  
  63. string m_Date;
  64. string m_Time;
  65. };
  66.  
  67. Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
  68. {
  69. m_Date = maindate;
  70. m_Time = maintime;
  71.  
  72. //add the double quotes to the object here so that the programs
  73. //other funtionality is possible, without the double quotes it may not be
  74.  
  75. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  76. //add double quotes to m_Name:
  77. m_Name = addDoubleQuotes(m_Name);
  78. //add double quotes to m_Address:
  79. m_Address = addDoubleQuotes(m_Address);
  80. //add double quotes to m_PostCode:
  81. m_PostCode = addDoubleQuotes(m_PostCode);
  82. //add double quotes to m_TelNo:
  83. m_TelNo = addDoubleQuotes(m_TelNo);
  84. //add double quotes to m_Details:
  85. m_Details = addDoubleQuotes(m_Details);
  86. //add double quotes to m_Date:
  87. m_Date = addDoubleQuotes(m_Date);
  88. //add double quotes to m_Time:
  89. m_Time = addDoubleQuotes(m_Time);
  90. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  91.  
  92. }
  93.  
  94. Leads::addDoubleQuotes()
  95. {
  96. //XXXXXXXXXXXXXXXXXXXXXXXXXX
  97. //add the double quotes here
  98. //XXXXXXXXXXXXXXXXXXXXXXXXXX
  99. return 0;
  100.  
  101. }

main.cpp :
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include "diary.h"
  6.  
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. vector<Diary *>vectorname;
  12.  
  13. do{
  14. int choice = 0;
  15. cout << "Choose one of the options below:" << endl;
  16. cout << "1) Enter a lead" << endl;
  17. cout << "2) Exit the program" << endl;
  18. cin >> choice;
  19. cin.ignore();
  20.  
  21. switch(choice)
  22. {
  23. case 1:
  24. {
  25. cout << "You chose to enter a lead" << endl;
  26. //---------------------
  27. //Collect the leads details
  28. //---------------------
  29.  
  30. string mainname;
  31. cout << "Name? " << endl;
  32. getline (cin, mainname);
  33.  
  34. string mainaddress;
  35. cout << "Address? " << endl;
  36. getline (cin, mainaddress);
  37.  
  38. string mainpostcode;
  39. cout << "Post Code? " << endl;
  40. getline (cin, mainpostcode);
  41.  
  42. string maintelno;
  43. cout << "Telephone Number? " << endl;
  44. getline (cin, maintelno);
  45.  
  46. string maindetails;
  47. cout << "Details? " << endl;
  48. getline (cin, maindetails);
  49.  
  50. string maindate;
  51. cout << "Date ? DDMMYY " << endl;
  52. getline (cin, maindate);
  53.  
  54. string maintime;
  55. cout << "Time? 24hr HHMM " << endl;
  56. getline (cin, maintime);
  57.  
  58. cout << "All questions asked" << endl;
  59.  
  60. try
  61. {
  62. Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
  63. vectorname.push_back( ptr );
  64. }
  65. catch (bad_alloc& ba)
  66. {
  67. cerr << "bad_alloc caught: " << ba.what() << endl;
  68. }
  69.  
  70. cout << "past push Lead to list and before deletion" << endl;
  71. break;
  72. }
  73.  
  74. case 2:
  75.  
  76. {
  77. cout << "You chose to exit the program" << endl;
  78.  
  79. ofstream streamname("database.csv");
  80. if(!streamname)
  81. {
  82. cout << "Cannot open file" << endl;
  83. return 1;
  84. }
  85.  
  86. for(unsigned int a = 0; a<vectorname.size(); a++)
  87. {
  88. streamname << vectorname[a]->getName() << "," << vectorname[a]->getAddress() << "," << vectorname[a]->getPostCode() << "," << vectorname[a]->getTelNo() << "," << vectorname[a]->getDetails() << "," << vectorname[a]->getDate() << "," << vectorname[a]->getTime() << endl;
  89. }
  90.  
  91.  
  92. for(unsigned int a = 0; a<vectorname.size(); a++)
  93. {
  94. delete vectorname[a];
  95. }
  96.  
  97. streamname.close();
  98.  
  99. exit(0);
  100.  
  101. break;
  102. }
  103. }
  104. }
  105. while(true);
  106.  
  107. system ("PAUSE");
  108.  
  109. return 0;
  110. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,660
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 29th, 2009
If you want to do it as a function then pass the string by reference
  1. void addDoubleQuotes(std::string& str)
  2. {
  3. str = '\"' + str + '\"';
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 41
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Light Poster
 
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.

  1. using namespace std;
  2. #include <string>
  3.  
  4. class Diary
  5. {
  6. public:
  7.  
  8. Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails);
  9.  
  10. //Getters (Getters are used to access private or protected datamembers) :
  11. string getName() const { return m_Name; }
  12. string getAddress() const { return m_Address; }
  13. string getPostCode() const { return m_PostCode; }
  14. string getTelNo() const { return m_TelNo; }
  15. string getDetails() const { return m_Details; }
  16.  
  17. //Pure Virtual getter functions for Leads class
  18. virtual string getDate() = 0;
  19. virtual string getTime() = 0;
  20.  
  21. //Virtul function for adding double quotes to strings for the .csv file:
  22. virtual void addDoubleQuotes(string& m_Name,string& m_Address,string& m_PostCode,string& m_TelNo,string& m_Details)
  23. {
  24. m_Name = '\"' + m_Name + '\"';
  25. m_Address = '\"' + m_Address + '\"';
  26. m_PostCode = '\"' + m_PostCode + '\"';
  27. m_TelNo = '\"' + m_TelNo + '\"';
  28. m_Details = '\"' + m_Details + '\"';
  29. }
  30.  
  31. private:
  32.  
  33. protected:
  34.  
  35. string m_Name;
  36. string m_Address;
  37. string m_PostCode;
  38. string m_TelNo;
  39. string m_Details;
  40. };
  41.  
  42. Diary::Diary(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails)
  43. {
  44. m_Name = mainname;
  45. m_Address = mainaddress;
  46. m_PostCode = mainpostcode;
  47. m_TelNo = maintelno;
  48. m_Details = maindetails;
  49.  
  50. addDoubleQuotes(m_Name, m_Address, m_PostCode, m_TelNo, m_Details);
  51. }
  52.  
  53. //----------------------------------------------------------------
  54.  
  55. class Leads : public Diary
  56. {
  57. public:
  58. Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
  59.  
  60. //Getters:
  61. string getTime() { return m_Time; }
  62. string getDate() { return m_Date; }
  63.  
  64. //Virtual function for adding double quotes to strings for the .csv file:
  65. void addDoubleQuotes(string& m_Date, string& m_Time)
  66. {
  67. m_Date = '\"' + m_Date + '\"';
  68. m_Time = '\"' + m_Time + '\"';
  69. }
  70.  
  71. private:
  72.  
  73. protected:
  74.  
  75. string m_Date;
  76. string m_Time;
  77. };
  78.  
  79. Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails)
  80. {
  81. m_Date = maindate;
  82. m_Time = maintime;
  83.  
  84. addDoubleQuotes(m_Date, m_Time);
  85. }

Again, thanks very much
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 408 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC