Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    [C] controllo variabile

    Se dichiaro una varibile "a" come int, quando faccio scanf e inserisco un carattere il programma incomincia ad impazzire...
    Come faccio a controllare che l'utente metti solamente un int e nn un altro tipo di variabile?
    Spero di essere stato chiaro!

    Buone feste
    By SirSeymour !!!

  2. #2
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    La scanf ritorna il numero di elementi letti con successo; in particolare, se leggi una sola varibile puoi fare:

    int read = scanf("%d", &var);
    if(read == 0){
    // significa che non è stata letta correttamente la variabile
    }

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  3. #3

    grazie ma...

    grazie.
    Ma ho provato ripetutamente, ma nn riesco a far in modo che l'utente riprovi a mettere la variabile.
    Per esempio io per controllare che il numero inserito sia > di 0 inserisco questo ciclo, ma con int read... nn riesce!

    codice:
    while (res<0 || res==0) {
      printf("________|il numero deve essere maggiore di 0|_________\n");
            printf("Inserisci un numero (considera solo le prima 5 cifre!) : ");
            scanf("%5d",&res);
    }
    By SirSeymour !!!

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2003
    Messaggi
    423
    Fai così:
    codice:
    int ok;
    int res;
    do
    {
        ok = scanf("%5d", &res);
    }while (ok==0)

  5. #5

    nn funziona

    nn funziona.
    Ho messo anche il ; alla fine del while,nn mi fa ripetere l'inserimento...va a capo e esce il cursore che lampeggia fermo..!
    Come mai
    ?
    By SirSeymour !!!

  6. #6
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Se vuoi controllare sia che la scanf abbia effettuato l'assegnamento, sia che la variabile letta sia positiva, un modo di precedere è il seguente (nota l'sitruzione fflush(stdin) che serve per svuotare il buffer di input, che altrimenti può dare problemi quando si leggono caratteri)

    codice:
    	int res = 0;
    	int ok;
    	do{
    		ok = 0;
    		fflush(stdin);
    		printf("Inserisci il numero: ");
    		ok = scanf("%d", &res);
    		if(ok == 0)
    			continue;
    		if(res <= 0)
    			printf("\nIl numero deve essere positivo!: ");
    	}
    	while(res <= 0);

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  7. #7

    grazie mille

    GRAZIE MILLE
    By SirSeymour !!!

  8. #8

    mi spieghi una cosa?

    codice:
    fflush(stdin);
    cosa è? a cosa serve precisamente?c'è una guida su internet che spiega meglio il funzionamento di questa funzione?

    grazie
    By SirSeymour !!!

  9. #9
    Utente di HTML.it L'avatar di Ilmalcom
    Registrato dal
    Oct 2002
    Messaggi
    1,345

    Re: mi spieghi una cosa?

    Originariamente inviato da SirSeymour
    codice:
    fflush(stdin);
    cosa è? a cosa serve precisamente?c'è una guida su internet che spiega meglio il funzionamento di questa funzione?

    grazie
    Tratto dalla man page di fflush
    codice:
    NAME
           fflush - flush a stream
    
    SYNOPSIS
           #include <stdio.h>
    
           int fflush(FILE *stream);
    
    DESCRIPTION
           The  function fflush forces a write of all user-space buffered data for
           the given output or update stream via  the  stream's  underlying  write
           function.  The open status of the stream is unaffected.
    
           If the stream argument is NULL, fflush flushes all open output streams.
    
           For a non-locking counterpart, see unlocked_stdio(3).
    
    RETURN VALUE
           Upon successful completion 0 is returned.  Otherwise, EOF  is  returned
           and the global variable errno is set to indicate the error.
    
    ERRORS
           EBADF  Stream is not an open stream, or is not open for writing.
    
           The  function  fflush may also fail and set errno for any of the errors
           specified for the routine write(2).
    
    NOTES
           Note that fflush() only flushes the user space buffers provided by  the
           C  library.   To  ensure that the data is physically stored on disk the
           kernel buffers must be flushed too, e.g. with sync(2) or fsync(2).
    
    CONFORMING TO
           The function fflush() conforms to ANSI X3.159-1989 (``ANSI C'').
    
    SEE ALSO
           fsync(2),   sync(2),   write(2),   fclose(3),   fopen(3),    setbuf(3),
           unlocked_stdio(3)

  10. #10
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Da:

    http://www.cplusplus.com/ref/

    Flush a stream.
    If the given stream has been opened for writing operations the output buffer is phisically written to the file.
    If the stream was open for reading operations the content of the input buffer is cleared.
    The stream remains open after this call.

    In pratica, se lo stream è di input, serve a cancellare l'eventuale input inserito dall'utente e non ancora letto dal programma; se lo stream e di output, il contenuto del buffer, scritto dal programma e non acora fisicamente scritto (ad esempio in un file a cui e collegato lo stream) viene scritto.

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.