0

I am a newbie to C, i am trying to do similar coding to this. but for some reason gets, asking for name for new record keep getting skipped.

/* Define libraries to be included */
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>

/* Define Structures*/
typedef struct contact {
    int number;        /*unique account number*/
    char name[20];     /*contains name*/
    char phone[15];    /*contains phone number*/
    char email[20];           /*contains email address*/
    struct contact *next; /*next is used to navigate through structures.*/
    int count;     /*count is used to input comments into array*/
} Contact;
void addNewContact(void) /* add new contact function*/
{
    newRecord = (struct contact*)malloc(sizeof(struct contact));
    if (firstRecord == NULL) {
        firstRecord = currentRecord = newRecord;
    }
    else {
        currentRecord = firstRecord;     
        while (currentRecord->next != NULL)currentRecord = currentRecord->next;
        currentRecord->next = newRecord; 
        currentRecord = newRecord;        
    }
    currentRecordNumber++;
    printf("%27s: %5i\n", "contact number", currentRecordNumber);
    currentRecord->number = currentRecordNumber;   
    fflush(stdin);
    printf("Enter contact name");
    gets(currentRecord->name);/*this got skipped(no input asked)*/
    fflush(stdin);
    printf("Enter contact Phone number");
    gets(currentRecord->phone);
    fflush(stdin);
    printf("Enter contact email");
    gets(currentRecord->email);
    fflush(stdin);
    printf("contact added!");
    currentRecord->count = 0;
    currentRecord->next = NULL;
}
A W
  • 1
  • 1
  • 3
    Please create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). The code is way too long. – Rohan Bari Feb 17 '21 at 12:28
  • 2
    Too much code as mentioned, so it will be hard for anyone to easily spot the exact bug(s) on your behalf. Some advice: [`malloc.h` is deprecated](https://stackoverflow.com/questions/12973311/difference-between-stdlib-h-and-malloc-h), so don't use it. **Please, never use the [very dangerous and obsolete `gets` function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used).** Also, [`fflush(stdin)` results in undefined behavior](https://stackoverflow.com/questions/2979209/using-fflushstdin). – costaparas Feb 17 '21 at 12:33
  • Make sure you have everything initialized, check the return value of `scanf`, check the return value of `malloc`, compile your code with some essential compilation flags, such as `-Wall -Werror -Wextra -O2 -g` if using `gcc`/`clang` and resolve all errors reported. Consider using [`valgrind`](https://valgrind.org/) and [`gdb`](https://www.gnu.org/software/gdb/) to debug your code. – costaparas Feb 17 '21 at 12:33
  • Your code is too long for me to read just now, but if you are using `scanf` to read some inputs, and `fgets` (or `gets`) to read others, it will not work. `scanf` tends to leave the `\n` on the input buffer, and the next time you try to use `fgets` (or `gets`) to read a line, you get that leftover `\n`, and it looks like a blank line. – Steve Summit Feb 17 '21 at 12:36
  • Where did you learn to use `gets()`? – klutt Feb 17 '21 at 12:37
  • At a glance, your code is probably mostly fine, except that the way you're taking input is likely the root cause of any issues you have. Fix the above & if the issue(s) persist (or new ones emerge), use GDB, or ask here with a [mre] (i.e. not 350 lines). – costaparas Feb 17 '21 at 12:38
  • Don't use `fflush(stdin)`; it is undefined behavior per the Standard (though it is defined on Linux). Flushing output buffers makes sense, but flushing input buffers does not; there are other (better, portable) ways to clear the input stream. – ad absurdum Feb 17 '21 at 12:50
  • You need a new source for learning C. There's a lot of very bad practice in this code, really old, bad stuff that went obsolete some 25 years ago. Someone or something is teaching you things that everyone knew where bad back in 90s. – Lundin Feb 17 '21 at 12:54
  • Thank you so much for the answers, i am trying to create a database for contacts in C, to save into a text file. But this is the only code i could find that serve the same use. So i am trying to dissect it to understand the logic of it. – A W Feb 17 '21 at 13:06
  • Read [here](https://stackoverflow.com/questions/2979209/using-fflushstdin) and [here](https://stackoverflow.com/questions/34219549/how-to-properly-flush-stdin-in-fgets-loop) about how/whether you need to flush input. Read [here](https://stackoverflow.com/questions/58403537/what-can-i-use-to-parse-input-instead-of-scanf) about alternatives to `scanf`. – Steve Summit Feb 17 '21 at 13:10
  • @AW Where did you get the idea to use `gets()`? – klutt Feb 17 '21 at 13:11
  • @AW If you want to stick with `scanf`, my suggestions to you are: (1) use `scanf` to read *every* input. (2) Read one thing at a time. (3) Always check `scanf`'s return value: if it doesn't return 1, it didn't read that one thing. (4) If you want to read one character, use `" %c"` (with a leading space). (5) Your program won't be able to read strings containing spaces. Just plan to live with this limitation for now. (6) You program probably won't be able to deal gracefully when the user types the wrong kind of input (like, letters instead of a number). Live with this limitation for now, too. – Steve Summit Feb 17 '21 at 13:17
  • @SteveSummit Thank you for the guidance i will try to get other input method. – A W Feb 18 '21 at 07:33

1 Answers1

0

fflush(stdin);/clears any text from the input stream/

you shouldn't use fflush-function with stdin-stream. It's undefinied behaviour.

Use instead something like that:

  while (getchar() != '\n') {
      // empty buffer
   }
  • Flushing the input stream is not only the problem in the OP's code. – Rohan Bari Feb 17 '21 at 12:55
  • of course not, but he is constantly calling this function. –  Feb 17 '21 at 12:57
  • 1
    This is a comment, more than an answer. – Rohan Bari Feb 17 '21 at 13:00
  • 1
    The point is: The problem is not flushing input using `fflush(stdin)`. The problem is trying to flush input at all. If you find yourself needing to flush input in a program like this, it's invariably because of poor usage of `scanf`, perhaps because it's being mixed with line-reading functions like `fgets` (or `gets`). Fix the `scanf` usage, and there won't be any need to flush input at all. – Steve Summit Feb 17 '21 at 13:07
  • 1
    @SteveSummit And a good way to fix `scanf` usage is to use `fgets` and `sscanf` instead – klutt Feb 17 '21 at 13:10
  • @klutt I completely agree. The problem is that there is an *immense* mass of poorly-written textbooks, poorly-trained instructors, and hapless former students of poorly-trained instructors out there, all perpetuating the myth that `scanf` is how you read user input in C. The inertia is just about unstoppable. Beginning programmers are going to keep struggling with `scanf`. Another approach to helping them is as in [this comment](https://stackoverflow.com/questions/66241947/c-code-not-working-as-intendedskipped-gets#comment117113605_66241947). – Steve Summit Feb 17 '21 at 13:29
  • @SteveSummit I also think that one of the problems are teachers who are using C as a way to teach programming in general, when there are languages that are so much better for that purpose. And when you are about to learn C, the best thing would be to minimize user input as much as possible. – klutt Feb 17 '21 at 13:34