View Single Post
Join Date: May 2008
Posts: 32
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Segmentation Fault error

 
0
  #1
Dec 3rd, 2008
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <stdio.h>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. bool StringToInt(const string &s, int &i);
  12.  
  13. int main()
  14. {
  15.  
  16. // opening up and setting up the input file stream so we can read it
  17. // to perform the "account info" and the "history" parts of the program.
  18. // the inserting part will be done later.
  19. ifstream logFileIn;
  20. logFileIn.open("logfile.txt");
  21.  
  22. // store each line of the logfile into an array where each element
  23. // of the array is each line of the text file.
  24. vector<string> each_Line;
  25. ifstream ifs( "logfile.txt" );
  26. string temp;
  27. while( getline( ifs, temp ) )
  28. {
  29. each_Line.push_back( temp );
  30. }
  31.  
  32. //closing file so we can open later to write to it
  33. logFileIn.close();
  34.  
  35. int numLines = each_Line.size();
  36.  
  37. // storing the account holders # in another array and then removing
  38. // dupes.
  39. vector<string> account_Numbers(numLines);
  40. int i;
  41. for (i=0;i<numLines-1;i++)
  42. {
  43. account_Numbers[i] = each_Line[i].substr(0,4);
  44. }
  45.  
  46. // storing the peoples names in another array in the same positions
  47. // as the previous array of their account #'s.
  48. vector<string> account_Names(numLines);
  49.  
  50. int j;
  51. for (j=0;j<numLines-1;j++)
  52. {
  53. account_Names[j] = each_Line[j].erase(0,5);
  54. int colonPos = account_Names[j].find(":",0);
  55. account_Names[j] = account_Names[j].substr(0,colonPos);
  56. }
  57.  
  58. // now doing the same thing with a date array.
  59. vector<string> account_Dates(numLines);
  60.  
  61. for (int k=0;k<numLines-1;k++)
  62. {
  63. account_Dates[k] = each_Line[k].erase(0,5);
  64. int colonPos2 = account_Dates[k].find(":",0);
  65. account_Dates[k] = account_Dates[k].substr(colonPos2+1,8);
  66. }
  67.  
  68. // once again but for the type of transaction.
  69. vector<string> account_Tran(numLines);
  70.  
  71. for (int l=0;l<numLines-1;l++)
  72. {
  73. account_Tran[l] = each_Line[l];
  74. int colonPos3 = account_Tran[l].rfind(":",(account_Tran[l].length()-1));
  75. account_Tran[l] = account_Tran[l].substr(colonPos3-1,1);
  76. }
  77.  
  78. // and finally an array for the amount.
  79. vector<string> amount(numLines);
  80.  
  81. for (int m=0;m<numLines-1;m++)
  82. {
  83. amount[m] = each_Line[m];
  84. int finalColon = amount[m].rfind(":",(amount[m].length()-1));
  85. amount[m] = amount[m].substr(finalColon+1,10);
  86. }
  87.  
  88. //** Here is where the user will select from one of three menus.
  89.  
  90. cout << endl;
  91. cout << "Please enter either 'i' for Account Info, 'h' for History, or 't' to Insert a Transaction: " << endl;
  92. string choice;
  93. cin >> choice;
  94.  
  95. //***** Account Info Menu *****//
  96. if (choice == "i")
  97. {
  98.  
  99. //display list of names
  100. for (int numNames=0;numNames<numLines-1;numNames++)
  101. {
  102. cout << numNames+1 << ". " << account_Names[numNames] << endl;
  103. }
  104.  
  105. //allow user to select a certain person by entering there number, or q
  106. //to exit
  107. cout << endl;
  108. cout << "Select a person by entering the number next to their name, or enter '0' to quit" << endl;
  109. int nameChoice;
  110. cin >> nameChoice;
  111. if (nameChoice == 0)
  112. {
  113. cout << endl;
  114. cout << "Thank you for using this program.";
  115. }
  116. else
  117. {
  118. cout << endl;
  119. cout << "Account #: " << account_Numbers[nameChoice-1] << endl;
  120. cout << "Name: " << account_Names[nameChoice-1] << endl;
  121. int balance;
  122. for (int a=0;a<numLines-1;a++)
  123. {
  124. if (account_Names[a] == account_Names[nameChoice-1])
  125. {
  126. int tempconvert;
  127. if (account_Tran[a] == "D")
  128. {
  129. if (StringToInt(amount[a],tempconvert))
  130. {
  131. }
  132. balance = balance + tempconvert;
  133. }
  134. else if (account_Tran[a] == "W")
  135. {
  136. if (StringToInt(amount[a],tempconvert))
  137. {
  138. }
  139. tempconvert = tempconvert * (-1);
  140. balance = balance + tempconvert;
  141. }
  142. }
  143. }
  144. cout << "Balance: $" << balance << endl << endl;
  145. cout << "Thank you and please use this program again." << endl;
  146. }
  147.  
  148. }
  149.  
  150. //***** History Menu *****//
  151. else if (choice == "h")
  152. {
  153.  
  154. //display list of names
  155. for (int numNames2=0;numNames2<numLines-1;numNames2++)
  156. {
  157. cout << numNames2+1 << ". " << account_Names[numNames2] << endl;
  158. }
  159. //allow user to select a certain person by entering there number, or q to exit
  160. cout << endl;
  161. cout << "Select a person by entering the number next to their name, or enter '0' to quit" << endl;
  162. int nameChoice2;
  163. cin >> nameChoice2;
  164. if (nameChoice2 == 0)
  165. {
  166. cout << endl;
  167. cout << "Thank you for using this program.";
  168. }
  169. else
  170. {
  171.  
  172. for (int b=0;b<numLines-1;b++)
  173. {
  174. if (account_Names[b] == account_Names[nameChoice2-1])
  175. {
  176. cout << account_Dates[b] << " " << account_Tran[b] << " $" << amount[b] << endl;
  177. }
  178. }
  179.  
  180. }
  181.  
  182. }
  183.  
  184. //***** Insert a Transaction Menu *****//
  185. else if (choice == "t")
  186. {
  187.  
  188. //display list of names
  189. for (int numNames3=0;numNames3<numLines-1;numNames3++)
  190. {
  191. cout << numNames3+1 << ". " << account_Names[numNames3] << endl;
  192. }
  193. //user select a person or now a new option to create a new account.
  194. cout << endl;
  195. cout << "Select the # next to the person whos account you want to add a transaction to or enter 'add' if you want to add a new account." << endl;
  196. string nameChoice3;
  197. cin >> nameChoice3;
  198. if (nameChoice3 == "add")
  199. {
  200. cout << endl << "Please enter the name of the account holder that you want to add(no spaces please): ";
  201. string newAccount;
  202. cin >> newAccount;
  203. account_Names[numLines] = newAccount;
  204. cout << endl << "Please enter the 4 digit account # of the account you would like to create: ";
  205. string newAccountNum;
  206. cin >> newAccountNum;
  207. account_Numbers[numLines] = newAccountNum;
  208. cout << endl << "Please enter the type of transaction ('D' or 'W'): ";
  209. string newAccountTran;
  210. cin >> newAccountTran;
  211. account_Tran[numLines] = newAccountTran;
  212. cout << endl << "Please enter the amount (without $ sign) of the transaction: ";
  213. string newTranAmount;
  214. cin >> newTranAmount;
  215. amount[numLines] = newTranAmount;
  216. cout << endl << "What is todays date? (YY/MM/DD): ";
  217. string newDate;
  218. cin >> newDate;
  219. account_Dates[numLines] = newDate;
  220.  
  221. ofstream logFileOut;
  222. logFileOut.open ("logfile.txt");
  223. for (int write=0;write<account_Names.size();write++)
  224. {
  225. logFileOut << account_Numbers[write] << ":" << account_Names[write] << ":" << account_Dates[write] << ":" << account_Tran[write] << ":" << amount[write] << "\n";
  226. logFileOut.close();
  227. }
  228. }
  229.  
  230.  
  231. }
  232.  
  233. // if they did not enter a correct option.
  234. else
  235. {
  236. cout << "You did not enter a correct choice. The program will now exit. Please run it again and enter correctly." << endl;
  237. }
  238.  
  239. }
  240.  
  241. bool StringToInt(const string &s, int &i)
  242. {
  243. istringstream myStream(s);
  244.  
  245. if (myStream>>i)
  246. return true;
  247. else
  248. return false;
  249. }

The segmentation fault error occurs I think in the block of code from line 200-203. It might occur beyond that as well but I haven't been able to find out because it won't get past the previous part.

Any help?
Reply With Quote