helpppppp me with this program

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

Join Date: Jan 2008
Posts: 2
Reputation: abeshare is an unknown quantity at this point 
Solved Threads: 0
abeshare abeshare is offline Offline
Newbie Poster

helpppppp me with this program

 
0
  #1
Jan 15th, 2008
// I want to dislpay my cout in an acsending order by ssn

// here is the program
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <fstream>
  5. #include <math.h>
  6. #include <ctype.h>
  7. //Ebrahim Sharif
  8. //ECE 370
  9. //1-16-2008
  10.  
  11. using namespace std;
  12.  
  13. void getPersonalInfo();//function prototypes
  14. int Str2Int(string InpStr);
  15. long Str2Long(string);
  16. void lookupPerson( char );
  17.  
  18. struct personalInfo
  19. {
  20. string name;
  21. int Age;
  22. long ssn;
  23. string Address;
  24. };
  25. ifstream readfile;
  26. string readfilename = "a1.txt";//read in file
  27. personalInfo person[12];
  28. int NumberOfPeople;
  29.  
  30. int main()
  31. {
  32. cout<<"*********Assinment number one******"<<endl<<endl;
  33.  
  34. int i,j,;
  35. bool stoploop;
  36. char lastInitial;
  37. char letter;
  38.  
  39.  
  40. //Step 1: Openning files
  41. readfile.open(readfilename.c_str());
  42.  
  43.  
  44. if (readfile.fail())//checking if file exists
  45. {
  46. cout<<"\n chech and make sure that there is an existing input file."<<endl;
  47. system ("PAUSE");
  48. return 0;
  49.  
  50. }
  51. else
  52. {
  53. cout<<"\nfile is found and was opened"<<endl;
  54. cout<<" "<<endl;
  55. }
  56.  
  57. //Step 2: finction 1 to read files
  58. getPersonalInfo();
  59.  
  60. //Step 3: cout all persons Info
  61. for (j=0;j<NumberOfPeople ; j++)
  62. {
  63. cout << person[j].name <<endl;
  64. cout << person[j].Age <<endl;
  65. cout << person[j].ssn<<endl;
  66. cout << person[j].Address << endl;
  67. cout << endl;
  68. }
  69.  
  70. //Step 4: looking up or searching persons
  71. stoploop = false;
  72. do
  73. {
  74. cout<<"********** search center ***********"<<endl<<endl;
  75. cout << "Enter Last Name Initial to look up person : ";
  76. cin>>lastInitial;
  77.  
  78. lookupPerson(lastInitial);
  79.  
  80. cout << "Do you want to search again press any key otherwise 'N' or 'n' to quit : ";
  81. cin>>letter;
  82.  
  83. if(letter == 'N' || letter == 'n')
  84. {
  85.  
  86. stoploop = true;//stop searching
  87. }
  88. else
  89. {
  90. stoploop = false; //keep asking for search
  91. }
  92.  
  93. }while(!stoploop);
  94.  
  95.  
  96. //Step : Close Files
  97. readfile.close();
  98.  
  99.  
  100. system ("PAUSE");
  101. return 0;
  102. }
  103.  
  104. void getPersonalInfo()
  105. {
  106. int i;
  107. string temp; //temperary memory for convertion
  108. string space;
  109.  
  110.  
  111. i=0;//First Person (0th element)
  112.  
  113. while (readfile)
  114. {
  115. getline(readfile,person[i].name);//get person's name
  116.  
  117. getline(readfile,temp);//get age and convert to int
  118. person[i].Age = Str2Int(temp);
  119.  
  120. getline(readfile,temp);//get ssn and conveert to long
  121. person[i].ssn = Str2Long(temp);
  122.  
  123. getline(readfile,person[i].Address); //get persons sddress
  124.  
  125. getline(readfile,space); // skip line
  126.  
  127. i++;//loop for next person
  128. }
  129.  
  130. NumberOfPeople = i;
  131.  
  132. }
  133.  
  134. int Str2Int(string InpStr)
  135. {
  136. char *str_ptr;
  137. int Strlength;
  138. int Outputage;
  139. int i;
  140.  
  141. Strlength = InpStr.length();//get Length of the string
  142.  
  143. str_ptr = new char[Strlength]; // allocate memory needed for the input string
  144.  
  145. for (i = 0; i < Strlength; i++)//copy string to a poiunter for conversion need
  146. str_ptr[i] = InpStr[i];
  147.  
  148. Outputage = atoi (str_ptr);//(alphabetic to integer)
  149.  
  150.  
  151. return (Outputage);
  152. }
  153.  
  154. long Str2Long(string InpStr)
  155. {
  156. char *str_ptr;
  157. int Strlength;
  158. long outputssn;
  159. int i;
  160.  
  161. Strlength = InpStr.length();
  162.  
  163. str_ptr = new char[Strlength];
  164.  
  165. for (i = 0; i < Strlength; i++)
  166. {
  167. str_ptr[i] = InpStr[i];
  168. }
  169.  
  170. outputssn = atol(str_ptr);
  171.  
  172. return (outputssn);
  173. }
  174.  
  175. void lookupPerson(char lastInitial)
  176. {
  177. int j;
  178. bool PersonFound ;
  179.  
  180. for (j=0;j<NumberOfPeople ; j++)
  181. {
  182. if(toupper(person[j].name[0]) == toupper(lastInitial))
  183. { //display on screen
  184. cout << person[j].name <<endl;
  185. cout << person[j].Age <<endl;
  186. cout << person[j].ssn <<endl;
  187. cout << person[j].Address << endl;
  188. cout << endl;
  189.  
  190. PersonFound = true;
  191. }
  192. }
  193.  
  194. if(PersonFound == false)
  195. {
  196. cout<<"No one is found with a Last name initial '"<<lastInitial<<"'."<<endl<<endl;
  197. }
  198.  
  199. }
Last edited by Narue; Jan 16th, 2008 at 9:35 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2
Reputation: abeshare is an unknown quantity at this point 
Solved Threads: 0
abeshare abeshare is offline Offline
Newbie Poster

Re: helpppppp me with this program

 
0
  #2
Jan 15th, 2008
hello anyone
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,819
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: helpppppp me with this program

 
1
  #3
Jan 16th, 2008
Originally Posted by abeshare View Post
hello anyone
Hi
Originally Posted by abeshare View Post
I want to dislpay my cout in an acsending order by ssn ... -lot's of code-
Cool. Do you have a question?

Before you post again please read this about code tags and this about asking clear questions, meaningfull titles and bumping.
Niek
Last edited by niek_e; Jan 16th, 2008 at 4:08 am.
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