Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249

    [C++] conversione da string a int

    come cavolo si fa ?
    es,ho una:
    string str = "123";
    int k;
    voglio fare in modo che k abbia il il valore 123.
    I got the remedy

  2. #2

    Re: [C++] conversione da string a int

    Originariamente inviato da albgen
    come cavolo si fa ?
    es,ho una:
    string str = "123";
    int k;
    voglio fare in modo che k abbia il il valore 123.
    codice:
      k=atoi(str);
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  3. #3
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    grazie...
    I got the remedy

  4. #4
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    visto che sono in tema,l'inverso come si fa ?
    ps:itoa(..) non mi funziona
    I got the remedy

  5. #5
    Originariamente inviato da albgen
    visto che sono in tema,l'inverso come si fa ?
    ps:itoa(..) non mi funziona
    Se correttamente implementata funziona :-)

    codice:
    	int number = 12345;
    	char string[25];
    	
    	itoa(number, string, 10);
    	printf("integer = %d string = %s\n", number, string);
    Nota dall' help del BCC32 :

    /**
    * Header File
    * stdlib.h
    *
    * Category Conversion Routines, Math Routines
    *
    * Syntax :
    *
    * #include <stdlib.h>
    * char *itoa(int value, char *string, int radix);
    * wchar_t *_itow(int value, wchar_t *string, int radix);
    *
    * Description :
    *
    * Converts an integer to a string.
    *
    * itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer. _itow is the unicode version of the function. It converts an integer to a wide-character string.
    *
    * radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).
    *
    * Note: The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 33 bytes.
    *
    * Return Value
    *
    * itoa returns a pointer to string.
    **/
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  6. #6
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    #include <stdio.h>
    #include <stdlib.h>
    libro.cpp:44: error: `itoa' undeclared (first use this function)

    uso g++ 3.3
    I got the remedy

  7. #7
    Originariamente inviato da albgen
    #include <stdio.h>
    #include <stdlib.h>
    libro.cpp:44: error: `itoa' undeclared (first use this function)

    uso g++ 3.3
    Io ho compilato questo senza problemi

    codice:
    // Compilabile con BCC32 (v5.5)
    // E con MinGW32-GCC-3.4.2 (g++.exe)
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    	int number = 12345;
    	char string[25];
    	
    	itoa(number, string, 10);
    	printf("integer = %d string = %s\n", number, string);
    
    	printf("%i", atoi("123") );
    	getch();
    	return 0;
    }
    Forse il tuo non è installato correttamente. :master:
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  8. #8
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    il g++ del mingwin non è lo stesso del g++ del http://gcc.gnu.org.

    ho trovato questa pagina http://www.cplusplus.com/ref/cstdlib/
    in fondo c'è scritto:
    * = not ANSI-C, but supported by most compilers.

    probabilmente il gcc non è 100% ansi-c

    I got the remedy

  9. #9
    Originariamente inviato da albgen
    il g++ del mingwin non è lo stesso del g++ del http://gcc.gnu.org.
    Proprio lo stesso no... Ma in pratica...
    ho trovato questa pagina http://www.cplusplus.com/ref/cstdlib/
    in fondo c'è scritto:
    * = not ANSI-C, but supported by most compilers.

    probabilmente il gcc non è 100% ansi-c

    Che io sappia non esiste ancora un compilatore 100% ansi-c !
    Parziale soluzione : Installa il MinGW

    Oppure potresti ripiegare con questo :
    codice:
        char str[10];
        int val=123;
        sprintf(str, "%d", val);
        printf("%s", str);
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  10. #10
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    il mingw l'ho installato ma il progetto che stò facendo, deve compilare con gcc 3.3. ti posso garantire che se provo lo stesso codice con mingw mi vengono furoi un centinaio di errori..invece il viceversa no, cioè se ho un codice scritto con ming e lo compilo con gcc allora va tutto bene..non sò perchè succede questo, ma è cosi'.

    grazie.
    I got the remedy

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 © 2024 vBulletin Solutions, Inc. All rights reserved.