struct problem

This program doesn't print the right number i put.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<stdlib.h>
typedef struct {
char targa[16];
char marca[16];
char modello[16];
int anno_imm;
} automobile;
int main(){
// Puntatore alla struttura automobile
automobile *a;
int i,n;
printf("Inserire i dati di quante automobili? ");
scanf ("%d",&n);
// Inizializzazione dinamica del vettore di automobili
a = (automobile*) malloc (n*sizeof(automobile));
for (i=0; i<n; i++) {
printf ("\nAutomobile n.%d\n",i+1);
printf ("Inserisci la targa: ");
scanf ("%s",a->targa);
printf ("Inserisci la marca: ");
scanf ("%s",a->marca);
printf ("Inserisci il modello: ");
scanf ("%s",a->modello);
printf ("Inserisci l'anno di immatricolazione: ");
scanf ("%d",&a->anno_imm);
}
for(i=0; i<n; i++){
	printf ("Automobile n.%d\n",i+1);
	printf("Targa: %d\n",a->targa);
	printf("Marca: %d\n",a->marca);
	printf("Modello: %d\n",a->modello);
	printf("Anno di immatricolazione: %d\n",a->anno_imm);
	getchar();
}
free(a);
}
One of the first things you should take care of would be to use a consistent indent style. This will make reading your program much easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<stdlib.h>

typedef struct
{
   char targa[16];
   char marca[16];
   char modello[16];
   int anno_imm;
} automobile;

int main()
{
// Puntatore alla struttura automobile
   automobile *a;
   int i,n;
   printf("Inserire i dati di quante automobili? ");
   scanf ("%d",&n);
// Inizializzazione dinamica del vettore di automobili
   a = (automobile*) malloc (n*sizeof(automobile));
   for (i=0; i<n; i++)
   {
      printf ("\nAutomobile n.%d\n",i+1);
      printf ("Inserisci la targa: ");
      scanf ("%s",a->targa);
      printf ("Inserisci la marca: ");
      scanf ("%s",a->marca);
      printf ("Inserisci il modello: ");
      scanf ("%s",a->modello);
      printf ("Inserisci l'anno di immatricolazione: ");
      scanf ("%d",&a->anno_imm);
   }
   for(i=0; i<n; i++)
   {
      printf ("Automobile n.%d\n",i+1);
      printf("Targa: %d\n",a->targa);
      printf("Marca: %d\n",a->marca);
      printf("Modello: %d\n",a->modello);
      printf("Anno di immatricolazione: %d\n",a->anno_imm);
      getchar();
   }
   free(a);
   return 0;
}


Next your compiler should be warning you about several issues. If it isn't you need to see about increasing your compiler warning level.

In function ‘main’:|
main.c|36|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
main.c|37|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
main.c|38|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|

These warnings are a good indication as to where to start looking for your problem. You're using the wrong format specifier for the type of variable you supplied. The printf() and scanf() series of functions are very insistant about the specifiers matching the variable types.

Topic archived. No new replies allowed.