943,519 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 917
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 24th, 2009
0

stuck with strings and there relation to a file.

Expand Post »
my code so far......
C++ Syntax (Toggle Plain Text)
  1. // ProjectDevelopment.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream> // This libary allows standard input and output operations.
  6. #include <string> // strings are used as tempory storage - for the input to a text based file
  7. #include <vector> // at one point in my program i expermented with vectors for data storage
  8. using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors.
  9. #include <fstream> // this allows filestreaming and allows the reading and writing of a file
  10. #include <iomanip>
  11. #include <cstring>
  12. int intEnterANumber;
  13. int intConfirmation;
  14.  
  15.  
  16.  
  17. ifstream in ( "Store" );
  18.  
  19. string temp;
  20. string strConfirmation;
  21. string strFirstName;
  22. string strSurname;
  23. string strMiddleName;
  24. string strNickName;
  25. string strTelephoneNo;
  26. string strMobileNo;
  27. string strHouseNo;
  28. string strRoadStreet;
  29. string strVillageTown;
  30. string strPostcode;
  31.  
  32. int AddFirstName(int intEnterANumber)
  33. {
  34. cout << "Please Enter the FirstName \n";
  35. cin >> strFirstName;
  36.  
  37. ofstream File;
  38.  
  39. File.open("Store.txt",ios::app);
  40.  
  41. File << strFirstName;
  42. File <<",";
  43.  
  44. File.close();
  45.  
  46.  
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52. int AddSurname(int intEnterANumber)
  53. {
  54. cout << "Please Enter the Surname \n";
  55. cin >> strSurname;
  56.  
  57. ofstream File;
  58.  
  59. File.open("Store.txt",ios::app);
  60.  
  61.  
  62. File << strSurname;
  63. File <<",";
  64.  
  65. File.close();
  66. return 0;
  67. }
  68. int AddMiddleName(int intEnterANumber)
  69. {
  70. cout << "Please Enter the Middle Name\n";
  71. cin >> strMiddleName;
  72.  
  73. ofstream File;
  74.  
  75. File.open("Store.txt",ios::app);
  76.  
  77. File << strMiddleName;
  78. File <<",";
  79.  
  80. File.close();
  81. return 0;
  82. }
  83.  
  84.  
  85. int AddTelephoneNo(int intEnterANumber)
  86. {
  87. cout <<"Please Enter a Telephone Number\n";
  88. cin >> strTelephoneNo;
  89. ofstream File;
  90.  
  91. File.open("Store.txt",ios::app);
  92.  
  93. File << strTelephoneNo;
  94. File <<",";
  95.  
  96. File.close();
  97. return 0;
  98. }
  99. int AddAddress(int intEnterANumber) // a function which is called by the switch statement.
  100. {
  101. cout <<"Please Enter the House No\n";
  102. cin >> strHouseNo;
  103. ofstream File;
  104.  
  105. File.open("Store.txt",ios::app);
  106.  
  107. File << strHouseNo;
  108. File <<",";
  109.  
  110. File.close();
  111.  
  112.  
  113. cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation.
  114. cin >> strRoadStreet;
  115.  
  116. File.open("Store.txt",ios::app);
  117.  
  118. File << strRoadStreet;
  119. File <<",";
  120.  
  121. File.close();
  122.  
  123.  
  124. cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street
  125. cin >> strVillageTown;
  126.  
  127. File.open("Store.txt",ios::app);
  128.  
  129. File << strVillageTown;
  130. File <<",";
  131.  
  132. File.close();
  133.  
  134. cout <<"Please Enter the Postcode\n";
  135. cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc.
  136.  
  137. File.open("Store.txt",ios::app);
  138.  
  139. File << strPostcode;
  140. File <<",";
  141. File <<"\n";
  142.  
  143. File.close();
  144.  
  145. return 0;
  146. }
  147.  
  148.  
  149.  
  150. int main(int argc, char *argv[]) // could just be just int main
  151.  
  152. {
  153.  
  154. do {
  155. int intEnterANumber;
  156. cout << "Welcome To Your Phone \n\n";
  157. cout << " (1) Enter a New Person's Details \n";
  158. cout << " (2) Enter a New Person FirstName and telephone No. \n";
  159. cout << " (3) Search Phone Book for all Names \n";
  160. cout << " (4) Search Phone Book by Firstname n\n";
  161. cout << " (5) Exit \n\n";
  162. cout << "Please Select A Number From the Options Above\n ";
  163.  
  164. cin >> intEnterANumber;
  165.  
  166. switch (intEnterANumber)
  167. {
  168. case 1: AddFirstName(intEnterANumber);
  169. AddMiddleName(intEnterANumber);
  170. AddSurname(intEnterANumber);
  171. AddTelephoneNo(intEnterANumber);
  172. AddAddress(intEnterANumber);
  173.  
  174. break;
  175. case 2: AddFirstName(intEnterANumber);
  176. AddTelephoneNo(intEnterANumber);
  177. break;
  178. //case 3:
  179. //if(!in){
  180. // cout << "Cannot open file.";
  181. // exit (1);
  182. //}
  183. //char str[255];
  184. //while(in){
  185. //in.getline(str, 255);
  186. //cout << str << endl;
  187. //}
  188. // in.close();
  189. //} break;
  190.  
  191. case 4:
  192. //bits of code not quite right
  193.  
  194. char* strSearchData;
  195.  
  196. cout << "Please enter a name for searching \n";
  197. cin >> strSearchData,255 ;
  198.  
  199. in.open("Store.txt");
  200.  
  201. char* strDataStore;
  202.  
  203.  
  204. while ( in.getline( strDataStore, 50));
  205. // tried to write token to seperate words in text file (creating split).
  206. //using commors.
  207.  
  208.  
  209. strcmp (strDataStore,strSearchData );
  210. // sought of works cause error on intilzation.
  211. { // Planning to use if strcmp is true do below if false do other)
  212. cout << strDataStore <<endl;
  213.  
  214. in.close();
  215. } break;
  216.  
  217.  
  218. }
  219. // case 5:
  220.  
  221. // exit(EXIT_SUCCESS)
  222. // or
  223. // exit(1)
  224.  
  225. }
  226.  
  227. while (intEnterANumber !=6 );
  228.  
  229. return 0;
  230. }
if you notice under case 4 - there is a an attempt at some rather bad search code. please help me make my file searchable. i have tried so many different ideas like linear search. but nothing is working. note strcmp and strcpy is a must. please point me in the right direction if you read this code.

many thanks to those who help me - no need to rush.

sincerly stephen.a.barratt - posted name to show it is my coding.
Last edited by Narue; May 24th, 2009 at 10:51 pm. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009
May 24th, 2009
2

Re: stuck with strings and there relation to a file.

Please, post your code with code tag:
[code=cplusplus]
source
[/code]
Read this forum announcement: http://www.daniweb.com/forums/announcement8-3.html
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 24th, 2009
0

Re: stuck with strings and there relation to a file.

Is it so difficult to post using code tags?
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.
Last edited by tux4life; May 24th, 2009 at 2:05 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 24th, 2009
0

Re: stuck with strings and there relation to a file.

Click to Expand / Collapse  Quote originally posted by tux4life ...
Is it so difficult to post using code tags?
It's nearly mentioned everywhere: in the forum announcement which you didn't read, on the background of the text box where you type in your message when making a post, above the forum announcements, in the 'Read this before posting'.


suppose not sorry i have been ill - and did not read everything i was supposed too. so can anyone help me with my problem please.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009
May 24th, 2009
1

Re: stuck with strings and there relation to a file.

suppose not sorry i have been ill - and did not read everything i was supposed too. so can anyone help me with my problem please.
And selecting your code and clicking the code tag button ( # ) was asked too much?
You must be very ill then...
If you're ill then it's better to stay away from your computer and rest a bit
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 24th, 2009
0

Re: stuck with strings and there relation to a file.

Case 4 - where to start!?

You cannot just declare a char* and then use it for input - you must allocate memory to the pointer. Why not just allocate it as a char array, like char strSearchData[128]; (Choose a size you think is sufficient for any fool who attempts to abuse your program.) Then use getline( ) to read in the input, which will protect against buffer overflow (that is, writing beyond the bounds of the array.)

Same for strDataStore. Note that you are using getline( ) reading into it from the file, but again, it has no memory allocated.

When you use strcmp( ) you need to either store the return value or use the function's return as a condition in a branching right then and there. Remember that it returns a value less than 0, 0 or greater than 0. It does not give a correct true/false evaluation - actually it's backwards, in that 0 (false) return indicates a match.

If all you're trying to match is the first name, which one would expect is the first item on the data file line, you could use getline( ) with blank space as the delimiter, so you read in only first name. Then read the rest of the line into another string, using newline as delimiter.
C++ Syntax (Toggle Plain Text)
  1. while ( in.getline( strFirstName, 50, ' ' ) )
  2. {
  3. in.getline( strRestOfLine, 100, '\n' );
  4. //compare the first name, do stuff
  5. }
Oh, watch out for the semicolon following the while( in.getline( )...) - that's going to make your loop just read through the whole file and do nothing to it.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
May 24th, 2009
0

Re: stuck with strings and there relation to a file.

update of my code new question.
C++ Syntax (Toggle Plain Text)
  1. // ProjectDevelopment.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream> // This libary allows standard input and output operations.
  6. #include <string> // strings are used as tempory storage - for the input to a text based file
  7. #include <vector> // at one point in my program i expermented with vectors for data storage
  8. using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors.
  9. #include <fstream> // this allows filestreaming and allows the reading and writing of a file
  10. #include <iomanip>
  11. #include <cstring>
  12. int intEnterANumber;
  13. int intConfirmation;
  14.  
  15.  
  16.  
  17. ifstream in ( "Store" );
  18.  
  19. string temp;
  20. string strConfirmation;
  21. string strFirstName;
  22. string strSurname;
  23. string strMiddleName;
  24. string strNickName;
  25. string strTelephoneNo;
  26. string strMobileNo;
  27. string strHouseNo;
  28. string strRoadStreet;
  29. string strVillageTown;
  30. string strPostcode;
  31.  
  32. int AddFirstName(int intEnterANumber)
  33. {
  34. cout << "Please Enter the FirstName \n";
  35. cin >> strFirstName;
  36.  
  37. ofstream File;
  38.  
  39. File.open("Store.txt",ios::app);
  40.  
  41. File << strFirstName;
  42. File <<" ";
  43.  
  44. File.close();
  45.  
  46.  
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52. int AddSurname(int intEnterANumber)
  53. {
  54. cout << "Please Enter the Surname \n";
  55. cin >> strSurname;
  56.  
  57. ofstream File;
  58.  
  59. File.open("Store.txt",ios::app);
  60.  
  61.  
  62. File << strSurname;
  63. File <<" ";
  64.  
  65. File.close();
  66. return 0;
  67. }
  68. int AddMiddleName(int intEnterANumber)
  69. {
  70. cout << "Please Enter the Middle Name\n";
  71. cin >> strMiddleName;
  72.  
  73. ofstream File;
  74.  
  75. File.open("Store.txt",ios::app);
  76.  
  77. File << strMiddleName;
  78. File <<" ";
  79.  
  80. File.close();
  81. return 0;
  82. }
  83.  
  84.  
  85. int AddTelephoneNo(int intEnterANumber)
  86. {
  87. cout <<"Please Enter a Telephone Number\n";
  88. cin >> strTelephoneNo;
  89. ofstream File;
  90.  
  91. File.open("Store.txt",ios::app);
  92.  
  93. File << strTelephoneNo;
  94. File <<" ";
  95.  
  96. File.close();
  97. return 0;
  98. }
  99. int AddAddress(int intEnterANumber) // a function which is called by the switch statement.
  100. {
  101. cout <<"Please Enter the House No\n";
  102. cin >> strHouseNo;
  103. ofstream File;
  104.  
  105. File.open("Store.txt",ios::app);
  106.  
  107. File << strHouseNo;
  108. File <<" ";
  109.  
  110. File.close();
  111.  
  112.  
  113. cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation.
  114. cin >> strRoadStreet;
  115.  
  116. File.open("Store.txt",ios::app);
  117.  
  118. File << strRoadStreet;
  119. File <<" ";
  120.  
  121. File.close();
  122.  
  123.  
  124. cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street
  125. cin >> strVillageTown;
  126.  
  127. File.open("Store.txt",ios::app);
  128.  
  129. File << strVillageTown;
  130. File <<" ";
  131.  
  132. File.close();
  133.  
  134. cout <<"Please Enter the Postcode\n";
  135. cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc.
  136.  
  137. File.open("Store.txt",ios::app);
  138.  
  139. File << strPostcode;
  140. File <<" ";
  141. File <<"\n";
  142.  
  143. File.close();
  144.  
  145. return 0;
  146. }
  147.  
  148.  
  149.  
  150. int main(int argc, char *argv[]) // could just be just int main
  151.  
  152. {
  153.  
  154. do {
  155. int intEnterANumber;
  156. cout << "Welcome To Your Phone \n\n";
  157. cout << " (1) Enter a New Person's Details \n";
  158. cout << " (2) Enter a New Person FirstName and telephone No. \n";
  159. cout << " (3) Search Phone Book for all Names \n";
  160. cout << " (4) Search Phone Book by Firstname n\n";
  161. cout << " (5) Exit \n\n";
  162. cout << "Please Select A Number From the Options Above\n ";
  163.  
  164. cin >> intEnterANumber;
  165.  
  166. switch (intEnterANumber)
  167. {
  168. case 1: AddFirstName(intEnterANumber);
  169. AddMiddleName(intEnterANumber);
  170. AddSurname(intEnterANumber);
  171. AddTelephoneNo(intEnterANumber);
  172. AddAddress(intEnterANumber);
  173.  
  174. break;
  175. case 2: AddFirstName(intEnterANumber);
  176. AddTelephoneNo(intEnterANumber);
  177. break;
  178. //case 3:
  179. //if(!in){
  180. // cout << "Cannot open file.";
  181. // exit (1);
  182. //}
  183. //char str[255];
  184. //while(in){
  185. //in.getline(str, 255);
  186. //cout << str << endl;
  187. //}
  188. // in.close();
  189. //} break;
  190.  
  191. case 4:
  192. //bits of code not quite right
  193.  
  194. char strSearchData[255];
  195.  
  196. cout << "Please enter a name for searching \n";
  197. cin >> strSearchData,255 ;
  198.  
  199. in.open("Store.txt");
  200.  
  201. char strDataStore[255];
  202. char strRestOfLine[255];
  203.  
  204. while ( in.getline( strDataStore, 255, ' ' ) )
  205. {
  206. in.getline( strRestOfLine, 255, '\n' );
  207.  
  208. while(strcmp (strDataStore,strSearchData) !=0);
  209. {
  210. cout << strDataStore << strRestOfLine <<endl;
  211. }
  212.  
  213. }
  214. in.close();
  215. } break;
  216.  
  217. // case 5:
  218.  
  219. // exit(EXIT_SUCCESS)
  220. // exit(1)
  221.  
  222. }
  223.  
  224. while (intEnterANumber !=6 );
  225.  
  226. return 0;
  227. }
  228.  
  229.  
  230.  
  231. // ProjectDevelopment.cpp : Defines the entry point for the console application.
  232. //
  233.  
  234. #include "stdafx.h"
  235. #include <iostream> // This libary allows standard input and output operations.
  236. #include <string> // strings are used as tempory storage - for the input to a text based file
  237. #include <vector> // at one point in my program i expermented with vectors for data storage
  238. using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors.
  239. #include <fstream> // this allows filestreaming and allows the reading and writing of a file
  240. #include <iomanip>
  241. #include <cstring>
  242. int intEnterANumber;
  243. int intConfirmation;
  244.  
  245.  
  246.  
  247. ifstream in ( "Store" );
  248.  
  249. string temp;
  250. string strConfirmation;
  251. string strFirstName;
  252. string strSurname;
  253. string strMiddleName;
  254. string strNickName;
  255. string strTelephoneNo;
  256. string strMobileNo;
  257. string strHouseNo;
  258. string strRoadStreet;
  259. string strVillageTown;
  260. string strPostcode;
  261.  
  262. int AddFirstName(int intEnterANumber)
  263. {
  264. cout << "Please Enter the FirstName \n";
  265. cin >> strFirstName;
  266.  
  267. ofstream File;
  268.  
  269. File.open("Store.txt",ios::app);
  270.  
  271. File << strFirstName;
  272. File <<" ";
  273.  
  274. File.close();
  275.  
  276.  
  277.  
  278. return 0;
  279.  
  280. }
  281.  
  282. int AddSurname(int intEnterANumber)
  283. {
  284. cout << "Please Enter the Surname \n";
  285. cin >> strSurname;
  286.  
  287. ofstream File;
  288.  
  289. File.open("Store.txt",ios::app);
  290.  
  291.  
  292. File << strSurname;
  293. File <<" ";
  294.  
  295. File.close();
  296. return 0;
  297. }
  298. int AddMiddleName(int intEnterANumber)
  299. {
  300. cout << "Please Enter the Middle Name\n";
  301. cin >> strMiddleName;
  302.  
  303. ofstream File;
  304.  
  305. File.open("Store.txt",ios::app);
  306.  
  307. File << strMiddleName;
  308. File <<" ";
  309.  
  310. File.close();
  311. return 0;
  312. }
  313.  
  314.  
  315. int AddTelephoneNo(int intEnterANumber)
  316. {
  317. cout <<"Please Enter a Telephone Number\n";
  318. cin >> strTelephoneNo;
  319. ofstream File;
  320.  
  321. File.open("Store.txt",ios::app);
  322.  
  323. File << strTelephoneNo;
  324. File <<" ";
  325.  
  326. File.close();
  327. return 0;
  328. }
  329. int AddAddress(int intEnterANumber) // a function which is called by the switch statement.
  330. {
  331. cout <<"Please Enter the House No\n";
  332. cin >> strHouseNo;
  333. ofstream File;
  334.  
  335. File.open("Store.txt",ios::app);
  336.  
  337. File << strHouseNo;
  338. File <<" ";
  339.  
  340. File.close();
  341.  
  342.  
  343. cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation.
  344. cin >> strRoadStreet;
  345.  
  346. File.open("Store.txt",ios::app);
  347.  
  348. File << strRoadStreet;
  349. File <<" ";
  350.  
  351. File.close();
  352.  
  353.  
  354. cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street
  355. cin >> strVillageTown;
  356.  
  357. File.open("Store.txt",ios::app);
  358.  
  359. File << strVillageTown;
  360. File <<" ";
  361.  
  362. File.close();
  363.  
  364. cout <<"Please Enter the Postcode\n";
  365. cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc.
  366.  
  367. File.open("Store.txt",ios::app);
  368.  
  369. File << strPostcode;
  370. File <<" ";
  371. File <<"\n";
  372.  
  373. File.close();
  374.  
  375. return 0;
  376. }
  377.  
  378.  
  379.  
  380. int main(int argc, char *argv[]) // could just be just int main
  381.  
  382. {
  383.  
  384. do {
  385. int intEnterANumber;
  386. cout << "Welcome To Your Phone \n\n";
  387. cout << " (1) Enter a New Person's Details \n";
  388. cout << " (2) Enter a New Person FirstName and telephone No. \n";
  389. cout << " (3) Search Phone Book for all Names \n";
  390. cout << " (4) Search Phone Book by Firstname n\n";
  391. cout << " (5) Exit \n\n";
  392. cout << "Please Select A Number From the Options Above\n ";
  393.  
  394. cin >> intEnterANumber;
  395.  
  396. switch (intEnterANumber)
  397. {
  398. case 1: AddFirstName(intEnterANumber);
  399. AddMiddleName(intEnterANumber);
  400. AddSurname(intEnterANumber);
  401. AddTelephoneNo(intEnterANumber);
  402. AddAddress(intEnterANumber);
  403.  
  404. break;
  405. case 2: AddFirstName(intEnterANumber);
  406. AddTelephoneNo(intEnterANumber);
  407. break;
  408. //case 3:
  409. //if(!in){
  410. // cout << "Cannot open file.";
  411. // exit (1);
  412. //}
  413. //char str[255];
  414. //while(in){
  415. //in.getline(str, 255);
  416. //cout << str << endl;
  417. //}
  418. // in.close();
  419. //} break;
  420.  
  421. case 4:
  422. //bits of code not quite right
  423.  
  424. char strSearchData[255];
  425.  
  426. cout << "Please enter a name for searching \n";
  427. cin >> strSearchData,255 ;
  428.  
  429. in.open("Store.txt");
  430.  
  431. char strDataStore[255];
  432. char strRestOfLine[255];
  433.  
  434. while ( in.getline( strDataStore, 255, ' ' ) )
  435. {
  436. in.getline( strRestOfLine, 255, '\n' );
  437.  
  438. while(strcmp (strDataStore,strSearchData) !=0);
  439. {
  440. cout << strDataStore << strRestOfLine <<endl;
  441. }
  442.  
  443. }
  444. in.close();
  445. } break;
  446.  
  447. // case 5:
  448.  
  449. // exit(EXIT_SUCCESS)
  450. // exit(1)
  451.  
  452. }
  453.  
  454. while (intEnterANumber !=6 );
  455.  
  456. return 0;
  457. }

thank you - last poster it works without errors now but iam am still a very stuck noob.

when you said i need other stuff what time of stuff do i need - to complete my search. don't have to be specfic but some read up topics would be nice thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009
May 24th, 2009
1

Re: stuck with strings and there relation to a file.

Firstly, I am not going to do the functions for you, but I'll show you where you are going wrong, and give you ideas on how to correct them. The following code is extracted from your program's case 4: , with added //<MARKER (x) comments to indicate the lines that are wrong and/or unnecessary...

C++ Syntax (Toggle Plain Text)
  1. case 4:
  2. { // Added this brace for clarity
  3. //bits of code not quite right
  4.  
  5. char* strSearchData; //< MARKER (1)
  6.  
  7. cout << "Please enter a name for searching \n";
  8. cin >> strSearchData,255 ; //< MARKER (2)
  9.  
  10. in.open("Store.txt");
  11.  
  12. char* strDataStore; //< MARKER (3)
  13.  
  14. while ( in.getline( strDataStore, 50)); //< MARKER (4)
  15.  
  16. // tried to write token to seperate words in text file (creating split).
  17. //using commors. //< MARKER (5)
  18.  
  19. strcmp (strDataStore,strSearchData ); //< MARKER (6)
  20. // sought of works cause error on intilzation.
  21. { // Planning to use if strcmp is true do below if false do other)
  22. cout << strDataStore <<endl; //< MARKER (7) <- EDIT: Ignore this marker
  23. in.close();
  24. } //< MARKER (8) <- EDIT: Ignore this marker
  25. }
  26. break;

//< MARKER (1) : The char* is uninitialized (memory has not been allocated for it to act as an array of characters). Its basically unusable for the purpose you have outlined it for. I would recommend for you to go through this tutorial on pointers, and their correct usage. Try the new operator to allocate memory for the char* .

EDIT: (To the OP) You have fixed this...

//< MARKER (2) : What exactly where you thinking when you wrote this? Here is a tutorial on how to use the getline function to receive a character array as input from the user, and consequentially store it in your char* .


EDIT: (To the OP) This(^) still needs to be looked into...

//< MARKER (3) : Follow the same advice I gave for //< MARKER (1) .

EDIT: (To the OP) This(^) has been fixed...

//< MARKER (4) : Check this link for information on basic File Input/Output operations. You should find an example where getline is used to read the file content. There's a (misplaced?) semicolon there as well, that probably shouldn't be, because right now, the while loop doesn't do anything useful.

EDIT: (To the OP) This(^) still needs to be looked into...

//< MARKER (5) : You mentioned that your file is delimited using commas, so your getline statement might resemble the following: getline(in, strDataStore, ',') .

EDIT: (To the OP) This(^) still needs to be looked into...

//< MARKER (6) :
Note: This statement is causing an error because you are passing uninitialized pointers into the function.
This statement should probably be inside your while loop. The function strcmp returns a value indicating whether or not the two strings are equal or not, and as you are checking for equality, it should return 0. Assuming, of course, that you have initialized your char* pointers to act as an array, and you want to check for equality, then the function strcmp will have to be put within an if(-else) construct, like the following:

C++ Syntax (Toggle Plain Text)
  1. if(strcmp(strDataStore, strSearchData) == 0)
  2. {
  3. // Code to do whatever you want to do
  4. }

Note: The above should be within your while(getline(....)) loop. Check this link for more information on strcmp ...


EDIT: (To the OP) This(^) still needs to be looked into...

As for your question on sorting through a list of first-names - The bubble-sort algorithm is easy to learn and apply, so I would recommend you apply this as the sorting technique. Note: You will have to load all the First-Names into a char** (array of strings) to sort it using bubble sort.
Here: A tutorial on the Bubble sort algorithm

Here: A program that shows how to implement the bubble sort algorithm for an array of strings.

Hope this helps!

EDIT: The post directly above this was posted as I was writing this... sorry about that... but the links might still be useful, as the new code also requires some tinkering...
Last edited by amrith92; May 24th, 2009 at 6:50 pm.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. // ProjectDevelopment.cpp : Defines the entry point for the console application.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include <iostream> // This libary allows standard input and output operations.
  8. #include <string> // strings are used as tempory storage - for the input to a text based file
  9. #include <vector> // at one point in my program i expermented with vectors for data storage
  10. using namespace std; // (or maybe std::getline;, std:: string;, std:: cout; , std:: cin; (namespace howecver is a short hand version to reduce code and possible errors.
  11. #include <fstream> // this allows filestreaming and allows the reading and writing of a file
  12. #include <iomanip>
  13. #include <cstring>
  14. int intEnterANumber;
  15. int intConfirmation;
  16.  
  17.  
  18.  
  19. ifstream storage ( "Store.txt" );
  20. ifstream in ( "Store.txt" );
  21.  
  22. string strConfirmation;
  23. string strFirstName;
  24. string strSurname;
  25. string strMiddleName;
  26. string strNickName;
  27. string strTelephoneNo;
  28. string strMobileNo;
  29. string strHouseNo;
  30. string strRoadStreet;
  31. string strVillageTown;
  32. string strPostcode;
  33.  
  34. int StoreNameNumber(int intEnterANumber)
  35. {
  36. cout << "Please Enter the FirstName \n";
  37. cin >> strFirstName;
  38.  
  39. ofstream Store;
  40.  
  41. Store.open("StoreNamePhone.txt",ios::app);
  42.  
  43. Store << strFirstName;
  44.  
  45. Store <<",";
  46.  
  47.  
  48. cout << "Please Enter the MobilePhone \n";
  49. cin >> strMobileNo;
  50.  
  51. Store << strMobileNo;
  52.  
  53. Store <<",";
  54. Store <<" ";
  55.  
  56.  
  57. Store.close();
  58. return 0;
  59. }
  60.  
  61.  
  62.  
  63. int AddFirstName(int intEnterANumber)
  64. {
  65. cout << "Please Enter the FirstName \n";
  66. cin >> strFirstName;
  67.  
  68. ofstream File;
  69.  
  70. File.open("Store.txt",ios::app);
  71.  
  72. File << strFirstName;
  73.  
  74. File <<" ";
  75.  
  76. File.close();
  77.  
  78.  
  79.  
  80. return 0;
  81.  
  82. }
  83.  
  84. int AddSurname(int intEnterANumber)
  85. {
  86. cout << "Please Enter the Surname \n";
  87. cin >> strSurname;
  88.  
  89. ofstream File;
  90.  
  91. File.open("Store.txt",ios::app);
  92.  
  93.  
  94. File << strSurname;
  95. File <<" ";
  96.  
  97. File.close();
  98. return 0;
  99. }
  100. int AddMiddleName(int intEnterANumber)
  101. {
  102. cout << "Please Enter the Middle Name\n";
  103. cin >> strMiddleName;
  104.  
  105. ofstream File;
  106.  
  107. File.open("Store.txt",ios::app);
  108.  
  109. File << strMiddleName;
  110. File <<" ";
  111.  
  112. File.close();
  113. return 0;
  114. }
  115.  
  116.  
  117. int AddTelephoneNo(int intEnterANumber)
  118. {
  119. cout <<"Please Enter a Telephone Number\n";
  120. cin >> strTelephoneNo;
  121. ofstream File;
  122.  
  123. File.open("Store.txt",ios::app);
  124.  
  125. File << strTelephoneNo;
  126. File <<" ";
  127.  
  128. File.close();
  129. return 0;
  130. }
  131. int AddAddress(int intEnterANumber) // a function which is called by the switch statement.
  132. {
  133. cout <<"Please Enter the House No\n";
  134. cin >> strHouseNo;
  135. ofstream File;
  136.  
  137. File.open("Store.txt",ios::app);
  138.  
  139. File << strHouseNo;
  140. File <<" ";
  141.  
  142. File.close();
  143.  
  144.  
  145. cout <<"Please Enter the road or street name\n"; // use street(ST) or (R) for road or street check validation.
  146. cin >> strRoadStreet;
  147.  
  148. File.open("Store.txt",ios::app);
  149.  
  150. File << strRoadStreet;
  151. File <<" ";
  152.  
  153. File.close();
  154.  
  155.  
  156. cout <<"Please Enter the Village or town name\n"; // use Village or (VI)or Town (T) for validation check on town or street
  157. cin >> strVillageTown;
  158.  
  159. File.open("Store.txt",ios::app);
  160.  
  161. File << strVillageTown;
  162. File <<" ";
  163.  
  164. File.close();
  165.  
  166. cout <<"Please Enter the Postcode\n";
  167. cin >> strPostcode; // Check length during validation. With country as well we could have allowed selection of zipcodes etc.
  168.  
  169. File.open("Store.txt",ios::app);
  170.  
  171. File << strPostcode;
  172. File <<"\n";
  173.  
  174. File.close();
  175.  
  176. return 0;
  177. }
  178.  
  179.  
  180.  
  181. int main(int argc, char *argv[]) // could just be just int main
  182.  
  183. {
  184.  
  185. do {
  186. int intEnterANumber;
  187. cout << "Welcome To Your Phone \n\n";
  188. cout << " (1) Enter a New Person's Details \n";
  189. cout << " (2) Enter a New Person FirstName and telephone No. \n";
  190. cout << " (3) Search Phone Book for all Names \n";
  191. cout << " (4) Search Phone Book by Firstname n\n";
  192. cout << " (5) Exit \n\n";
  193. cout << "Please Select A Number From the Options Above\n ";
  194.  
  195. cin >> intEnterANumber;
  196.  
  197. switch (intEnterANumber)
  198. {
  199. case 1: AddFirstName(intEnterANumber);
  200. AddMiddleName(intEnterANumber);
  201. AddSurname(intEnterANumber);
  202. AddTelephoneNo(intEnterANumber);
  203. AddAddress(intEnterANumber);
  204.  
  205. break;
  206. case 2: StoreNameNumber(intEnterANumber);
  207. break;
  208. case 3:
  209.  
  210. if(!storage){
  211. cout << "Cannot open file.";
  212. exit (1);
  213. }
  214. char str[255];
  215. while(storage){
  216. storage.getline(str, 255);
  217. cout << str << endl;
  218. }
  219. storage.close();
  220. //} break;
  221.  
  222.  
  223.  
  224.  
  225. break;
  226.  
  227. case 4:
  228. //bits of code not quite right
  229.  
  230. char strSearchData[255];
  231.  
  232. cout << "Please enter a name for searching \n";
  233. cin >> strSearchData,255 ;
  234.  
  235. storage.open("Store.txt");
  236.  
  237. char strDataStore[255];
  238. char strRestOfLine[255];
  239. char match;
  240.  
  241. while ( storage.getline( strDataStore, 255, ',' ) )
  242. {
  243. storage.getline( strRestOfLine, 255, '\n' );
  244.  
  245. match = strcmp (strDataStore,strSearchData);
  246.  
  247. if (match == 0)
  248. {
  249. cout << strDataStore << strRestOfLine <<endl;
  250. }
  251. else
  252. {
  253. cout <<"Data Not found";
  254. }
  255. }
  256. storage.close();
  257. break;
  258.  
  259. case 5:
  260.  
  261. exit(EXIT_SUCCESS);
  262. exit(1);
  263.  
  264. }
  265. }
  266.  
  267. while (intEnterANumber !=6 );
  268.  
  269. return 0;
  270. }
updated code still trying to work out how or where in my code i could use bubble sort to search for a particular name in a file and how to call a function that uses void. i hope the code tags are in this time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brightsolar is offline Offline
19 posts
since May 2009
May 25th, 2009
0

Re: stuck with strings and there relation to a file.

DaniWeb engine adds line numbers in snippets only if code tag defines the language. Please, next time use code tag with the language specifier:
[code=cplusplus]
source
[/code]
It's more comfortable to refer to line numbers...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked List Copy Constructor
Next Thread in C++ Forum Timeline: any idea on this error?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC