2014-06-16 47 views
0

我试图创建一个由用户填充并通过指针访问的结构。用户输入填充结构并通过指针访问

现在我不知道编译器错误,但它不会正确输入输入,我需要输入第二个变量两次,输出是垃圾。

我试图让指针和结构下移到链表之前,任何帮助将不胜感激。

//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  
} 

我一直在玩弄代码,并将指针改为正常的结构输入,但一直存在一个问题。
当第二个printf被删除并且输入int时,它不会移动到下一个printf,光标将移动到命令提示符中的新行。任何想法都会被认可,我已经用指针尝试了不同的东西,但是我没有想法。

返回指针

//功能结构由用户填充

结构cliente input_cliente() { 结构cliente tipo_cliente; //初始化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 

}

+0

第一个问题:'gets'。 *从来没有,永远不会使用'gets'。* – jwodder

回答

1

input_cliente指针返回到该函数内声明的变量。但是,一旦函数结束,该变量的内容就变得不确定。您应该返回一个实际的struct cliente(不是指针)或使用mallocstruct cliente*分配内存,该内存将超出该函数的执行范围。

+0

通过返回实际的结构客户端,我将使用传递值,我已经尝试和工作。但我想在移动到文件和链接列表之前学习指针和结构。通过使用malloc它可以工作并生成输出correctley,谢谢。但是当我输入第二个变量并按回车时,它不会要求第三个变量! –

+0

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

1

这里n局部变量的功能input_cliente。所以n的范围仅限于函数体。函数返回后将无效。

所以,你应该分配它使用malloc自由存储区:

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

或者让函数有一个输出参数:

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