943,900 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13425
  • C++ RSS
Apr 19th, 2004
1

C++ Address Book

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
The Shadows is offline Offline
3 posts
since Apr 2004
Apr 20th, 2004
0

Re: C++ Address Book

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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
The Shadows is offline Offline
3 posts
since Apr 2004
Apr 28th, 2004
1

Re: C++ Address Book

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
Reputation Points: 19
Solved Threads: 0
Light Poster
BlackDice is offline Offline
43 posts
since Apr 2004
Apr 28th, 2004
1

Re: C++ Address Book

it is faster to type but there's no difference in the code generated, test it and see for yourself.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Apr 28th, 2004
0

Re: C++ Address Book

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!!
Reputation Points: 19
Solved Threads: 0
Light Poster
BlackDice is offline Offline
43 posts
since Apr 2004
Apr 28th, 2004
0

Re: C++ Address Book

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.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Sep 2nd, 2010
0
Re: C++ Address Book
please help me in making address book
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bonethugs is offline Offline
2 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: use c++ compiler
Next Thread in C++ Forum Timeline: video/image editing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC