Jorge,
I think your problem is you are using '%i' in scanf() to read an =
integer - you should use '%d'.
Also, when you display the value listin[i].num, you are using '%s' for a =
string when you should be using '%d' again:
Heres the amended code:
#include <stdio.h>
#include <conio.h>
#define MaxEnt 5
struct agenda
{
char nombre[25];
char direcc[25];
int num;
char tlf[9];
} listin[MaxEnt];
main()
{
int i, n=3D2;
for(i=3D0;i<MaxEnt;i++)
{
printf("Introduzca Nombre:");
gets(listin[i].nombre);
printf("Introduzca direccion:");
gets(listin[i].direcc);
printf("Introduzca numero:");
scanf("%d",&listin[i].num); // use %d here
fflush(stdin); // flush stdin as =
scanf leaves a 'n' in the stream (i think)=20
// which will =
cause problems with the next gets() call.
printf("Introduzca telefono:");
gets(listin[i].tlf);
}
for(i=3D0;i<n;i++)
printf("%s %s %d =
%s",listin[i].nombre,listin[i].direcc,listin[i].num,listin[i].tlf); =
// use %d here
}
I hope this helps
Chris.
----- Original Message -----=20
From: Jorge Garcia-Donas K=FCbler=20
To: Dev...@li...=20
Sent: 25 November 2000 07:59
Subject: [Dev-C++] What's up with scanf and struct's
I'm a newbie. Does any body know what's wrong in the next simple =
program. It's my first time i define an struct, and the problem seems to =
be in the scanf instruction , because if i redefine "int num" to "char =
num[2]" and use gets() everything goes ok.
=20
#include <stdio.h>
#include <conio.h>
#define MaxEnt 5
struct agenda
{
char nombre[25];
char direcc[25];
int num;
char tlf[9];
} listin[MaxEnt];
main()
{
int i, n=3D2;
for(i=3D0;i<MaxEnt;i++)
{
printf("Introduzca Nombre:");
gets(listin[i].nombre);
printf("Introduzca direccion:");
gets(listin[i].direcc);
printf("Introduzca numero:");
scanf("%i",&listin[i].num);
printf("Introduzca telefono:");
gets(listin[i].tlf);
}
for(i=3D0;i<n;i++)
printf("%s %s %s =
%s",listin[i].nombre,listin[i].direcc,listin[i].num,listin[i].tlf);
}
|