se ho
do{
}while(scanf("%d",&a[i]));
la while vedo che è TRUE se inserisco un numero naturale, se invece inserisco un * è FALSE...ma quando rende TRUE di preciso?
Grazie.
se ho
do{
}while(scanf("%d",&a[i]));
la while vedo che è TRUE se inserisco un numero naturale, se invece inserisco un * è FALSE...ma quando rende TRUE di preciso?
Grazie.
GdR Online - http://lenar.it/
Yesterday is history. Tomorrow is mystery.
Today is a gift. That's why it's called the present
A giudicare dal prototipo della funzione, la risposta è mai !
Header File
stdio.h
Category
Input/output Routines
Syntax
#include <stdio.h>
int scanf(const char *format[, address, ...]);
int wscanf(const wchar_t *format[, address, ...]);
Description
Scans and formats input from the stdin stream.
Note: For Win32s or Win32 GUI applications, stdin must be redirected.
The scanf function:
scans a series of input fields one character at a time
formats each field according to a corresponding format specifier passed in the format string *format.
vsscanf scans and formats input from a string, using an argument list
There must be one format specifier and address for each input field.
scanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely. For details about why this might happen, see When ...scanf Stops Scanning.
Warning: scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.
The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.
Return Value
On success, scanf returns the number of input fields successfully scanned, converted, and stored. The return value does not include scanned fields that were not stored.
On error:
if no fields were stored, scanf returns 0.
if scanf attempts to read at end-of-file or at end-of-string, it returns EOF.
01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
All errors are undocumented features waiting to be discovered.
Ritorna il numero di campi letti con successo.returns the number of input fields successfully scanned
Siccome ha come direttiva %d gli stai dicendo di leggere un intero. Quindi se inserisci un intero ha letto con successo un campo. scanf restituisce 1 che quindi è true.
Se metti un altro carattere che non sia un numero, il campo letto non è un intero e quindi scanf restituisce 0, cioè un false.
ciao ciao
perfetto:-) come posso uscire in modo "elegante"? Ovvero...l'utente può inserire quanti valori interi vuole...in questo modo smette se inserisce un nonnumero...potrei farlo uscire se da un invio a vuoto?
GdR Online - http://lenar.it/
Yesterday is history. Tomorrow is mystery.
Today is a gift. That's why it's called the present
codice:#include <stdio.h> #define MAX_A 10 int main() { int i=0, q, tmp, a[MAX_A]; printf("Inserisci max %d valori interi, (digita una lettera per terminare).\n", MAX_A); while( scanf("%d", &tmp) && i<MAX_A) a[i++]=tmp; for( q=0; q<i; q++) printf("%d,", a[q]); getchar(); return 0; }
01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
All errors are undocumented features waiting to be discovered.
#define MAX_A 10
perché si mette?
GdR Online - http://lenar.it/
Yesterday is history. Tomorrow is mystery.
Today is a gift. That's why it's called the present
Definisce una costante globale che dichiara la dimensione massima del vettore.Originariamente inviato da Vre
#define MAX_A 10
perché si mette?
In questo modo modificando semplicemente quel valore, si modifica in un sol colpo
sia la dimensione del vettore, che il controllo nel while(), in pratica durante la
fase di compilazione tutte le occorrenze di MAX_A vengono sostituite con il
valore assegnatogli. Evitando 'sgradevoli' errori, a volte difficili da rilevare.
01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
All errors are undocumented features waiting to be discovered.
ma se io non ho un massimo definito? L'utente potrebbe inserire anche 10^10 valori...
GdR Online - http://lenar.it/
Yesterday is history. Tomorrow is mystery.
Today is a gift. That's why it's called the present
Se non lo scrivi, io come posso saperlo questo ?Originariamente inviato da Vre
ma se io non ho un massimo definito? L'utente potrebbe inserire anche 10^10 valori...
Utilizza allora una variabile normale.
01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
All errors are undocumented features waiting to be discovered.
okok, scusa e grazie!!!!!!
GdR Online - http://lenar.it/
Yesterday is history. Tomorrow is mystery.
Today is a gift. That's why it's called the present