Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142

    [C++] Come acquisire una stringa vuota

    Salve ragazzi avrei un problemino dovrei acquisire delle stringhe e poi quando l utente non ne vuole più acquisire basta che prema invio per acquisire una stringa vuota e così l acquisizione finisce:
    ecco il codice
    codice:
    typedef char Tstring25[26];
    typedef Tstring25 tvs[20];
    
    int main()
    {
      Tstring25 parola;
      tvs frase;
      
      int cont,i;
      
      cont = 0;
      i = 0;
      do{  
        cout<<"Inserisci la "<<i+1<<char(248)<<" stringa: "<<endl;    
        cin>>frase[i]; 
        i++;
      }while((i!='\0')&&(i<20)&&(i!=//qui non so cosa mettere per far terminare l acquisizione con una stringa vuota));

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Una strcmp() ?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    in che senso scusa devo confrontare la stringa con che cosa??

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Hai detto che devi confrontare la stringa acquisita con una stringa vuota. La strcmp() confronta due stringhe. Una la passi tu, l'altra sarà vuota.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    ok adesso provo e vediamo

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    ma nel do while funziona la strcmp() oppure no?perchè a me da errore

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Certo che funziona. Tu come la usi?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    cosi:
    codice:
      do{  
        cout<<"Inserisci la "<<i+1<<char(248)<<" stringa: "<<endl;    
        cin>>frase[i]; 
        i++;  
      }while((i!='\0')&&(i<20)&&(strcmp(frase[i],frase[\0]));

  9. #9
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Dovrebbe essere

    strcmp(frase[i],"") non

    strcmp(frase[i],frase[\0])

    Comunque non funziona con gli array di char perché non viene acquisito il terminatore '\n' finale.
    Prova così.
    codice:
    std::string frase;
    do{  
        cout<<"Inserisci la "<<i+1<<char(248)<<" stringa: "<<endl;    
        getline(cin,frase); 
        i++;  
      }while((i!='\0')&&(i<20)&&(strcmp(frase.c_str(),""));
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  10. #10
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    scusa ma nella maniera che mi hai indicato tu mi da errore come mai??

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.