fstream isnt getting the data right

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

fstream isnt getting the data right

 
0
  #1
May 8th, 2005
I'm running into to this problem where the fstream is suppose to read data off a file to store in arrays. I cant continue with my project until the program reads it right. It is skipping some of the datas. Can I get a little help? Thanks in advance
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. #include <iomanip>
  7. #include <ctime>
  8. using namespace std;
  9.  
  10.  
  11. void mainMenu();
  12. void readRecords();
  13. void saveRecords();
  14. void displayRecords();
  15. int choice;
  16. int takingPass(string);
  17. int i;
  18. int pass1;
  19. char ch[13];
  20. void wrong();
  21. int ID[100];
  22. string fname[100];
  23. string lname[100]={string()};
  24. char middlei[100]={char()};
  25. string state[100]={string()};
  26. int zip[100]={};
  27. double hrWork[100]={};
  28. double ratePhour[100]={};
  29.  
  30. void header()
  31. {
  32. cout << "Employee Records System (ERS)\n";
  33. cout << "Long Beach City College\n";
  34. cout << "4901 E. Carson Street\n";
  35. cout << "Long Beach, CA 90808\n";
  36. }
  37. void IDcode()
  38. {
  39. string user;
  40. cout << setw(10) << "\t\t ****************************************\n";
  41. cout << setw(10) << "\t\t *Please Log-in before using application*\n";
  42. cout << setw(10) << "\t\t ****************************************\n";
  43. cout << "Please Enter Your User Name. "; // asking for user name
  44. cin >> user;
  45. cout << "Please Enter Your User ID Code. "; // asking for password
  46.  
  47. for (i=0; i<13; i++) //user can input max 13 character
  48. {
  49. ch[i] = getch(); // take in passwore
  50. cout << "*"; //display * when password is input
  51.  
  52. if (ch[i]==13) //when 13 keys are enter
  53. {
  54. takingPass(user); // go to function takingPass
  55. }
  56. }
  57. }
  58.  
  59. int takingPass(string name)
  60. {
  61. time_t currentTime;
  62. time(&currentTime);
  63.  
  64. ofstream out;
  65. out.open("c:\\UserLog.txt", ios::app); // file user login
  66. ch[i] = 0;
  67. pass1 = stricmp(ch,"lbcc"); //compare with name "lbcc" not sure what stricmp does(my cousin told me to use it)
  68. if (pass1 == 0) // case insensitive
  69. {
  70. cout <<"\nID CODE CONFIRM \n";
  71.  
  72. out << name << " " << asctime(localtime(&currentTime))<< endl; // out put user login time and date
  73.  
  74. system("cls");
  75. out.close();
  76. mainMenu();
  77. }
  78.  
  79. else
  80. {
  81. wrong(); // go wrong to display message to start over
  82. }
  83.  
  84. return 0;
  85. }
  86. void wrong() //if password is not correct
  87. {
  88.  
  89. cin.clear(); // clear cin
  90. cout <<"\nUNKNOWN ID CODE\n";
  91. cout<< endl;
  92. system("cls"); // clear screen
  93. IDcode();
  94. }
  95. int main()
  96. {
  97. IDcode(); // get password when program runs
  98. }
  99.  
  100. void mainMenu() // Menu after correct password is enter
  101. {
  102. header();
  103. cout << " Main Menu\n";
  104. cout << "============\n";
  105. cout << "1. Read Employee Records\n";
  106. cout << "2. Save Processed Records - Files\n";
  107. cout << "3. Display Records\n";
  108. cout << "4. Search Records\n";
  109. cout << "5. Sort Records \n";
  110. cout << "6. Exit\n";
  111. cin >> choice;
  112.  
  113.  
  114. if (choice == 1) // go to readRecords
  115. {
  116. readRecords();
  117. }
  118. else if (choice == 2) // go to saveRecords
  119. {
  120. saveRecords();
  121. }
  122. else if (choice == 3) // go to displayRecords
  123. {
  124. displayRecords();
  125. }
  126. /*else if (choice == 4) // go to searchRecords
  127. {
  128. searchRecords();
  129. }
  130. else if (choice == 5) // go to sortRecords
  131. {
  132. sortRecords();
  133. }*/
  134. else if (choice == 6) // exit when 6 is enter
  135. {
  136. system("cls");
  137. cout << "Logged Out"<<endl;
  138. cout << "Press any key to close screen" <<endl;
  139. exit(1);
  140. }
  141. else // when other than 1-6 is enter
  142. {
  143. cout << "Invalid choice";
  144. system("cls");
  145. mainMenu();
  146. }
  147. }
  148.  
  149. void readRecords()
  150. {
  151.  
  152. ifstream inFile;
  153. ofstream outFile;
  154. inFile.open("c:\\FinalData.txt");
  155. outFile.open("c:\\outData.txt");
  156. for (int a=0; a<100; a++)
  157. {
  158. inFile >> ID[a] >> fname[a] >> lname[a] >> middlei[a] >> state[a] >> zip[a] >> hrWork[a] >> ratePhour[a];
  159. cout << ID[a]<< " " << fname[a]<< endl;
  160. }
  161. //cout << "Records have been read" << endl;
  162. //system("cls");
  163. //displayRecords();
  164. mainMenu();
  165.  
  166. }
  167.  
  168. void saveRecords()
  169. {
  170. /*
  171. int ID[100]={};
  172. string fname[100]={string()};
  173. string lname[100]={string()};
  174. char middlei[100]={char()};
  175. string state[100]={string()};
  176. int zip[100]={};
  177. double hrWork[100]={};
  178. double ratePhour[100]={};
  179. */
  180. ifstream inFile;
  181. ofstream outFile;
  182.  
  183. inFile.open("c:\\FinalData.txt");
  184. outFile.open("c:\\outData.txt");
  185.  
  186. for (int a=0; a<30; a++)
  187. {
  188. //inFile >> ID[a] >> fname[a] >> lname[a] >> middlei[a] >> state[a] >> zip[a] >> hrWork[a] >> ratePhour[a];
  189. outFile << ID[a]<< " " << fname[a] << " " << lname[a] << " " << middlei[a] << " " << state[a] << " " << zip[a] << " " << hrWork[a] << " " << ratePhour[a]<<endl;
  190. }
  191. outFile.close();
  192. cout << "Records saved" << endl;
  193. system("cls");
  194. mainMenu();
  195. }
  196.  
  197. void displayRecords()
  198. {
  199. /* int display;
  200.  
  201. if (display == 1)
  202. {
  203. findLowest();
  204. }
  205. else if (display == 2)
  206. {
  207. findHighiest();
  208. }
  209. else if (display == 3)
  210. {
  211. displayTotals();
  212. }
  213. /*else if (display == 4)
  214. {
  215. allRecords();
  216. }
  217. else if (display == 5)
  218. {
  219. DisplayMenu();
  220. }
  221. else if (display == 6) //
  222. {
  223. system("cls");
  224. exit(1);
  225. }
  226. else
  227. {
  228. cout << "Invalid choice";
  229. system("cls");
  230. mainMenu();
  231. } */
  232. }

This is the file that it is reading from
  1. 000001 Moshiur Ahmed I CA 90805 40 12.50
  2. 000002 Tasmia Amaat J CA 90706 35 25.60
  3. 000003 Victor Barboza K CA 90706 42 30.00
  4. 000004 Carlos Detorres I CA 90804 50 20.45
  5. 000005 Tyree Ek O CA 90755 60 15.60
  6. 000006 Amine El-Maziati M CA 90815 20 9.75
  7. 000007 Loubna El-Maziati M CA 90815 25 10.50
  8. 000008 Theresa Eyssallenne L CA 90715 40 20.00
  9. 000009 Craig Griffith I CA 90804 40 45.00
  10. 000010 Rodney Hallett H CA 90808 43 25.25
  11. 000011 Andrew Hegstrom P CA 90808 45 13.50
  12. 000012 Corey James D CA 90815 40 20.00
  13. 000013 Farsio Kottab E 90013 CA 90755 40 20.00
  14. 000014 Peter Lon W CA 90755 40 60.00
  15. 000015 Joseph Lopes A CA 90808 50 100.00
  16. 000016 Arley Lozada O CA 90280 50 30.40
  17. 000017 Ana Moreno W CA 90805 55 14.50
  18. 000018 Chris Myers Q CA 90802 57 30.00
  19. 000019 Thanakom Paiboolsilp U CA 90805 56 45.75
  20. 000020 Leonardo Priego P CA 90703 77 23.00
  21. 000021 Evel Sanchez Q CA 90806 48 12.50
  22. 000022 Ruben Santacruz S CA 90813 40 20.00
  23. 000023 John Ung R CA 90806 40 25.00
  24. 000024 Fady Attia P CA 90706 40 28.00
  25. 000025 Sopha Dip I CA 90713 40 28.00
  26. 000026 Ricky Reed A CA 90221 40 11.50
  27. 000027 Julio Umana J CA 90221 40 60.00
  28. 000028 Rin Ran E CA 90808 47 22.45
  29. 000029 Faycal Ferhat Q CA 90814 25 65.00
  30. 000030 Habtom Tesfazghi P CA 90808 40 35.00
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: fstream isnt getting the data right

 
0
  #2
May 9th, 2005
>It is skipping some of the datas.
Which parts of the data are being skipped? Help us to help you by narrowing down the problem as much as possible. Otherwise we'll just ignore you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: fstream isnt getting the data right

 
0
  #3
May 9th, 2005
Originally Posted by Narue
>It is skipping some of the datas.
Which parts of the data are being skipped? Help us to help you by narrowing down the problem as much as possible. Otherwise we'll just ignore you.
Or talk about you on IRC Hah j/k
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #4
May 9th, 2005
I'm sorry for being dimwitted. Ok the first problem is that when I go in to my menu to choose read option it would skip the 0's in the first column for example
000001 Moshiur Ahmed I CA 90805 40 12.50
fstream would get only
1 Moshiur Ahmed I CA 90805 40 12.50.

My second problem is that fstream would only read and get up to or so
1 Moshiur Ahmed I CA 90805 40 12.50
2 Tasmia Amaat J CA 90706 35 25.60
3 Victor Barboza K CA 90706 42 30.00
4 Carlos Detorres I CA 90804 50 20.45
5 Tyree Ek O CA 90755 60 15.60
6 Amine El-Maziati M CA 90815 20 9.75
7 Loubna El-Maziati M CA 90815 25 10.50
8 Theresa Eyssallenne L CA 90715 40 20.00
9 Craig Griffith I CA 90804 40 45.00
10 Rodney Hallett H CA 90808 43 25.25
11 Andrew Hegstrom P CA 90808 45 13.50
12 Corey James D CA 90815 40 20.00


After this it would replace everything with 0's. Is it becasue there is something wrong with how I set it up or maybe the arrays are wrong.

Oh if you would like to run my program the ID code that it will asks for is "lbcc"
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: fstream isnt getting the data right

 
0
  #5
May 9th, 2005
>it would skip the 0's in the first column
Well that's easy. Leading zeros are meaningless on an integer, so they're ignored. If you want the whole string, read it as a string instead of an integer.

>After this it would replace everything with 0's.
Well, your array is of size 100 and there are only 30 records in the file you gave us...
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: fstream isnt getting the data right

 
0
  #6
May 9th, 2005
<unconstructive>
haha
</unconstructive>
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #7
May 9th, 2005
Yeah I know there is only 30 record but Its only reading 13 records. At first it was at a array of 30 but it would read up to 13 records and still had 0's so change it to 100 but still the samething
heres a pic
here
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,344
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fstream isnt getting the data right

 
0
  #8
May 9th, 2005
000013 Farsio Kottab E 90013 CA 90755 40 20.00
This gets you into a fail state.
Last edited by Dave Sinkula; May 9th, 2005 at 7:10 pm. Reason: The/This
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #9
May 9th, 2005
ohhhh. Im an idiot. Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 16
Reputation: Tetsu is an unknown quantity at this point 
Solved Threads: 0
Tetsu Tetsu is offline Offline
Newbie Poster

Re: fstream isnt getting the data right

 
0
  #10
May 17th, 2005
Hi everyone,
I ran into another problem. Im trying to sort the employees records by using the bubble sort with arrays and I just got the names arrays down but how would I get the rest of the information of that employees to go next to the names.
  1. void sortLastname(int a)
  2. {
  3. string temp = string();
  4. char pick = char();
  5.  
  6. for(int i=0; i<30; i++)
  7. {
  8. for (int j=0; j<30-i; j++)
  9. {
  10. if (lname[j] > lname[j+1]) // compare the two neighbors
  11. {
  12. temp = lname[j];
  13. lname[j] = lname[j+1]; // swap lname[j] and lname[j+1]
  14. lname[j+1] = temp;
  15. }
  16. }
  17. cout << temp <<endl;
  18.  
  19. //cout << setw(5) << left << ID[i] << setw(10) << left << fname[i] << setw(12) << lname[i] << setw(2) << middlei[i] << setw(7) << state[i] << setw(5) << right << zip[i] << setw(6) << right << hrWork[i] << setw(8) << right << ratePhour[i] << endl;
  20. }
Would I have to use another bubble sort in the bubble sort? Why is it that I cant seem to edit my first post. I like to paste my updated code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC