Click here to Skip to main content
15,881,173 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionUpdate data in CEDB using C++ and win32 APIs Pin
vijaywithu13-Apr-09 19:00
vijaywithu13-Apr-09 19:00 
QuestionC++ win32 console copy files problems Pin
h2_goh13-Apr-09 17:44
h2_goh13-Apr-09 17:44 
AnswerRe: C++ win32 console copy files problems Pin
flyxie13-Apr-09 19:08
flyxie13-Apr-09 19:08 
GeneralRe: C++ win32 console copy files problems Pin
h2_goh13-Apr-09 20:36
h2_goh13-Apr-09 20:36 
GeneralRe: C++ win32 console copy files problems Pin
flyxie13-Apr-09 20:51
flyxie13-Apr-09 20:51 
AnswerRe: C++ win32 console copy files problems Pin
KarstenK13-Apr-09 21:40
mveKarstenK13-Apr-09 21:40 
GeneralRe: C++ win32 console copy files problems Pin
Iain Clarke, Warrior Programmer14-Apr-09 3:52
Iain Clarke, Warrior Programmer14-Apr-09 3:52 
QuestionHelp with a simple phone book program Pin
Subrina Bisnauth13-Apr-09 13:20
Subrina Bisnauth13-Apr-09 13:20 
Hi I have an assignment to write a simple phone book program. I've done that & everything is working perfectly except my edit function. Can someone plz help me. Here is what I've done so far (sorry it's so long):
/*
Name: Subrina's Phone Book
Author: Subrina Bisnauth
Description: Electronic phone book
*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>

void menu();
void program_start();
void add();
void edit();
void search();
void del();
void view();
void program_exit();
void DelayProg(float sec);

char name[100];
char email[50];
char phone[15];
char namebuff[50];
char lnamebuff[50];
char buf[80];
int count;

FILE *phonebook;
FILE *pbtemp;
time_t now;
struct tm *ts;

/*Main Function*/
int main()
{
system("CLS");
program_start();
menu ();
view();
return 0;
}

/*Menu Function*/
void menu()
{
int ch = 0;
char ch2;
program_start();
printf("\t\t Please choose one . . .\n\n");
printf("\t\t\t1: < Add to phonebook >\n");
printf("\t\t\t2: < Edit phonebook >\n");
printf("\t\t\t3: < Search phonebook >\n");
printf("\t\t\t4: < Remove from phonebook >\n");
printf("\t\t\t5: < View phonebook >\n");
printf("\t\t\t6: < Exit phonebook >\n\n");
printf("\t\t\t\tPlease enter the number of your choice: ");
fflush(stdin);
scanf("%d", &ch);
switch(ch)
{
case 1:
add();
case 2:
edit();
case 3:
search();
case 4:
del();
case 5:
view();
case 6:
program_exit();
default:
program_start();
printf("\t\tYou must choose 1 - 5. Retry? Y/N ");
fflush(stdin);
scanf("%c", &ch2);
if(ch2=='y'||ch2=='Y')
{
menu();
}
else
{
program_exit();
}
}
}

/*Function executed at program start-up*/
void program_start()
{
system("CLS");
FILE *file;
}

/* Function that allows new entries to be made in the phonebook*/
void add()
{
int numbers[100];
char choice;
do {
int Exists = 0;
program_start();
printf("\t\t- Add to Phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","a+");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if(strcmp(name, namebuff)==0)
{
printf("\n\t\tThat entry is already stored in the phonebook . . .");
printf("\n\n\t\t\t");
system("PAUSE");
Exists=1;
}
}
if(!Exists)
{
ts = localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
printf("\n\t\t\tEmail : ");
gets(email);
fflush(stdin);
printf("\n\n\t\t\tPhone Number: ");
gets(phone);
fflush(stdin);
fprintf(phonebook, "%s%s\n%s\n\n%s\n%s\n", name, email, phone, buf);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}

/*Function to search for a particular entry in the phonebook*/
void search()
{
char choice;
int length;
do {
int Exists = 0;
int i = 1;
program_start();
printf("\t\t- Search phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","r");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if((strcmp(namebuff, name)==0))
{
Exists = 1;
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
}
}
if(Exists)
{
name[strlen(name)-2]=' ';
printf("\n\t\tSuccess . . .\n\n");
printf("\t\tName : %s\n", name);
printf("\t\tEmail : %s\n", email);
printf("\t\tPhone Number: %s\n", phone);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
else
{
length = strlen(name);
name[length-2] = '\0';
printf("\n\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.", name);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}

/*Function to edit entries in the phonebook*/
void edit()
{
char choice;
int length;
do {
int Exists = 0;
int i = 1;
int g;
char ph[6];
char nam[25];
char e_mail[15];
char tel[15];
program_start();
printf("\t\t- Edit an entry -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "|");
strcat(name, "\n");
phonebook = fopen("phonebook.txt","r+");
while(!feof(phonebook) && Exists==0)
{
fgets(namebuff, 50, phonebook);
if((strcmp(namebuff, name)==0))
{
Exists = 1;
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
}
}
if(Exists)
{
name[strlen(name)-2]=' ';
for (g=0;g<count;g++)
{
printf("\n\tPlease enter what you would like to edit - name, e-mail or tel");
scanf("%s", &ph);
if(ph=='name')
{
printf("\n\t\tPlease enter the correct name");
scanf("%s",&nam);
name[g]=nam;
}
if(ph=='e-mail')
{
printf("\n\t\tPlease enter the correct e-mail address");
scanf("%s",&e_mail);
email[g]=e_mail;
}
if(ph=='tel')
{
printf("\n\t\tPlease enter the correct telephone #");
scanf("%s",&tel);
phone[g]=tel;
}
}
}
else
{
length = strlen(name);
name[length-2] = '\0';
printf("\n\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.", name);
printf("\n\n\t\t\t");
system("PAUSE");
fflush(stdin);
}
fclose(phonebook);
program_start();
printf("If you would like to continue press Y or if you want to return to the menu press N\n ");
fflush(stdin);
scanf("%c", &choice);
if(choice!='y'&&choice!='Y')
{
menu();
}
}while(choice=='y'||choice=='Y');
}

/*Fuction to delete entries from phonebook*/
void del()
{
char conf;
int Exist = 0;
int len;
int i = 1;
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\n\t\t\tEnter Full Name: ");
fflush(stdin);
gets(name);
strcat(name, "\n");
if(phonebook = fopen("phonebook.txt", "r"))
{
printf("\n\n\t\tDelete the selection? Y/N ");
scanf("%c", &conf);
if(conf=='y'||conf=='Y')
{
if(pbtemp = fopen("phonebook", "w"))
{
while(!feof(phonebook))
{
fgets(namebuff, 50, phonebook);
len = strlen(namebuff);
if(namebuff[len-2]=='|')
{
namebuff[len-2]='\n';
namebuff[len-1]='\0';
if(strcmp(name, namebuff)!=0)
{
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
namebuff[len-2]='|';
namebuff[len-1]='\n';
fprintf(pbtemp, "%s%s%s", namebuff, email, phone);
}
else if((strcmp(name, namebuff)==0))
{
Exist = 1;
}
}
}
if(!Exist)
{
fclose(pbtemp);
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\tSorry . . .\n\n");
printf("\t\t\"%s\" was not found in phonebook.\n\n", name);
printf("\t\t\t");
system("PAUSE");
fflush(stdin);
remove("phonebook.txt");
menu();
}
}
else
{
fclose(phonebook);
fclose(pbtemp);
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\tError . . .\n\n");
printf("\t\t\tThe file could not be opened.\n\n");
printf("\t\t");
system("PAUSE");
fflush(stdin);
menu();
}
fclose(pbtemp);
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
remove("phonebook.txt");
rename("pbtemp.txt", "phonebook");
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\t\"%s\" was deleted from phonebook.", name);
printf("\n\n\t\t");
system("PAUSE");
fflush(stdin);
view();
}
else
{
fclose(phonebook);
len = strlen(name);
name[len-1]='\0';
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\t\"%s\" was not deleted.", name);
printf("\n\n\t\t");
system("PAUSE");
menu();
}
}
else
{
program_start();
printf("\t\t- Delete From phonebook -\n\n");
printf("\t\t\tError . . .\n\n");
printf("\t\t\tThe file could not be opened.\n\n");
printf("\t\t");
system("PAUSE");
menu();
}
}

/*Function that allows user to view entries in phonebook*/
void view()
{
namebuff[0] = '\0';
int len;
phonebook = fopen("phonebook.txt", "r");
program_start();
printf("\t\t- View phonebook -\n\n");
while(!feof(phonebook))
{
int i = 1;
fflush(stdin);
fgets(namebuff, 50, phonebook);
len = strlen(namebuff);
if(namebuff[len-2]=='|')
{
while(i<6)
{
if(i==1)
{
fgets(email, 50, phonebook);
fflush(stdin);
}
if(i==5)
{
fgets(phone, 15, phonebook);
fflush(stdin);
}
i++;
}
namebuff[len-2]=' ';
printf("\n\t\t\tName : %s", namebuff);
printf("\n\t\t\tEmail : %s", phone);
printf("\n\t\t\tPhone Number: %s\n", phone);
}
}
printf("\n\n\t\t\t");
system("PAUSE");
fclose(phonebook);
menu();
}

/*Function executed when program is about to close*/
void program_exit()
{
fflush(stdin);
program_start();
printf("\t\t\tThank you for using phonebook . . .");
DelayProg(2);
}

/*Function that controls the time delay when program is about to exit*/
void DelayProg(float sec)
{
float t = GetTickCount() + (1000L*sec);
while(GetTickCount() < t)
{

}
exit(1);
}
/*End of Program*/
AnswerRe: Help with a simple phone book program Pin
David Crow13-Apr-09 15:42
David Crow13-Apr-09 15:42 
QuestionC++ Socket/Multithread Programming Question Pin
djcouture13-Apr-09 9:18
djcouture13-Apr-09 9:18 
AnswerRe: C++ Socket/Multithread Programming Question Pin
bulg13-Apr-09 12:05
bulg13-Apr-09 12:05 
GeneralRe: C++ Socket/Multithread Programming Question Pin
djcouture14-Apr-09 3:29
djcouture14-Apr-09 3:29 
GeneralRe: C++ Socket/Multithread Programming Question Pin
bulg14-Apr-09 7:32
bulg14-Apr-09 7:32 
GeneralRe: C++ Socket/Multithread Programming Question Pin
djcouture15-Apr-09 5:27
djcouture15-Apr-09 5:27 
QuestionInternet to USB application Pin
2buck5613-Apr-09 9:17
2buck5613-Apr-09 9:17 
AnswerRe: Internet to USB application Pin
bulg13-Apr-09 12:08
bulg13-Apr-09 12:08 
GeneralRe: Internet to USB application Pin
2buck5613-Apr-09 13:04
2buck5613-Apr-09 13:04 
QuestionMore specific question, help please Pin
bulg13-Apr-09 14:32
bulg13-Apr-09 14:32 
AnswerRe: More specific question, help please Pin
2buck5613-Apr-09 15:58
2buck5613-Apr-09 15:58 
AnswerRe: Internet to USB application Pin
Iain Clarke, Warrior Programmer14-Apr-09 4:02
Iain Clarke, Warrior Programmer14-Apr-09 4:02 
GeneralRe: Internet to USB application Pin
2buck5614-Apr-09 5:01
2buck5614-Apr-09 5:01 
QuestionSet Class - Insert Function Pin
aab1990213-Apr-09 7:28
aab1990213-Apr-09 7:28 
AnswerRe: Set Class - Insert Function Pin
David Crow13-Apr-09 7:32
David Crow13-Apr-09 7:32 
QuestionDetect copied file? Pin
steve7606313-Apr-09 6:57
steve7606313-Apr-09 6:57 
AnswerRe: Detect copied file? Pin
David Crow13-Apr-09 7:28
David Crow13-Apr-09 7:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.