Phonebook program!

Reply

Join Date: Jan 2006
Posts: 5
Reputation: mozala is an unknown quantity at this point 
Solved Threads: 0
mozala mozala is offline Offline
Newbie Poster

Phonebook program!

 
0
  #1
Jun 21st, 2006
I have a problem with search function by name ,could anyone help me?
This my code so far!!
Thanks in advance.
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class phonebook {
  12.  
  13. public :
  14.  
  15. phonebook() : MobilNumber(0), lastName(""), firstName(""), pcode(0),Email(""),city(""),Street("") { }
  16. void setMobilNumber( int num) { MobilNumber = num; }
  17. void setLastName (string lname) { lastName = lname; }
  18. void setFirstName(string fname) { firstName = fname; }
  19. void setEmail( string mail) { Email = mail; }
  20. void setcity (string cty) { city = cty; }
  21. void setstreet(string strt) { Street = strt; }
  22. void setpcode( int code) { pcode = code; }
  23. int getMobilNumber() const { return MobilNumber; }
  24. string getLastName() const { return lastName; }
  25. string getFirstName() const { return firstName; }
  26. int getpcode() const { return pcode; }
  27. string getEmail() const { return Email; }
  28. string getcity() const { return city; }
  29. string getstreet() const { return Street; }
  30.  
  31. private:
  32.  
  33. string firstName;
  34. string lastName;
  35. int MobilNumber;
  36. string Email;
  37. string city;
  38. string Street;
  39. int pcode;
  40. };
  41.  
  42.  
  43. int enterChoice();
  44. void textFile( fstream& );
  45. void updateRecord( fstream& );
  46. void newRecord( fstream& );
  47. void deleteRecord( fstream& );
  48. void outputLine( ostream&, const phonebook& );
  49. int getmobil( const char * const );
  50. char getname( const char * const );
  51. void searchbyname(fstream&);
  52. void searchbyphone(fstream&);
  53. enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE,SEARCH_BY_NAME,SEARCH_BY_PHONE,END};
  54.  
  55. /*----------------------------------Main function------------------------------*/
  56. int main()
  57. {
  58. // note: fstream object, both input and output
  59. fstream inOutUser( "credit.dat", ios::in | ios::out );
  60.  
  61. if ( !inOutUser )
  62. { cerr << "File could not be opened." << endl;
  63.  
  64. }
  65.  
  66. int choice;
  67.  
  68. while ( ( choice = enterChoice() )!=END )
  69. {
  70. switch ( choice )
  71. {
  72. case TEXTFILE: // create a text file to print
  73. textFile( inOutUser );
  74. break;
  75. case UPDATE: // update a record
  76. updateRecord( inOutUser );
  77. break;
  78. case NEW: // add a record
  79. newRecord( inOutUser );
  80. break;
  81. case DELETE: // remove a record
  82. deleteRecord( inOutUser );
  83. break;
  84. case SEARCH_BY_NAME:
  85. searchbyname(inOutUser);
  86. break;
  87. case SEARCH_BY_PHONE:
  88. searchbyphone(inOutUser);
  89. break;
  90. default:
  91. cerr << "Incorrect choice\n";
  92. break;
  93. }
  94. inOutUser.clear(); // resets end-of-file indicator
  95. }
  96.  
  97. return 0;
  98. }
  99. /*----------------------------User's choice------------------------------*/
  100. // Prompt for and input menu choice
  101. int enterChoice()
  102. {
  103. cout << "\nEnter your choice" << endl
  104. << "1 - store a formatted text file of \n"
  105. << " called \"print.txt\" for printing\n"
  106. << "2 - update a record\n"
  107. << "3 - add a new record\n"
  108. << "4 - delete a record\n"
  109. << "5 - search by name\n"
  110. << "6 - search by phone\n"
  111. << "7 - end program\n? ";
  112.  
  113. int menuChoice;
  114. cin >> menuChoice;
  115. return menuChoice;
  116. }
  117. /*-------------------------Creation of textfile------------------------------*/
  118. // Create formatted text file for printing
  119. void textFile( fstream &readFromFile )
  120. {
  121. ofstream outPrintFile( "c:\\print.txt", ios::out );
  122.  
  123. if ( !outPrintFile )
  124. { cerr << "Print file could not be opened." << endl;
  125. exit(1);
  126. }
  127.  
  128. outPrintFile << setiosflags( ios::left ) << setw( 10 )
  129. << "First name :" << setw( 16 ) << "Last Name :" << setw( 11 )
  130. << "Email address :"<<setw(10)
  131. << "City name :" <<setw(10)<<"Street name :"<<setw(10)
  132. << "Postal code :"<<resetiosflags( ios::left )
  133. << setw( 10 ) << "Mobil number" << endl;
  134. readFromFile.seekg( 0 ); // start at the beginning
  135. phonebook person;
  136. readFromFile.read( reinterpret_cast<char *>( &person ),
  137. sizeof( phonebook) );
  138.  
  139. while ( !readFromFile.eof() )
  140. {
  141. if ( person.getMobilNumber() != 0 ) // skip empty records
  142. outputLine( outPrintFile, person);
  143.  
  144. readFromFile.read( reinterpret_cast<char *>( &person ),
  145. sizeof(phonebook ) );
  146. }
  147.  
  148. }
  149. /*----------------------UpdateRecord function------------------------------*/
  150.  
  151. // Update mobil's number
  152. void updateRecord( fstream &updateFile )
  153. {
  154. int mobil = getmobil( "Enter Mobil number to update" );
  155.  
  156. updateFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );
  157.  
  158. phonebook person;
  159. updateFile.read( reinterpret_cast<char *>( &person ),
  160. sizeof( phonebook ) );
  161.  
  162. if ( person.getMobilNumber() != 0 )
  163. {
  164. outputLine( cout, person );
  165.  
  166. updateFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
  167. updateFile.write(
  168. reinterpret_cast<const char *>( &person ),
  169. sizeof( phonebook) );
  170. }
  171. else
  172. cerr << "Mobil #" << mobil
  173. << " has no information." << endl;
  174. }
  175. /*-------------------------New Users entryfile function---------------------*/
  176.  
  177. // Create and insert new record
  178. void newRecord( fstream &insertInFile )
  179. {
  180. int mobil= getmobil( "Enter new mobil number" );
  181.  
  182. insertInFile.seekg( (mobil - 1 ) * sizeof( phonebook) );
  183.  
  184. phonebook person;
  185. insertInFile.read( reinterpret_cast<char *>( &person ),
  186. sizeof(phonebook ) );
  187. string firstName;
  188. string lastName;
  189. string Email;
  190. string city;
  191. string Street;
  192. int pcode;
  193. if ( person.getMobilNumber() == 0 ) // make sure record is empty
  194. {
  195. cout << "Enter lastname, firstname,mobilnumber,postal code,Email address,city,street\n ";
  196. cin >> lastName >> firstName
  197. >> pcode >> Email
  198. >> city >> Street;
  199. person.setLastName( lastName );
  200. person.setFirstName( firstName );
  201. person.setpcode( pcode );
  202. person.setMobilNumber( mobil );
  203. person.setcity( city );
  204. person.setstreet( Street );
  205. person.setEmail( Email);
  206.  
  207. insertInFile.seekp( ( mobil - 1 ) *
  208. sizeof( phonebook ) );
  209. insertInFile.write(
  210. reinterpret_cast<const char *>( &person ),
  211. sizeof(phonebook ) );
  212. }
  213. else
  214. cerr << "mobilnumber #" << mobil
  215. << " already contains information." << endl;
  216. }
  217.  
  218.  
  219. /*-----------------------Deletion of User's details----------------------*/
  220.  
  221. // Delete an existing record
  222. void deleteRecord( fstream &deleteFromFile )
  223. {
  224. int mobil = getmobil( "Enter mobil number to delete" );
  225.  
  226. deleteFromFile.seekg( (mobil - 1) * sizeof( phonebook ) );
  227.  
  228. phonebook person;
  229. deleteFromFile.read( reinterpret_cast<char *>( &person ),
  230. sizeof( phonebook ) );
  231.  
  232. if ( person.getMobilNumber() != 0 ) // make sure record exists
  233. {
  234. phonebook * MobilOwner = new phonebook();
  235.  
  236. deleteFromFile.seekp( ( mobil - 1) *
  237. sizeof( phonebook ) );
  238. deleteFromFile.write(
  239. reinterpret_cast<const char *>( &MobilOwner ),
  240. sizeof( phonebook ) );
  241.  
  242. cout << "Mobil #" << mobil << " deleted." << endl;
  243. }
  244. else
  245. cerr << "Mobil #" << mobil << " is empty." << endl;
  246. }
  247. /*-------------------User's Output function--------------------------*/
  248.  
  249. // Output a line of User's information
  250. void outputLine( ostream &output, const phonebook &c )
  251. {
  252. output << setiosflags( ios::left ) << setw( 10 )
  253. << c.getMobilNumber() << setw( 16 ) << c.getLastName()
  254. << setw( 11 ) << c.getFirstName() << setw( 10 )
  255. << c.getEmail() <<setw( 10 ) << c.getcity() <<setw( 10 )
  256. << c.getstreet() <<setw( 10 ) << c.getpcode() <<setw( 10 )
  257. << setprecision( 2 ) << resetiosflags( ios::left )
  258. << setiosflags( ios::fixed | ios::showpoint ) ;
  259.  
  260. }
  261. /*---------------------Search User by the firstname---------------------*/
  262. void searchbyname(fstream &search_nameFile)
  263. {
  264.  
  265. string name = getname( "Enter the Last name of the user " );
  266.  
  267. //int name = reinterpret_cast<int>(pname);
  268. search_nameFile.seekg( ( name - 1 ) * sizeof( phonebook) );
  269.  
  270. phonebook person;
  271. search_nameFile.read( reinterpret_cast<char *>( &person ),
  272. sizeof( phonebook ) );
  273.  
  274. if ( person.getFirstName() != " " )
  275. {
  276. outputLine( cout, person );
  277.  
  278. search_nameFile.seekp( (name - 1 ) * sizeof( phonebook ) );
  279. search_nameFile.write(
  280. reinterpret_cast<const char *>( &person ),
  281. sizeof( phonebook) );
  282. }
  283. else
  284. cerr << "The name :" <<"["<< name
  285. <<"]"<< " does not exist." << endl;
  286. }
  287. /*------------------Search by the phone------------------------------------*/
  288. void searchbyphone(fstream &search_phoneFile)
  289. {
  290. int mobil = getmobil( "Enter Mobil number you want to search by" );
  291.  
  292. search_phoneFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );
  293.  
  294. phonebook person;
  295. search_phoneFile.read( reinterpret_cast<char *>( &person ),
  296. sizeof( phonebook ) );
  297.  
  298. if ( person.getMobilNumber() != 0 )
  299. {
  300. outputLine( cout, person );
  301.  
  302. search_phoneFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
  303. search_phoneFile.write(
  304. reinterpret_cast<const char *>( &person ),
  305. sizeof( phonebook) );
  306. }
  307. else
  308. cerr << "Mobil #" <<"["<< mobil
  309. <<"]"<< " does not exist." << endl;
  310. }
  311. /*-----------------get mobil number from the keyboard--------------------*/
  312. int getmobil( const char * const prompt )
  313. {
  314. int mobil;
  315.  
  316. do {
  317. cout << prompt << " (1 - 100): ";
  318. cin >> mobil;
  319. } while ( mobil < 1 || mobil > 100 );
  320.  
  321. return mobil;
  322. } // end getmobil
  323. /*-------------------------get name from the keyboard--------------------*/
  324. string getname(string name)
  325.  
  326. {
  327.  
  328. do
  329. {
  330. cin >> name;
  331. }while (name!="");
  332. return name;
  333. }
  334. /*&-------------------------------------END------------------------------------*/
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Phonebook program!

 
0
  #2
Jun 22nd, 2006
>> search_nameFile.seekg( ( name - 1 ) * sizeof( phonebook) );

variable name is a std::string -- why are you attempting to treat it as an integer? I would think that function searchbyname() should seek to the beginning of the file then enter a loop to sequentially read each record until eof or the desired name is found. Your version of that function does not do that at all.
Last edited by Ancient Dragon; Jun 22nd, 2006 at 7:25 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 5
Reputation: mozala is an unknown quantity at this point 
Solved Threads: 0
mozala mozala is offline Offline
Newbie Poster

Re: Phonebook program!

 
0
  #3
Jun 23rd, 2006
Originally Posted by Ancient Dragon
>> search_nameFile.seekg( ( name - 1 ) * sizeof( phonebook) );

variable name is a std::string -- why are you attempting to treat it as an integer? I would think that function searchbyname() should seek to the beginning of the file then enter a loop to sequentially read each record until eof or the desired name is found. Your version of that function does not do that at all.
so how about this i've changed the search method by refering to one the threads hear ,syntax is ok but not doing what i want and im even receing some warnigs can anybody try to run it figure out for me wht could the problem,thanx!!

This is it
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class phonebook {
  12.  
  13. public :
  14.  
  15. phonebook() : MobilNumber(0), lastName(""), firstName(""), pcode(0),Email(""),city(""),Street("") { }
  16. void setMobilNumber( int num) { MobilNumber = num; }
  17. void setLastName (string lname) { lastName = lname; }
  18. void setFirstName(string fname) { firstName = fname; }
  19. void setEmail( string mail) { Email = mail; }
  20. void setcity (string cty) { city = cty; }
  21. void setstreet(string strt) { Street = strt; }
  22. void setpcode( int code) { pcode = code; }
  23. int getMobilNumber() const { return MobilNumber; }
  24. string getLastName() const { return lastName; }
  25. string getFirstName() const { return firstName; }
  26. int getpcode() const { return pcode; }
  27. string getEmail() const { return Email; }
  28. string getcity() const { return city; }
  29. string getstreet() const { return Street; }
  30.  
  31. private:
  32.  
  33. string firstName;
  34. string lastName;
  35. int MobilNumber;
  36. string Email;
  37. string city;
  38. string Street;
  39. int pcode;
  40. };
  41.  
  42.  
  43. int enterChoice();
  44. void textFile( fstream& );
  45. void updateRecord( fstream& );
  46. void newRecord( fstream& );
  47. void deleteRecord( fstream& );
  48. void outputLine( ostream&, const phonebook& );
  49. int getmobil( const char * const );
  50. char getname( const char * const );
  51. void searchbyname(fstream&);
  52. void searchbyphone(fstream&);
  53. enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE,SEARCH_BY_NAME,SEARCH_BY_PHONE,END};
  54.  
  55. /*-----------------------------------Main function------------------------------*/
  56. int main()
  57. {
  58. // note: fstream object, both input and output
  59. fstream inOutUser( "credit.dat", ios::in | ios::out );
  60.  
  61. if ( !inOutUser )
  62. { cerr << "File could not be opened." << endl;
  63.  
  64. }
  65.  
  66. int choice;
  67.  
  68. while ( ( choice = enterChoice() )!=END )
  69. {
  70. switch ( choice )
  71. {
  72. case TEXTFILE: // create a text file to print
  73. textFile( inOutUser );
  74. break;
  75. case UPDATE: // update a record
  76. updateRecord( inOutUser );
  77. break;
  78. case NEW: // add a record
  79. newRecord( inOutUser );
  80. break;
  81. case DELETE: // remove a record
  82. deleteRecord( inOutUser );
  83. break;
  84. case SEARCH_BY_NAME:
  85. searchbyname(inOutUser);
  86. break;
  87. case SEARCH_BY_PHONE:
  88. searchbyphone(inOutUser);
  89. break;
  90. default:
  91. cerr << "Incorrect choice\n";
  92. break;
  93. }
  94. inOutUser.clear(); // resets end-of-file indicator
  95. }
  96.  
  97. return 0;
  98. }
  99. /*-----------------------------------User's choice------------------------------*/
  100. // Prompt for and input menu choice
  101. int enterChoice()
  102. {
  103. cout << "\nEnter your choice" << endl
  104. << "1 - store a formatted text file of \n"
  105. << " called \"print.txt\" for printing\n"
  106. << "2 - update a record\n"
  107. << "3 - add a new record\n"
  108. << "4 - delete a record\n"
  109. << "5 - search by name\n"
  110. << "6 - search by phone\n"
  111. << "7 - end program\n? ";
  112.  
  113. int menuChoice;
  114. cin >> menuChoice;
  115. return menuChoice;
  116. }
  117. /*-----------------------------------Creation of textfile------------------------------*/
  118. // Create formatted text file for printing
  119. void textFile( fstream &readFromFile )
  120. {
  121. ofstream outPrintFile( "c:\\print.txt", ios::out );
  122.  
  123. if ( !outPrintFile )
  124. { cerr << "Print file could not be opened." << endl;
  125. exit(1);
  126. }
  127.  
  128. outPrintFile << setiosflags( ios::left ) << setw( 10 )
  129. << "First name :" << setw( 16 ) << "Last Name :" << setw( 11 )
  130. << "Email address :"<<setw(10)
  131. << "City name :" <<setw(10)<<"Street name :"<<setw(10)
  132. << "Postal code :"<<resetiosflags( ios::left )
  133. << setw( 10 ) << "Mobil number" << endl;
  134. readFromFile.seekg( 0 ); // start at the beginning
  135. phonebook person;
  136. readFromFile.read( reinterpret_cast<char *>( &person ),
  137. sizeof( phonebook) );
  138.  
  139. while ( !readFromFile.eof() )
  140. {
  141. if ( person.getMobilNumber() != 0 ) // skip empty records
  142. outputLine( outPrintFile, person);
  143.  
  144. readFromFile.read( reinterpret_cast<char *>( &person ),
  145. sizeof(phonebook ) );
  146. }
  147.  
  148. }
  149. /*-----------------------------------UpdateRecord function------------------------------*/
  150.  
  151. // Update mobil's number
  152. void updateRecord( fstream &updateFile )
  153. {
  154. int mobil = getmobil( "Enter Mobil number to update" );
  155.  
  156. updateFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );
  157.  
  158. phonebook person;
  159. updateFile.read( reinterpret_cast<char *>( &person ),
  160. sizeof( phonebook ) );
  161.  
  162. if ( person.getMobilNumber() != 0 )
  163. {
  164. outputLine( cout, person );
  165.  
  166. updateFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
  167. updateFile.write(
  168. reinterpret_cast<const char *>( &person ),
  169. sizeof( phonebook) );
  170. }
  171. else
  172. cerr << "Mobil #" << mobil
  173. << " has no information." << endl;
  174. }
  175. /*-----------------------------------New Users entryfile function------------------------------*/
  176.  
  177. // Create and insert new record
  178. void newRecord( fstream &insertInFile )
  179. {
  180. int mobil= getmobil( "Enter new mobil number" );
  181.  
  182. insertInFile.seekg( (mobil - 1 ) * sizeof( phonebook) );
  183.  
  184. phonebook person;
  185. insertInFile.read( reinterpret_cast<char *>( &person ),
  186. sizeof(phonebook ) );
  187. string firstName;
  188. string lastName;
  189. string Email;
  190. string city;
  191. string Street;
  192. int pcode;
  193. if ( person.getMobilNumber() == 0 ) // make sure record is empty
  194. {
  195. cout << "Enter lastname, firstname,mobilnumber,postal code,Email address,city,street\n ";
  196. cin >> lastName >> firstName
  197. >> pcode >> Email
  198. >> city >> Street;
  199. person.setLastName( lastName );
  200. person.setFirstName( firstName );
  201. person.setpcode( pcode );
  202. person.setMobilNumber( mobil );
  203. person.setcity( city );
  204. person.setstreet( Street );
  205. person.setEmail( Email);
  206.  
  207. insertInFile.seekp( ( mobil - 1 ) *
  208. sizeof( phonebook ) );
  209. insertInFile.write(
  210. reinterpret_cast<const char *>( &person ),
  211. sizeof(phonebook ) );
  212. }
  213. else
  214. cerr << "mobilnumber #" << mobil
  215. << " already contains information." << endl;
  216. }
  217.  
  218.  
  219. /*-----------------------------------Deletion of User's details------------------------------*/
  220.  
  221. // Delete an existing record
  222. void deleteRecord( fstream &deleteFromFile )
  223. {
  224. int mobil = getmobil( "Enter mobil number to delete" );
  225.  
  226. deleteFromFile.seekg( (mobil - 1) * sizeof( phonebook ) );
  227.  
  228. phonebook person;
  229. deleteFromFile.read( reinterpret_cast<char *>( &person ),
  230. sizeof( phonebook ) );
  231.  
  232. if ( person.getMobilNumber() != 0 ) // make sure record exists
  233. {
  234. phonebook * MobilOwner = new phonebook();
  235.  
  236. deleteFromFile.seekp( ( mobil - 1) *
  237. sizeof( phonebook ) );
  238. deleteFromFile.write(
  239. reinterpret_cast<const char *>( &MobilOwner ),
  240. sizeof( phonebook ) );
  241.  
  242. cout << "Mobil #" << mobil << " deleted." << endl;
  243. }
  244. else
  245. cerr << "Mobil #" << mobil << " is empty." << endl;
  246. }
  247. /*-----------------------------------User's Output function-----------------------------------------*/
  248.  
  249. // Output a line of User's information
  250. void outputLine( ostream &output, const phonebook &c )
  251. {
  252. output << setiosflags( ios::left ) << setw( 10 )
  253. << c.getMobilNumber() << setw( 16 ) << c.getLastName()
  254. << setw( 11 ) << c.getFirstName() << setw( 10 )
  255. << c.getEmail() <<setw( 10 ) << c.getcity() <<setw( 10 )
  256. << c.getstreet() <<setw( 10 ) << c.getpcode() <<setw( 10 )
  257. << setprecision( 2 ) << resetiosflags( ios::left )
  258. << setiosflags( ios::fixed | ios::showpoint ) ;
  259.  
  260. }
  261.  
  262. /*-----------------------------------Search User by the name-----------------------------------------*/
  263. void searchbyname(fstream &search_nameFile)
  264.  
  265. {
  266. cout<<"An empty string or EOF will stop the program"<<endl;
  267.  
  268.  
  269. for (;;)
  270. {
  271. string name;
  272. cout<<"Enter the name";
  273. if ( !getline ( cin, name ) || name.empty() )
  274. break;
  275.  
  276. if ( !search_nameFile)
  277.  
  278. {
  279. cerr<<"Error opening file"<<endl;
  280. }
  281.  
  282. vector<string> lines;
  283. string line;
  284.  
  285. while ( getline ( search_nameFile, line ) )
  286.  
  287. {
  288. if ( line.find ( name ) )
  289. lines.push_back ( line );
  290. }
  291.  
  292. if ( lines.empty() )
  293. cout<<"No records found"<<endl;
  294. else if ( lines.size() == 1 )
  295. cout<<"One record found"<<endl;
  296. else
  297. cout<<"Multiple records found"<<endl;
  298.  
  299. for ( vector<string>::size_type i = 0; i < lines.size(); i++ )
  300. cout<< lines[i] <<endl;
  301. }
  302.  
  303.  
  304.  
  305. }
  306.  
  307. /*-----------------------------------Search by the phone-----------------------------------------*/
  308. void searchbyphone(fstream &search_phoneFile)
  309. {
  310. int mobil = getmobil( "Enter Mobil number you want to search by" );
  311.  
  312. search_phoneFile.seekg( ( mobil - 1 ) * sizeof( phonebook) );
  313.  
  314. phonebook person;
  315. search_phoneFile.read( reinterpret_cast<char *>( &person ),
  316. sizeof( phonebook ) );
  317.  
  318. if ( person.getMobilNumber() != 0 )
  319. {
  320. outputLine( cout, person );
  321.  
  322. search_phoneFile.seekp( ( mobil - 1 ) * sizeof( phonebook ) );
  323. search_phoneFile.write(
  324. reinterpret_cast<const char *>( &person ),
  325. sizeof( phonebook) );
  326. }
  327. else
  328. cerr << "Mobil #" <<"["<< mobil
  329. <<"]"<< " does not exist." << endl;
  330. }
  331. /*-----------------------------------get mobil number from the keyboard-----------------------------------------*/
  332. int getmobil( const char * const prompt )
  333. {
  334. int mobil;
  335.  
  336. do {
  337. cout << prompt << " (1 - 100): ";
  338. cin >> mobil;
  339. } while ( mobil < 1 || mobil > 100 );
  340.  
  341. return mobil;
  342. } // end getmobil
Last edited by Dave Sinkula; Jun 23rd, 2006 at 12:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Phonebook program!

 
0
  #4
Jun 23rd, 2006
To answer your specific question, as a start, it would be helpful to indicate what the warnings are and what code you think the warning refers to.

In a more general sense, I think you need to think about the relationship between what you refer to as phonebook, record, and person within your code. Using comments can help you dramatically in this regard.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 5
Reputation: mozala is an unknown quantity at this point 
Solved Threads: 0
mozala mozala is offline Offline
Newbie Poster

Re: Phonebook program!

 
0
  #5
Jun 23rd, 2006
Originally Posted by Lerner
To answer your specific question, as a start, it would be helpful to indicate what the warnings are and what code you think the warning refers to.

In a more general sense, I think you need to think about the relationship between what you refer to as phonebook, record, and person within your code. Using comments can help you dramatically in this regard.
Ok here r warnings that received!!

Deleting intermediate files and output files for project 'phonebook - Win32 Debug'.
--------------------Configuration: phonebook - Win32 Debug--------------------
Compiling...
phonebook.cpp
e:\school\c++\c++\phonebook.cpp(305) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std
::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
e:\school\c++\c++\phonebook.cpp(305) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char
_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
e:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
e:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Linking...

phonebook.exe - 0 error(s), 4 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Phonebook program!

 
0
  #6
Jun 23rd, 2006
There are few warnings that you should feel comfortable about ignoring, but warning C4786: is the exception to the rule. This particular warning is very common when I use VC6++ and STL containers like vectors. You can use a pragma statement to prevent them from showing up, use a different compiler, or ignore them. Since I forget the pragma statement syntax and don't want to get another compiler yet, I usually just ignore them.

The bad news is that these warnings aren't the source of your problems. I think working on the other suggestion in my previous post will go a long way to fixing what ails your program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
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: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Phonebook program!

 
0
  #7
Jun 23rd, 2006
put this as the very first line in stadafx.h but after include safeguards
  1. #pragma warning(disable: 4786)

Note: NO semicolon! That warning only occurs when your program is compiled in debug mode and can safely be ignored. If you don't disable it, it will muddy up compiler complaints quite badly.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 5
Reputation: mozala is an unknown quantity at this point 
Solved Threads: 0
mozala mozala is offline Offline
Newbie Poster

Re: Phonebook program!

 
0
  #8
Jun 23rd, 2006
Originally Posted by Ancient Dragon
put this as the very first line in stadafx.h but after include safeguards
  1. #pragma warning(disable: 4786)

Note: NO semicolon! That warning only occurs when your program is compiled in debug mode and can safely be ignored. If you don't disable it, it will muddy up compiler complaints quite badly.
wow its amazing ,it did reduce them to 2 warnings.
Thnx ppl but still this program doesnt do eactly what i want to..
Have u ever!! i think i did all could but now i've got no clue how to approach this or what change it to make it work....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Phonebook program!

 
0
  #9
Jun 25th, 2006
To me at least, the member variables of the phonebook class represent the attributes of a person/customer or phonebook record, but they don't represent a phonebook. A phone book to me is a collection of phonebook records or people/customers. This means I think your C++ syntax is (mostly) correct (you mentioned two remaining warnings after adding the pragma statement, so there may still be some concerns with the syntax, too), but you have an underlying logic problem. I would redesign the program by renaming your phonebook class customer or person or record, and then determine which container class---array, list, map, set, vector, (?file?)---would be the most appropriate for a collection of customers/people/records. Then you could either keep the independent functions as listed and pass them the appropriate parameters or roll them into another class called phonebook that has a container of the customers/people/records as a member variable and the current independent functions as member variables.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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