1
void menu() {
    int op;
    printf("\n Choose a option from below : \n\n");
    printf(" 1: Add Contact\n"
           " 2: View A Contact\n"
           " 3: View All Contacts\n"
           " 4: View All Contacts With a Common First Character\n"
           " 5: Delete All Contact\n"
           " 6: Delete A Contact\n"
           " 7: Replace A Contact Name\n"
           " 8: Replace A Contact Number\n"
           " 9: Refresh\n"
           " 10: Exit\n\n");

    printf(" Which one ? ");
    fgets(op, 5, stdin);

    switch ((int)op) {
    case 1: addrecords(); break;
    case 2: viewone(); break;
    case 3: viewall(); break;
    case 4: viewonechar(); break;
    case 5: deleteall(); break;
    case 6: deleteone(); break;
    case 7: replaceone(); break;
    case 8: replaceonenumber(); break;
    case 9: refresh(); break;
    case 10: exit(0); break;

    default:
        printf ("\n Wrong Option.\n\n");
        menu();
        break;
    }
}

void addrecords() {
    char name[50];
    char number[20];

    printf("\n\n Enter Contact Number (+880) : "); //Skips this
    fgets(number, 20, stdin);

    check(number);

    printf(" Enter Contact Name : "); //Comes in here
    fgets(name, 50, stdin);

    fp = fopen("Phonebook.txt","a");

    fprintf(fp, "%s %s\n", name, number);

    fclose(fp);

    printf("\n Contact Successfully Saved!\n Returning To Main Menu...\n\n");

    menu();
}

void check(char n[20]) {
    char name[25];
    char ncheck[20];

    fp = fopen("Phonebook.txt", "r");
    fscanf(fp, "%s %s", name, ncheck);

    while (!feof(fp)) {
        if ((strcmp(ncheck, n)) == 0) {
            printf ("\n Contact Already Exists.\n\n");
            fclose(fp);
            menu();
        } else {
            fscanf (fp, "%s %s", name, ncheck);
        }
    }
}

Okay I edited my program. After I enter 1, the program says wrong option. But I am entering the write one. Though I am doing it right, why is the program showing wrong option? Is it something with fgets? What's the problem now?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Anik Shahriar
  • 151
  • 1
  • 11
  • Isn't there any way to make scanf not leave any newlines? – Anik Shahriar Dec 06 '16 at 11:55
  • My advice: don't use `scanf` but replace it by a function you need to write such as `int GetInteger()`. Replace `scanf ("%d",&op);` by `op = GetInteger();`. The `GetInteger()` function is pretty trivial to write, ~3-4 lines of code, using [`fgets`](http://www.cplusplus.com/reference/cstdio/fgets/) and [`atoi`](http://www.cplusplus.com/reference/cstdlib/atoi/). – Jabberwocky Dec 06 '16 at 12:01
  • Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh Dec 06 '16 at 12:02
  • BTW: please check if `fopen` returns `NULL`, and if yes don't proceed further but display an error message inbstead and quit. – Jabberwocky Dec 06 '16 at 12:03
  • @SouravGhosh. That wasn't my problem. My problem was it fgets and scanf – Anik Shahriar Dec 06 '16 at 12:06
  • Okay I edited my program. After I enter 1, the program says wrong option. But i am entering the write one. What's the problem now? – Anik Shahriar Dec 06 '16 at 12:07
  • How can I do this? Please post an answer – Anik Shahriar Dec 06 '16 at 12:22
  • Okay thanks @user3121023. But what will happen when I add another option at '10'. Then how should I do that? – Anik Shahriar Dec 06 '16 at 12:30

1 Answers1

2

Well, trying to change your program the least I can and not pointing other errors or lack of error checks... and trying to keep it as simple as possible for you to understand what's happening, what you'd need to do is replace:

switch ((int)op) {

with something like

switch (atoi(op)) {

(or create your own get_int() combining fgets() and strtol() or sscanf())

chqrlie
  • 131,814
  • 10
  • 121
  • 189
yLaguardia
  • 585
  • 3
  • 7