I have a project here (console address book program)
Code:
#include<iostream>
#include<fstream>
#include<windows.h>
#include<cstdio>
#include<string>
using namespace std;
void clrscr(void) //Code by Sunlight, http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385
{
DWORD n;
DWORD size;
COORD coord={0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h,&csbi);
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(h,TEXT(' '),size,coord,&n);
GetConsoleScreenBufferInfo(h,&csbi);
FillConsoleOutputAttribute(h,csbi.wAttributes,size,coord,&n);
SetConsoleCursorPosition(h,coord);
}
struct record
{
char name[30];
char phone[15];
record *next;
}*start, *newptr, *save, *ptr, *rear;
record *create_new_record();
void insert(record *);
int disp1(record *);
int add();
int main()
{
start=rear=NULL;
char spc[6]="\n\t\t";
int choice;
do
{
clrscr();
cout<<spc<<"--------------( PowerContact )-------------";
cout<<spc<<" A console-based contacts management utility ";
cout<<spc<<" (c)ultrabot90/Kashish Sharma, 2007 ";
cout<<spc<<"---------------( Main Menu )---------------";
cout<<spc<<" 1.Display all contacts ";
cout<<spc<<" 2.Add a new contact ";
cout<<spc<<" 3.Exit ";
cout<<spc<<"Enter your choice...";
cin>>choice;
switch(choice)
{
case '1':
{
ifstream readfile;
readfile.open("db.txt", ios::in);
if(!readfile)
{
cout<<"\nFile read error.";
return 1;
}
readfile.read((char *) &start, sizeof(record));
disp1(start);
break;
}
case '2':
cout<<"\n2 is it?";
add();
break;
case '3':
return 1;
break;
}
}while(choice!=3);
return 0;
}
int add()
{
newptr=create_new_record();
if(newptr!=NULL)
{
cout<<"\nCreate record success.";
getchar();
}
else
{
cout<<"\nCreate record error.";
return 1;
}
insert(newptr);
getchar();
return 0;
}
record *create_new_record(void)
{
ptr=new record;
cout<<"\nName : ";
cin.getline(ptr->name,30);
cout<<"\nPhone : ";
cin.getline(ptr->phone,15);
ptr->next=NULL;
ofstream writefile;
writefile.open("db.txt", ios::binary|ios::app);
if(!writefile)
{
cout<<"\nFile write error.";
system("PAUSE");
}
writefile.write((char *) &ptr, sizeof(record));
//writefile<<ptr->name<<"\n";
//writefile<<ptr->phone<<"\n";
writefile.close();
return ptr;
}
void insert(record *np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
int disp1(record *np)
{
while(np!=NULL)
{
clrscr();
cout<<"\nName : "<<np->name<<"\nPhone : "<<np->phone;
np=np->next;
cout<<"\nPress enter to continue.";
getchar();
}
return 0;
}
The problem is that when I enter 2 as a choice in the menu, nothing happens...Also, right now it can only add and display entries...I want to be able to 1. *search* the entries 2. sort and display all entries (the other basic functions - edit, delete - can be done by me, I guess)
Edit - Maybe I should put it as an attachment...its filling up the page x_x
-regards,
ultrabot90