Thread: Help can't put this to work

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    Help can't put this to work

    Hello, my name is João, i'm from Portugal so forgive me in advance for any writing errors.

    I have a piece of C code that I can't put it to work correctly and I was hoping you can help me on this.

    The objective of this program is to have 2 generic function, one for reading integers and another for reading strings, both to be applied on many other functions each one with a different question.

    Code:
    #include <stdio.h>
    
    int ler_int (){
        int num;
    
        scanf(" %d", &num);
        
        return num;
        
    
    }
    
    char* ler_char (char *frase, int max){
    
        fgets(frase, max, stdin);
        return frase;
    
    }
    
    int le_nss (){
        printf ("Qual o seu numero de segurança social?:  ");
        
        return ler_int();
    
    
    }
    
    char *le_nome(char *nome){
        printf ("qual é o seu nome?: ");
        int max = 100;
    
        return ler_char(nome, max);
    }
    
    main(){
        char id_nome[100];
        int NSS = le_nss();
    
        le_nome(id_nome);
    
        printf("o seu numero de NSS é: %d\n", NSS);
        printf("o seu nome é: %s\n", id_nome);
    }
    I'm learning about pointers and it's a little bit confusing for me the use of them so probably the problem lies in the incorrect use of pointers but I can't seem to figure it out.

    Thanks for the help

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    When you read the integer in ler_int, the Enter key leaves a newline character ('\n') in the input buffer. The function fgets reads up to -- and including -- a newline character, so it gets that newline and returns a string with just that newline character. The quickest way to fix this is to add a call to getchar() after you read the integer.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    When you read the integer in ler_int, the Enter key leaves a newline character ('\n') in the input buffer. The function fgets reads up to -- and including -- a newline character, so it gets that newline and returns a string with just that newline character. The quickest way to fix this is to add a call to getchar() after you read the integer.
    Still not working

    I've tried already insert "fflush(stdio); " before the fgets, still the same result.

    This is the output:

    --- Qual o seu numero de segurança social?: 3456
    o seu numero de NSS é: 3456
    qual é o seu nome?: O seu nome é:



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    Quote Originally Posted by jonymcarrony
    I've tried already insert "fflush(stdio); " before the fgets, still the same result.
    fflush(stdio) would likely result in a syntax error. Perhaps you meant fflush(stdin), but that results in undefined behaviour.

    In fact, a suggestion was made in the previous post that you seem to have ignored:
    Quote Originally Posted by rags_to_riches
    The quickest way to fix this is to add a call to getchar() after you read the integer.
    Did you also ignore the explanation and just typed "Still not working"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this work?
    By Syscal in forum C Programming
    Replies: 4
    Last Post: 10-09-2013, 03:46 AM
  2. OS: How does it work?
    By GReaper in forum Tech Board
    Replies: 15
    Last Post: 08-27-2010, 01:41 PM
  3. At Work...Need C++....
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-04-2004, 12:36 AM
  4. Getting nfs to work...
    By deathstryke in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-03-2004, 03:16 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM