C++ Address Book

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

Join Date: Apr 2004
Posts: 3
Reputation: The Shadows is an unknown quantity at this point 
Solved Threads: 0
The Shadows The Shadows is offline Offline
Newbie Poster

C++ Address Book

 
1
  #1
Apr 19th, 2004
Hi, I have to write an address book using structs for a class. I have 90% of the code done, but I am having trouble ordering it. It is supposed to read in 10 entries from a file and then sort them by last name. All the information also has to be swapped. I am using Microsoft VC++ 6. I would appreciate any input on what might be wrong with this program. Thanks for the help.

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <algorithm>
  5. using namespace std;
  6. struct student
  7. {
  8. string first_name;
  9. string last_name;
  10. string phone_number;
  11. string age;
  12. int month;
  13. int day;
  14. int year;
  15. };
  16. student log[100];
  17. //double uslessfunction1(student log[].first_name,student log[].last_name,student log[].phone_number);
  18. void uslessfunction1(student log[],ifstream infile,int x);
  19. //double uslessfunction2(student log[].age,student log[].month,student log[].day,student log[].year);
  20. void uslessfunction2(student log[],ifstream infile,int x);
  21. void main()
  22. {
  23. double num=0,x=0,counter=0;
  24. ifstream infile;
  25. ofstream outfile;
  26. infile.open("database.txt");
  27. outfile.open("output.txt");
  28. while(!infile.eof()){
  29. { counter=counter+1;
  30.  
  31. // uslessfunction1(log[].first_name,log[].last_name,log[].phone_number);
  32. uslessfunction1(log[x],infile,x);
  33. // uslessfunction2(log[].age,log[].month,log[].day,log[].year);
  34. uslessfunction2(log[x],infile,x);
  35. x=x+1;
  36. }
  37.  
  38. for (x=0;x<counter;x++)
  39. sort(log[x].first_name,log[x].first_name,10);
  40. for (x=0;x<counter;x++)
  41. outfile<<log[x].last_name<<", "<<log.first_name<<endl<<log[x].phone_number<<endl<<log[x].month<<"/"<<log[x].day<<"/"<<log[x].year<<endl;
  42.  
  43. }
  44. //double uslessfunction1(student log[].first_name,student log[].last_name,student log[].phone_number)
  45. void uslessfunction1(student log[],ifstream infile,int x)
  46. {
  47. infile>>log[x].first_name;
  48. infile>>log[x].last_name;
  49. infile>>log[x].phone_number;
  50. }
  51. //double uslessfunction2(student log[].age,student log[].month,student log[].day,student log[].year)
  52. void uslessfunction2(student log[],ifstream infile,int x)
  53. {
  54. infile>>log[x].age;
  55. infile>>log[x].month,log[x].day,log[x].year;
  56. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 3
Reputation: The Shadows is an unknown quantity at this point 
Solved Threads: 0
The Shadows The Shadows is offline Offline
Newbie Poster

Re: C++ Address Book

 
0
  #2
Apr 20th, 2004
Just incase anyone is having the same problem, I got it after much work. I guess my inputs on the date were wrong, causing traumatic memory problems

If anyone is interested in the code, here it is
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <algorithm>
  5. using namespace std;
  6. struct student
  7. {
  8. string first_name;
  9. string last_name;
  10. string phone_number;
  11. string age;
  12. string month;
  13. string day;
  14. string year;
  15. };
  16. student log[100];
  17. void main()
  18. {
  19. int num=0,x=0,counter=0,y=0;
  20. student temp;
  21. ifstream infile;
  22. ofstream outfile;
  23. infile.open("database.txt");
  24. outfile.open("output.txt");
  25. while(!infile.eof())
  26. { counter=counter+1;
  27.  
  28. infile>>log[x].first_name;
  29. infile>>log[x].last_name;
  30. infile>>log[x].phone_number;
  31. infile>>log[x].age;
  32. infile>>log[x].month;
  33. infile>>log[x].day;
  34. infile>>log[x].year;
  35. x=x+1;
  36. }
  37. for (x=0;x<counter;x++){
  38. for(y=x+1;y<10;y++){
  39. if (log[x].last_name > log[y].last_name){
  40. temp = log[x];
  41. log[x] = log[y];
  42. log[y] = temp;
  43. }
  44. }
  45. }
  46. for (x=0;x<counter;x++)
  47. outfile<<log[x].last_name<<", "<<log[x].first_name<<endl<<log[x].phone_number<<endl<<log[x].month<<"/"<<log[x].day<<"/"<<log[x].year<<endl<<endl;
  48. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 45
Reputation: BlackDice is an unknown quantity at this point 
Solved Threads: 0
BlackDice's Avatar
BlackDice BlackDice is offline Offline
Light Poster

Re: C++ Address Book

 
1
  #3
Apr 28th, 2004
just wanted to let you know that you've re-discovered the 'bubble sort' algorithm. The code looks fine except that you should use 'x += 1' instead of 'x = x + 1' -- it's faster. you may not see much of a difference in a loop of that many iterations or with numbers, but it'll really help with strings and loops with more iterations. Besides, it's faster to type! Keep up the good work
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: C++ Address Book

 
1
  #4
Apr 28th, 2004
it is faster to type but there's no difference in the code generated, test it and see for yourself.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 45
Reputation: BlackDice is an unknown quantity at this point 
Solved Threads: 0
BlackDice's Avatar
BlackDice BlackDice is offline Offline
Light Poster

Re: C++ Address Book

 
0
  #5
Apr 28th, 2004
According to this book I have, 'C++ for Game Programmers' published by Charles River Media, it says that it's a lot faster when dealing with CString operations, I will go back to it and make sure, but I'm pretty sure now. I haven't tested it, but I was taking the book's word. But I appreciate your comment; that makes me investigate!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: C++ Address Book

 
0
  #6
Apr 28th, 2004
ok, if you're talking about something high level like that it may be true, but for say some simple integer x it makes no difference in the generated asm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC