Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1

    [C] errore strano (probabilmente da niubbio)

    Non ho finito l'altro post ed eccomi a voi con un altro problemuccio stavo facendo delle prove, quando mi sono imbattuto in un errore strano (per me). Allora, io ho questa porzione di codice C:

    Codice PHP:
    #include <stdio.h>

    int main(){

    char ciao[10];

    scanf("%s", &ciao);

    if (
    ciao == "dici")
        
    printf ("Complimenti hai superato un esame");

    else if (
    ciao == "quindi")
        
    printf ("Devi sostenere l'orale per questo esame");

    else
        
    printf ("Non hai superato l'esame");

    return 
    0;


    Il problema è che mi esegue direttamente l'istruzione dell'else e non so perchè :master:
    Powered by Linux

    "Windows cerca di fare Unix e ci riesce male, Red Hat cerca di fare Windows e ci riesce benissimo" (Jimmy Olgeni)

  2. #2
    Nella scanf() , se usi le stringhe non ci va' '&'...
    quindi diventa scanf("%s", stringa); ma non penso sia il problema....

    Invece di scanf() per le stringhe usa gets() perche' scanf() non prende gli spazi....

    gets (stringa);
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  3. #3
    Sono un pirla anchio perche' non lo visto subito...
    ma se voui confrontare due stringhe non puoi usare '=='
    ma devi usare la funzione di libreria
    strcmp()...

    codice:
    #include <string.h>
    #include <stdio.h>
    
    int main() {
        char str[10];
    
        printf ("Inserisci una Stringa : ");
        gets (str);
    
        if (!strcmp(str, "dici")) {
            printf ("Complimenti hai superato un esame");
        } else if (!strcmp(str, "quindi")) {
            printf ("Devi sostenere l'orale per questo esame");
        } else {
             printf ("Non hai superato l'esame");
        }
    
        return 0;
    }
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  4. #4
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    questo tipo di confronto non lo puoi fare
    ricorda che la stringa è un'array di caratteri, non un'unica variabile.
    usa la funzione strcmp (string compare), oppure scriviti da te una funzione che controlla ogni singolo carattere della stringa.

    codice:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    char ciao[10];
    scanf(" %s", &ciao);
    if (strcmp(ciao,"dici"==0))
        printf ("Complimenti hai superato un esame\n");
    else if (strcmp(ciao,"quindi")==0)
        printf ("Devi sostenere l'orale per questo esame\n");
    else
        printf ("Non hai superato l'esame\n");
    return 0;
    }


    [edit]- ma quanto sono lento oggi maledizione
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  5. #5
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    Originariamente inviato da kNemo
    Nella scanf() , se usi le stringhe non ci va' '&'...
    quindi diventa scanf("%s", stringa); ma non penso sia il problema....

    Invece di scanf() per le stringhe usa gets() perche' scanf() non prende gli spazi....

    gets (stringa);
    :master:


    Never use gets(). Because it is impossible to tell with-
    out knowing the data in advance how many characters gets()
    will read, and because gets() will continue to store char-
    acters past the end of the buffer, it is extremely danger-
    ous to use. It has been used to break computer security.
    Use fgets() instead.
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  6. #6
    Caspio non lo sapevo...
    comunque la scanf() non prende le parole dopo gli spazi.
    poi nella scanf() non ci va' '&' anche se funziona ugualmente...
    perche' gli dai gia' il puntatore alla prima componente (che e' gia' un indirizzo)....
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  7. #7
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    Originariamente inviato da kNemo
    Caspio non lo sapevo...
    comunque la scanf() non prende le parole dopo gli spazi.
    poi nella scanf() non ci va' '&' anche se funziona ugualmente...
    perche' gli dai gia' il puntatore alla prima componente (che e' gia' un indirizzo)....
    questione di gusti
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  8. #8
    Utente bannato
    Registrato dal
    Nov 2003
    Messaggi
    558
    Ma perchè tutti includono string.h per usare strcmp () ?? :master: Nn ho ancora trovato uno che nn lo fa....

    Eppure strcmp () è dichiarato anche in stdio.h ...

  9. #9
    io la includo perche' e' in string.h
    ma se in alcuni compilatori (dev C++) e' in stdio.h non vero perche' non dovrei includerla ugualmente...

    per me' e' una questione di standard
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  10. #10
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ah, comunque con scanf si può evitare il salto degli spazi con l'opzione %c indicando il numero di caratteri da leggere oppure con i set "[]"
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

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.