Thread: Little question from newbie(

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    4

    Question Little question from newbie(

    Could someone help, im at very beggining of learning programming, but already faced issue, at very start)
    When i do some practice tasks for begginers i wrote a program like this :
    int main()
    {
    char name[50], town[50];
    int age;
    printf("Hi, what`s your name? ");
    gets(name);
    printf("\nWelcome to our show, %s\n", name);
    printf("\nHow old are you?");
    scanf("%d", &age);
    printf("\nHmm, you don`t look a day over 22\n");
    printf("\nTell me, %s, where do you live?\n", name);
    gets(town);
    printf("Oh, i`ve heard %s is a lovely place", town);






    return 0;

    ...but unfortunately, this program breaks at the point where second gets(town) appeared, and doesn't let me enter the name of town.. I couldn't figured out why? seems like everything ok for me...
    I will be really appreciate for help!

    here the simple of program from this handbook, but its works properly, it looks lke there no any differences between


    char customer[30], acctNum[30];
    double avgBalance, interest, service;
    int numTrans;
    printf("Name? ");
    gets(customer);
    printf("Account number? ");
    gets(acctNum);
    printf("Average balance? ");
    scanf("%lf", &avgBalance);
    printf("Number of transactions? ");
    scanf("%d", &numTrans);
    interest = avgBalance * 0.06; service = numTrans * 0.50;
    printf("\nName: %s\n", customer);
    printf("Average balance: $%3.2f\n", avgBalance);
    printf("Interest: $%3.2f\n", interest);
    printf("Service charge: $%3.2f\n", service)
    Last edited by Nirvanko; 08-13-2022 at 04:15 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The problem is that when you enter a line from the console it has a "newline" character at the end. The gets function reads an entire line, including the newline character. But the scanf function does not generally read the newline after its input. So your scanf call is leaving the newline behind and the next gets call encounters the newline character right away and thinks it's reading a blank line. To fix the problem you need to get rid of the newline after the scanf call.

    Another problem with your code is that the gets function is obsolete and no longer exists in modern C as it is considered dangerous, having been the cause of vulnerabilities in the past. You are supposed to use fgets instead, but unfortunately it is a little harder to use since it not only reads the newline from the input but also includes it in the string so you usually need to remove it.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    #define STR_SIZE  50
    #define YOUNG_AGE 22
     
    int main()
    {
        char name[STR_SIZE], town[STR_SIZE], *p;
        int age;
     
        printf("Hi, what`s your name? ");
     
        fgets(name, STR_SIZE, stdin);
        // Remove the newline character from the string.
        p = strchr(name, '\n');
        if (p) *p = '\0';
     
        printf("Welcome to our show, %s.\n", name);
     
        printf("How old are you? ");
     
        scanf("%d", &age);
        // Remove the newline character from the input buffer.
        while (getchar() != '\n') ;
     
        if (age > YOUNG_AGE)
            printf("Hmm, you don`t look a day over %d.\n", YOUNG_AGE);
     
        printf("Tell me, %s, where do you live? ", name);
     
        fgets(town, STR_SIZE, stdin);
        // Remove the newline character from the string.
        p = strchr(town, '\n');
        if (p) *p = '\0';
     
        printf("Oh, I`ve heard %s is a lovely place.\n", town);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    Another way to tackle the scanf problem is to add a space after "%d", so this
    Code:
        scanf("%d", &age);
    turns into this
    Code:
        scanf("%d ", &age);
    This way, you won't need the while loop below. Beware, however, that this will consume ALL whitespace without stopping at the first newline, so they're not entirely identical.

    BTW, I want to echo john's warning about gets: do not ever adopt the habit of using it (or "%s" without a number between the % and s in scanf)! The problem with it is that it does not accept a parameter to specify how much name and town fit in them (49 bytes plus the trailing null byte in this case), so it will happily attempt to keep writing "outside" the arrays to unrelated memory, something known as "buffer overrun". In the past, serious exploits had been made possible due to this, and running such a program as root was a spell of death. Nowadays, most modern OSes will just signal a segfault (or a compiler such as gcc will by default harden the executable to detect "stack smashing") and not allow anything particularly bad to happen, but it's still a bad idea to use it, especially since compilers are no longer obligated to even acknowledge its existence.
    Last edited by erikkonstas; 08-13-2022 at 05:38 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Maybe you should actually try that eric.
    I see what you're getting at, but it doesn't work in this situation.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    4
    Thx a lot for answers, really appreciate it. But i wonder why they gave such kind of tasks, even do not mention about possible problems might encountered, when you use gets function. Even if i re-writed this program with fgets function it wont work... in first solution, i didnt get it completely, im not familiar with such commands that he used to delete new line character yet(using loop, strchr, (*p) - defined like char etc). Second solution - doesnt help at all. Only helps when i use scanf("%s", town); instead of gets and fgets function to pass the name and town like char variable. Even Mike Dane from freeCodeCamp.org (not sure that you know him, but anyway) in his tutorials of C programming for beginners slightly mention about jeopardy of new line character due of using fgets function, but didbt tell how to avoid it....weird.((
    Last edited by Nirvanko; 08-13-2022 at 07:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  2. Newbie question
    By Gareth321 in forum C Programming
    Replies: 6
    Last Post: 03-21-2012, 09:09 PM
  3. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. newbie question
    By jgonzales in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 01:34 PM

Tags for this Thread