home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / esempi / Stringhe / stringToInt-versione-2.c < prev    next >
C/C++ Source or Header  |  1999-03-11  |  453b  |  23 lines

  1. /* conversione da stringa ad intero positivo */
  2.  
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7.   char c = '0';
  8.   int i = 0;
  9.  
  10.   printf("Inserire un intero: ");
  11.  
  12.   /* trova la prima cifra significativa */
  13.   while ( (c = getchar()) != EOF && (c < '0' || c > '9')) ;
  14.  
  15.   /* legge e converte il resto del numero */
  16.   if (c != EOF)
  17.     do
  18.       i = 10 * i + c - '0';
  19.     while ( (c = getchar()) != EOF && c >= '0' && c <= '9');    
  20.   
  21.   printf("\nNumero letto: %d\n",i);
  22. }
  23.