0

the only problem i face here i want an array for names and one for mobile numbers which i did in addphonenumber(); funtion now i need to take name from file into serchphonebook(); function and compare it with what user search for but the problem that i face is that fgets(); take full name with number so i cant compare name of what stored in phonebook with what user searched for NOTES: the names need to be full like "andy ramsy kamal" with spaces , and if user searchd for "an" for example the code need to find all names start with "an" with there phone numbers

 #include<stdio.h>

void MainMenu();
int AddPhoneNumber();
void SearchPhoneBook();
void DeletePhoneNumber();
void DispalyPhoneBook();

void main()
{
    MainMenu();
}
void MainMenu()
{
    int Choice;
    printf("\t****Main Menu****\n");
    printf("1)Add Phone Number\n");
    printf("2)Search Phone Book\n");
    printf("3)Delete Phone Number\n");
    printf("4)Dispaly Phone Book");
    printf("Choose your Number\n");
    scanf("%d", &Choice);
        switch (Choice)
        {
        case 1:
            AddPhoneNumber();
            break;
        case 2:
            SearchPhoneBook();
            break;
        case 3:
            DeletePhoneNumber();
            break;
        case 4:
            DispalyPhoneBook();
            break;
        case 5:
            exit(0);

        default:
            MainMenu();
        }
}
int AddPhoneNumber()
{
    FILE *ph;
    ph = fopen("C:/Users/ec/Desktop/PhoneBook.txt", "a");
    char Name[3][50];
    char temp;
    char Mobile[3][11];
    for (int i = 0; i < 3; i++)
    {
            printf("Enter Name\n");
            scanf("%c", &temp);//temp statment to clear buffer,temp will store sapce taken after pressing number 1
            scanf("%[^\n]", Name[i]);//beacuse i hit enter when i choosed number 1 the compiler stored enter or null into string first and that why i use temp to avoid the problem
            printf("Enter Mobile Number");
            scanf("%s", &Mobile[i]);
            fprintf(ph, "%s %s\n", Name[i],Mobile[i]);
    }
    printf("%s %s\n", Name[0],Mobile[0]);
    printf("%s %s\n", Name[1], Mobile[1]);
    printf("%s %s\n", Name[2], Mobile[2]);
    fclose(ph);
    MainMenu();
}
void SearchPhoneBook()
{
    FILE *ph;
    ph = fopen("C:/Users/ec/Desktop/PhoneBook.txt", "r");
    printf("Enter the name you want to search for\n");
    char Name[3][50];
        double Mobile[11];
        char temp;
        scanf("%c", &temp);
        char Search[20];
        scanf("%s", Search);
        for (int i = 0; i < 3; i++)
        {
            fgets(Name[i], 25, ph);
        }
        printf("%s", Name);
    MainMenu();
}
void DeletePhoneNumber()
{
    printf("Works");
    MainMenu();
}
void DispalyPhoneBook()
{
    printf("Works");
    MainMenu();
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • If you are using `fgets` to input the name to search, be aware that it retains the newline, unlike your use of `scanf` to obtain the entry. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). You are also mixing the use of `scanf` with `fgets`. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – Weather Vane May 24 '20 at 19:47
  • okay so lets say the user enter "andy bob mike 2343453456"how do i separate them in search function to only get"andy bob mike" then if its what user wants it will print "andy bob mike 2343453456",so do i use fgets() or sacnf()@ Weather Vane – Crossplayer May 24 '20 at 19:53
  • You wrote `the only problem i face here i want an array for names and one for mobile numbers ` then `the problem that i face is that fgets()`. I do not understand what is your problem ? – Hitokiri May 24 '20 at 20:02
  • when i use fgets to get names stored in phonebook it take mobile numbers with them so how do i separate them from that "andy bob mike 2343453456" to that "andy bob mike" – Crossplayer May 24 '20 at 20:05

1 Answers1

0

when i use fgets to get names stored in phonebook it take mobile numbers with them so how do i separate them from that "andy bob mike 2343453456" to that "andy bob mike"

Because the context of string is: <name> <phone number>, so you can copy the string until last space character. You can create another function to get the name of user from the whole string as below

char * get_name(char * str) {
    char *name = malloc(strlen(str)+1);
    if (!name) {return NULL;}
    int i;
    for(i = strlen(str); (str[i] != ' ') && (i >= 0); i--);
    strncpy(name, str, i+1);
    name[i] = '\0';
    return name;
}

The test:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * get_name(char * str) {
    char *name = malloc(strlen(str)+1);
    int i;
    for(i = strlen(str); (str[i] != ' ') && (i >= 0); i--);
    strncpy(name, str, i+1);
    name[i] = '\0';
    return name;
}

int main() {
    char original_name[] = "andy bob mike 2343453456";
    char *name = get_name(original_name);
    printf("name: %s\n",name);
    free(name);
    return 0;
}

Other things, as the comment of @WeatherVane, you have to deal with using fgets after scanf because scanf() leaves the newline char in the buffer.

When i compiler your code, i see the warning:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[11]’ [-Wformat=]
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]

At two lines in your code:

scanf("%s", &Mobile[i]); // in  AddPhoneNumber() function
printf("%s", Name); // in void SearchPhoneBook() function

Should change to:

scanf("%10s", Mobile[i]); // remove '&' and add '10' to avoid overflow

for (int i = 0; i < 3; i++)
{
    fgets(Name[i], 25, ph);
    printf("%s", Name[i]); // use name[i] instead of name
}

Hitokiri
  • 3,607
  • 1
  • 9
  • 29