Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - errore nel codice
Forum - C/C++ - errore nel codice

Avatar
cami (Normal User)
Newbie


Messaggi: 3
Iscritto: 19/08/2011

Segnala al moderatore
Postato alle 0:04
Giovedì, 25/08/2011
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 il 25/08/2011 alle 0:12
PM
Avatar
Peppe91 (Member)
Rookie


Messaggi: 41
Iscritto: 09/04/2011

Up
1
Down
V
Segnala al moderatore
Postato alle 16:10
Domenica, 28/08/2011
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 :)

Codice sorgente - presumibilmente C

  1. #include <stdio.h>
  2.  
  3.  
  4. /* maximum lenght of arrays */
  5.  
  6. #define MAXLEN 30                      
  7.  
  8. struct info {                         /* set up info template */
  9.    
  10.     int number;            
  11.     int zipcode;            
  12.     double balance;          
  13.     double limit;          
  14.     char name[MAXLEN];                /* define an array of chars called name with max lenght 30 */
  15.     char street[MAXLEN];             /* define an array of chars called street with max lenght 30 */
  16.     char citystate[MAXLEN];          /* define an array of chars called citystate with max lenght 30 */
  17.    
  18. };                                    /* end of structure template */
  19.  
  20.  
  21. int main (void)
  22. {
  23.     struct info personal;   /* declare personal as an info variable */
  24.    
  25.     printf("Enter the account number:\n");
  26.     scanf("%d",&personal.number);          /* access to the number portion */
  27.     fflush(stdin);
  28.    
  29.     printf("Enter your street address:\n");
  30.     while (getchar() != '\n');    /* flush stdin buffer */
  31.     gets(personal.street);       /* access to the street portion */
  32.    
  33.     printf("Enter your zip code:\n");
  34.     scanf("%d",&personal.zipcode);              /* access to the zipcode portion */
  35.     fflush(stdin);
  36.    
  37.     printf("Enter city/state:\n");
  38.     while (getchar() != '\n');    /* flush stdin buffer */
  39.     gets(personal.citystate);    /* access to the citystate portion */
  40.    
  41.     printf("Enter the account balance:\n");
  42.     scanf("%lf",&personal.balance);              /* access to the balance portion */
  43.     fflush(stdin);
  44.    
  45.     printf("Enter the credit limit:\n");
  46.     scanf("%lf",&personal.limit);                /* access to the limit portion */
  47.     fflush(stdin);
  48.    
  49.     printf("Enter the account name:\n");
  50.     while (getchar() != '\n');            /* flush stdin buffer */
  51.     gets(personal.name);                 /* access to the name portion */
  52.    
  53.     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);
  54.    
  55.    
  56.     return 0;
  57. }


Ultima modifica effettuata da Peppe91 il 28/08/2011 alle 16:12
PM