Oppure

Loading
24/08/11 22:04
cami
Sto scrivendo un programma che chiede di popolare una struttura e mostra le informazioni inserite. C'e' un errore nel printf(), sembrano sbagliati tutti gli specificatori delle variabili definite come int e non riesco a capire come mai. Allego il codice qui sotto, grazie!

[#include <stdio.h>


/* maximum lenght of arrays */
#define MAXNUM 20
#define MAXLEN 30
#define MAXZIP 10
#define MAXBAL 20

struct info { /* set up info template */

int number [MAXNUM]; /* define an array of ints called number with max lenght 20 */
int zipcode [MAXZIP]; /* define an array of ints called zipcode with max lenght 10 */
double balance[MAXBAL]; /* define an array of doubles called balance with max lenght 20 */
double limit[MAXBAL]; /* define an array of doubles called limit with max lenght 20 */
char name [MAXLEN]; /* define an array of chars called name with max lenght 30 */
char street [MAXLEN]; /* define an array of chars called street with max lenght 30 */
char citystate [MAXLEN]; /* define an array of chars called citystate with max lenght 30 */

}; /* end of structure template */


int main (void)
{
struct info personal; /* declare personal as an info variable */

printf("Enter the account number:\n";);
scanf("%d",&personal.number); /* access to the number portion */

printf("Enter your street address:\n";);
while (getchar() != '\n'); /* flush stdin buffer */
gets(personal.street); /* access to the street portion */

printf("Enter your zip code:\n";);
scanf("%d",&personal.zipcode); /* access to the zipcode portion */

printf("Enter city/state:\n";);
while (getchar() != '\n'); /* flush stdin buffer */
gets(personal.citystate); /* access to the citystate portion */

printf("Enter the account balance:\n";);
scanf("%d",&personal.balance); /* access to the balance portion */

printf("Enter the credit limit:\n";);
scanf("%f",&personal.limit); /* access to the limit portion */

printf("Enter the account name:\n";);
while (getchar() != '\n'); /* flush stdin buffer */
gets(personal.name); /* access to the name portion */

printf("Account number: %d \n Address: %s,%d,%s\n Account balance: %d\n Account limit: %d\n Account name: %s\n", personal.number, personal.street, personal.zipcode, personal.citystate, personal.balance, personal.limit, personal.name);


return 0;
}
]

Ultima modifica effettuata da cami 24/08/11 22:12
aaa
28/08/11 14:10
Peppe91
Per i double devi usare %lf come codice di formato. E poi scusa, a che serve dichiarare un array di interi quando devi inserire un semplice numero? Ad esempio nel caso di "int number", perché l'hai dichiarato come array? Tu stai semplicemente assegnando un numero d'account che l'utente inserisce, ad una variabile di tipo int. Non vedo l'utilità dell'array. Comunque modificandolo come vedi qui sotto, funziona perfettamente :)

#include <stdio.h> 


/* maximum lenght of arrays */ 

#define MAXLEN 30                      

struct info {                         /* set up info template */ 
    
    int number;             
    int zipcode;             
    double balance;          
    double limit;          
    char name[MAXLEN];                /* define an array of chars called name with max lenght 30 */ 
    char street[MAXLEN];             /* define an array of chars called street with max lenght 30 */ 
    char citystate[MAXLEN];          /* define an array of chars called citystate with max lenght 30 */ 
    
};                                    /* end of structure template */ 


int main (void) 
{ 
    struct info personal;   /* declare personal as an info variable */ 
    
    printf("Enter the account number:\n"); 
    scanf("%d",&personal.number);          /* access to the number portion */ 
    fflush(stdin);
    
    printf("Enter your street address:\n"); 
    while (getchar() != '\n');    /* flush stdin buffer */ 
    gets(personal.street);       /* access to the street portion */ 
    
    printf("Enter your zip code:\n"); 
    scanf("%d",&personal.zipcode);              /* access to the zipcode portion */ 
    fflush(stdin);
    
    printf("Enter city/state:\n"); 
    while (getchar() != '\n');    /* flush stdin buffer */ 
    gets(personal.citystate);    /* access to the citystate portion */ 
    
    printf("Enter the account balance:\n"); 
    scanf("%lf",&personal.balance);              /* access to the balance portion */ 
    fflush(stdin);
    
    printf("Enter the credit limit:\n"); 
    scanf("%lf",&personal.limit);                /* access to the limit portion */ 
    fflush(stdin);
    
    printf("Enter the account name:\n"); 
    while (getchar() != '\n');            /* flush stdin buffer */ 
    gets(personal.name);                 /* access to the name portion */ 
    
    printf("Account number: %d \n Address: %s,%d,%s\n Account balance: %lf\n Account limit: %lf\n Account name: %s\n", personal.number, personal.street, personal.zipcode, personal.citystate, personal.balance, personal.limit, personal.name);
    
    
    return 0; 
} 
Ultima modifica effettuata da Peppe91 28/08/11 14:12
aaa