Thread: display problem after retrieving from text

  1. #1
    Unregistered
    Guest

    display problem after retrieving from text

    turbo c++ v4.5 compiler
    Win ME

    Here's my problem..
    if i choose to view a record / ALL the record

    the program can display it properly BUT it display twice..y?
    pls check my below code


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    struct date_en{
    	int dd,mm,yy;
    };
    
    struct data{
    	int id;
    	char name[20];
    	char addy[30];
    	int age;
    	struct date_en date;
    };
    
    add_new_rec(FILE *ptr)
    {
    	ptr=fopen("details.dat","a+");
    	struct data info;
    	printf("Enter ID: ");
    	info.id=0;
    	while(info.id < 1000 || info.id > 9999)
    	{
    
    		scanf("%d",&info.id);
    	}
    	printf("\nEnter Name: ");
    	scanf(" %[^\n]",info.name);
    	fflush(stdin);
    	printf("\nEnter Addy: ");
    	scanf(" %[^\n]",info.addy);
    	fflush(stdin);
    	printf("\nEnter age: ");
    	scanf("%d",&info.age);
    	printf("Enter date: ");
    	scanf("%d",&info.date.dd);
    	printf("/");
    	scanf("%d",&info.date.mm);
    	printf("/");
    	scanf("%d",&info.date.yy);
    	fwrite(&info,sizeof(struct data),1,ptr);
    	fclose(ptr);
    	return 0;
    }
    
    void view_all_rec(FILE *fptr)
    {
    	struct data info;
    	fptr=fopen("details.dat","r");
    	rewind(fptr);
    	while(!feof(fptr) )
    	{
    		fread(&info,sizeof(struct data),1, fptr);
    		printf("%d\n",info.id);
    		printf("%s\n",info.name);
    		printf("%s\n",info.addy);
    		printf("%d\n",info.age);
    		printf("%d/%d/%d",info.date.dd,info.date.mm,info.date.yy);
    		printf("press any key for next record!");
    		getch();
    	}
    	fclose(fptr);
    }
    
    void view_rec(FILE *ptr)
    {
    	struct data info;
    	ptr=fopen("details.dat","r");
    	int searchkey;
    	printf("Input ID no to search");
    	scanf("%d",&searchkey);
    //	rewind(ptr);
    	while(!feof(ptr) )
    	{
    		fread(&info,sizeof(struct data),1, ptr);
    		if (info.id == searchkey)
    		{
    			printf("%d\n",info.id);
    			printf("%s\n",info.name);
    			printf("%s\n",info.addy);
    			printf("%d\n",info.age);
    			printf("%d/%d/%d",info.date.dd,info.date.mm,info.date.yy);
    			getch();
    
    		}
    	}
    	fclose(ptr);
    }
    
    
    int print_menu()
    {
    	int choice;
    	printf("##########################\n");
    	printf("#   1.Add New Record     #\n");
    	printf("#   2.View Record        #\n");
    	printf("#   3.View all Record               #\n");
    	printf("##########################\n");
    	printf("Please choose what option you want:");
    	scanf("%d",&choice);
    	return choice;
    }
    
    main()
    {
    	FILE *ptr;
    	char dum;
    	int choose;
    	if((ptr = fopen ("details.dat","r"))==NULL){
    		ptr = fopen ("details.dat","w");
    	}
    	do {
    	choose=print_menu();
    	switch(choose)
    		{
    			case 1: add_new_rec(ptr);
    				break;
    			case 2: view_rec(ptr);
    				break;
    			case 3:
    					view_all_rec(ptr);
    					break;
    			default:
    				printf("Error - Wrong Option!");
    				scanf("%c",&dum);
    				getch();
    				clrscr();
    		}
    	} while (choose !=3);
    	return 0;
    }

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Code:
    scanf("%c",&dum); // for what purpose do you need this?
    getch();
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Unregistered
    Guest
    i use that to prevent error if some 1 input character rather than number

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    But this is not good, beacuse you still have to press a key three times (first you enter a character, then you press "enter" and the third one is a getch() function). Just use a getch() function.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display text in a Dialog Box
    By Dark_Phoenix in forum Windows Programming
    Replies: 9
    Last Post: 01-02-2009, 06:30 AM
  2. CEditView, how to display text on open
    By EmbeddedC in forum Windows Programming
    Replies: 2
    Last Post: 11-20-2008, 11:59 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM