我试图创建一个由用户填充并通过指针访问的结构。用户输入填充结构并通过指针访问
现在我不知道编译器错误,但它不会正确输入输入,我需要输入第二个变量两次,输出是垃圾。
我试图让指针和结构下移到链表之前,任何帮助将不胜感激。
//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
}
第一个问题:'gets'。 *从来没有,永远不会使用'gets'。* – jwodder