0

I´m trying to create a structure populated by the user and accessed through a pointer.

As it stands right now I don´t get compiler errors but it won't take input correctly, I need to input the second variable twice, and the output is garbage.

I´m trying to get pointers and structures down before moving on to linked lists, any help would be appreciated.

//struct date

struct data {    
    int dia;
    int mes;
    int ano;
};

//struct client

struct cliente {        
    char nome[15];
    int num_conta;
    float saldo;
    struct data dia_d_mes;          
};


// function that returns pointer to struct populated by user

struct cliente *input_cliente()
{    
    struct cliente *tipo_cliente, n;        
    SYSTEMTIME st;
    GetSystemTime (&st);

    tipo_cliente = &n;

    printf ("Nome cliente:");   
    gets (tipo_cliente->nome);
    //fflush (stdin);

    printf ("Numero da conta:");
    scanf ("%d ", &tipo_cliente->num_conta);

    printf ("Saldo da conta:");
    scanf ("%f ", &tipo_cliente->saldo);

    tipo_cliente->dia_d_mes.dia = st.wDay;
    tipo_cliente->dia_d_mes.mes = st.wMonth;
    tipo_cliente->dia_d_mes.ano = st.wYear;

    return tipo_cliente;            // return pointer    
}

//print client

void print_cliente(struct cliente *tipo_cliente)
{    
    printf  ("%s", tipo_cliente->nome);
    printf  ("\t%d", tipo_cliente ->num_conta);
    printf  ("\t%.2f", tipo_cliente ->saldo);
    printf  ("\t%d/%d/%d\n", tipo_cliente->dia_d_mes.dia, tipo_cliente->dia_d_mes.mes, tipo_cliente->dia_d_mes.ano);    
}


int main()
{    
    struct cliente *novo;       //declare a new struct pointer
    system ("color 17");
    system ("mode 70,10");

    novo = input_cliente();     //create new client

    system ("cls");

    printf ("Nome \t #conta \t Saldo \tData\n");
    printf ("============================================\n");

    print_cliente (novo);       //print new client    
}

I´ve been playing around with the code and changed the pointer to a normal structure input but keep having one constant problem.
When the second printf is desplayed and the int is entered, it doesn´t move to the next printf the cursor moves to a new line in the command prompt. Any idea would be apreciated, I´ve tryed different things with the pointer and without, but I´m running out of ideas.

// function that returns pointer to struct populated by user

struct cliente input_cliente() { struct cliente tipo_cliente; // initialize struct SYSTEMTIME st; GetSystemTime (&st);

printf ("Nome cliente:");
gets (tipo_cliente.nome);                //accepts this value

printf ("Numero da conta:");
scanf ("%d ", &tipo_cliente.num_conta);  //also accepts this value 
                                           //after pressing enter goes to a empty line
printf ("Saldo da conta:");
scanf ("%f ", &tipo_cliente.saldo);  //the value stored in this variable is the 
                                       // value entered in the previous empty line
tipo_cliente.dia_d_mes.dia = st.wDay;
tipo_cliente.dia_d_mes.mes = st.wMonth;
tipo_cliente.dia_d_mes.ano = st.wYear;

return tipo_cliente;            // return pointer

}

1
  • First problem: gets. Never, ever, ever use gets. Commented Jun 16, 2014 at 18:36

2 Answers 2

1

input_cliente returns a pointer to a variable declared within the function. However, once the function ends, the contents of that variable become undefined. You should either return an actual struct cliente (not a pointer) or use malloc to allocate memory for a struct cliente* that will last beyond the function's execution.

Sign up to request clarification or add additional context in comments.

2 Comments

By returning the actual struct cliente I would be using passing by value which i have tryed and works. But i´m trying to lear pointers and structures before moving to files and linked lists. By using malloc it works and generates output correctley, thank you. But when I input the second variable and press enter it doesn´t ask for the third variable !!!
tipo_cliente =(struct cliente *) malloc (sizeof (struct cliente));
1

Here n is the local variable of the function input_cliente. So the scope of n is limited to the function body. It will be invalid after the function returns.

So you should either allocate it on the free store using malloc:

struct cliente* tipo_cliente = (struct cliente*) malloc(sizeof(struct cliente));

Or let the function have an out parameter:

struct cliente* input_cliente(struct cliente* tipo_cliente)
{
     // fill tipo_cliente here.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.