| | |
C++ Address Book
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 3
Reputation:
Solved Threads: 0
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)
#include <iostream> #include <string> #include <fstream> #include <algorithm> using namespace std; struct student { string first_name; string last_name; string phone_number; string age; int month; int day; int year; }; student log[100]; //double uslessfunction1(student log[].first_name,student log[].last_name,student log[].phone_number); void uslessfunction1(student log[],ifstream infile,int x); //double uslessfunction2(student log[].age,student log[].month,student log[].day,student log[].year); void uslessfunction2(student log[],ifstream infile,int x); void main() { double num=0,x=0,counter=0; ifstream infile; ofstream outfile; infile.open("database.txt"); outfile.open("output.txt"); while(!infile.eof()){ { counter=counter+1; // uslessfunction1(log[].first_name,log[].last_name,log[].phone_number); uslessfunction1(log[x],infile,x); // uslessfunction2(log[].age,log[].month,log[].day,log[].year); uslessfunction2(log[x],infile,x); x=x+1; } for (x=0;x<counter;x++) sort(log[x].first_name,log[x].first_name,10); for (x=0;x<counter;x++) outfile<<log[x].last_name<<", "<<log.first_name<<endl<<log[x].phone_number<<endl<<log[x].month<<"/"<<log[x].day<<"/"<<log[x].year<<endl; } //double uslessfunction1(student log[].first_name,student log[].last_name,student log[].phone_number) void uslessfunction1(student log[],ifstream infile,int x) { infile>>log[x].first_name; infile>>log[x].last_name; infile>>log[x].phone_number; } //double uslessfunction2(student log[].age,student log[].month,student log[].day,student log[].year) void uslessfunction2(student log[],ifstream infile,int x) { infile>>log[x].age; infile>>log[x].month,log[x].day,log[x].year; }
•
•
Join Date: Apr 2004
Posts: 3
Reputation:
Solved Threads: 0
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

If anyone is interested in the code, here it is
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> #include <algorithm> using namespace std; struct student { string first_name; string last_name; string phone_number; string age; string month; string day; string year; }; student log[100]; void main() { int num=0,x=0,counter=0,y=0; student temp; ifstream infile; ofstream outfile; infile.open("database.txt"); outfile.open("output.txt"); while(!infile.eof()) { counter=counter+1; infile>>log[x].first_name; infile>>log[x].last_name; infile>>log[x].phone_number; infile>>log[x].age; infile>>log[x].month; infile>>log[x].day; infile>>log[x].year; x=x+1; } for (x=0;x<counter;x++){ for(y=x+1;y<10;y++){ if (log[x].last_name > log[y].last_name){ temp = log[x]; log[x] = log[y]; log[y] = temp; } } } for (x=0;x<counter;x++) 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; }
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
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!!
![]() |
Similar Threads
- A C++ Simple Address book (C++)
- Creating a Address Book using Random Access Files (Visual Basic 4 / 5 / 6)
- Where does Address Book & email hide? (Web Browsers)
- lost address book addresses (OS X)
- Address book deleted? HELP (OS X)
- Saving Address book and Messages in Outlook Express (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: RTTI typeinfo question
- Next Thread: Any better way of implementing this recusively?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





