C'de çalışan fonksiyon nasıl durdurulur?

NoWeDoR

Öğrenci
Katılım
14 Temmuz 2015
Mesajlar
9
Reaksiyon puanı
0
Puanları
1
Arkadaşlar basit bir telefon defteri uygulaması yazıyorum. İki fonksiyonu tamamladım ancak küçük bir problemim var.


İkinci fonksiyonun sonunda görüldüğü gibi bir if-else deyimi var.
ve dosya içinden okuduğu verilerden eğer isim, soyisim ya da numara'dan herhangi birisi boş ise "dosyada hiç kayıt yok" diye kullanıcıya bir mesaj verip o fonksiyondan çıkmak istiyorum.
Ancak bunun için return; komutu işe yaramıyor ve program devam ederek else yapısının içine atlayarak ekranda anlamsız şeyler gösteriyor. Bu nasıl çözülebilir?

Kod:
#include <stdio.h>
#include <stdlib.h>      // "stdlib" library contains of exit() function
#include <malloc.h>     // "malloc" library contains of malloc() function
#include <Windows.h>   // "Windows" library contains of Sleep() function which waits the system as you want
#include <io.h>       // "io" library contains of filelength() function


struct personKnowledge
{
    char name[32];
    char surname[32];
    char number[32];
};


FILE *ptrFILE, *ptrFILE1;
long int recordLength, totalRecordLength, location;
int number, totalRecordNumber;
static int counter = 0;


void newRecord();
void display();
void deletE();
void add();
void update();


int main()
{
    int choice;
    do
    {
        printf("\n\t\t --- Phone Book Program ---");
        printf("\n\n\t\t 1) New record");   // The options are being presented to user
        printf("\n\n\t\t 2) Display person knowledge");
        printf("\n\n\t\t 3) Delete someone");
        printf("\n\n\t\t 4) Add person to list");
        printf("\n\n\t\t 5) Update person knowledge");
        printf("\n\n\t\t 6) Exit");
        printf("\n\n\nEnter your choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
        {
            newRecord();
            break;
        }
        case 2:
        {
            display();
            break;
        }
        case 3:
        {
            break;
        }
        case 4:
        {
            break;
        }
        case 5:
        {
            break;
        }
        case 6:
        {
            printf("\nWorking has been completed.\n");
            return 0;
        }
        default:
        {
            printf("\nWrong entry! The program has been terminated.\n");
            break;
        }
        }
    } while (choice >= 1 && choice <= 6);
    return 0;
}


void newRecord()
{
    if (counter > 0)
    {
        printf("You have already entered '1' and opened a file. To add a person please enter '4'\n");
        return;
    }
    else
    {
        if ((ptrFILE = fopen("Phone Book.dat", "wb")) == NULL)
        {
            printf("The file couldn't open\n");
            exit(0);
        }
        system("cls");   // Screen is being cleaned
        struct personKnowledge *p;   // p means person
        p = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));   // Memory is being allocated
        fflush(stdin);
        recordLength = sizeof(*p);   // size of p
        printf("\n\Express person name: ");   // User is entering the person's knowledge and they are being saved in file
        gets(p->name);
        printf("Express %s's surname: ", p->name);
        gets(p->surname);
        printf("Express %s's number: ", p->name);
        gets(p->number);
        fwrite(&(*p), recordLength, 1, ptrFILE);
        printf("\nPlease wait, information is saving to file..\n");
        Sleep(750);
        printf("*-* Saving operation has been completed succesfully. *-*\n");
        free(p);
    }
    fclose(ptrFILE);
    counter++;
}


void display()
{
    if ((ptrFILE = fopen("Phone Book.dat", "rb")) == NULL)
    {
        printf("The file couldn't open\n");
        exit(0);
    }
    system("cls");   // Screen is being cleaned
    struct personKnowledge *s;   // s means searching
    s = (struct personKnowledge *)malloc(sizeof(struct personKnowledge));
    fflush(stdin);
    recordLength = sizeof(*s);
    totalRecordLength = filelength(fileno(ptrFILE));
    totalRecordNumber = totalRecordLength / recordLength;
    printf("\n\nExpress person record number which you search: ");
    scanf("%d", &number);
    location = (number - 1)*(recordLength);
    fseek(ptrFILE, location, SEEK_SET);
    fread(&(*s), recordLength, 1, ptrFILE);
    printf("\n*-* Person knowledge which you search *-*\n");
    Sleep(750);
    if (s->name == NULL || s->surname == NULL || s->number == NULL)
    {
        printf("There is no record like this.\n");
        return;
    }
    else
    {
        printf("Name: %s\n", s->name);
        printf("Surname: %s\n", s->surname);
        printf("Number: %s\n", s->number);
    }
    free(s);
    fclose(ptrFILE);
}
 

Arthassin

Öğrenci
Katılım
4 Eylül 2009
Mesajlar
39
Reaksiyon puanı
0
Puanları
0
if (s->name == NULL || s->surname == NULL || s->number == NULL)
{
printf("There is no record like this.\n");
return;
}

Sorun yukardaki if'teki kosulun hicbir zaman dogru olmamasi.
name, surname, number degiskenlerini static olarak allocate ettigin icin bunlar hicbir zaman NULL olmaz. struct'u olustururken, bu degiskenlere ilk harf olarak bitis sembolu ('\0') atayarak, yukardaki if'in icinde ilk karakter '\0' mi diye kontrol edip ona gore fonksiyondan cikabilirsin.
 
Üst