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. :)

#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;
}

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

#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

it is faster to type but there's no difference in the code generated, test it and see for yourself.

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!!

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. :)

please help me in making address book

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.