jviana Posted April 28, 2012 at 08:10 AM Report #451886 Posted April 28, 2012 at 08:10 AM #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main () { string tempo; char tempo2; int nc; //nc = numero de carateres //horas = input cout<<"Introduza as horas \n"; cin.get(tempo2); tempo = tempo2; nc = tempo.length(); cout<<nc; cin.get(); return 0; } Seja qualquer coisa, expecto " ", retorna sempre 1. O que está mal? Cumps. J.Viana Learning VB.Net HTML C/C++
Localhost Posted April 28, 2012 at 09:21 AM Report #451890 Posted April 28, 2012 at 09:21 AM Estás a ler uma string para um char. Lê directamente para a variável "tempo". here since 2009
jviana Posted April 28, 2012 at 10:24 AM Author Report #451894 Posted April 28, 2012 at 10:24 AM #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main () { string tempo; char tempo2; int nc; //nc = numero de carateres //horas = input cout<<"Introduza as horas \n"; cin.get(tempo); nc = tempo.length(); cout<<nc; cin.get(); return 0; } Dá-me 3 erros na linha acima assinalada: Error 1 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'std::string' to 'char &' c:\users\joão viana\documents\visual studio 2010\projects\test 1\test 1\test 1.cpp 15 2 IntelliSense: no instance of overloaded function "std::basic_istream<_Elem, _Traits>::get [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list c:\users\joão viana\documents\visual studio 2010\projects\test 1\test 1\test 1.cpp 15 3 IntelliSense: too few arguments in function call c:\users\joão viana\documents\visual studio 2010\projects\test 1\test 1\test 1.cpp 15 Cumps. J.Viana Learning VB.Net HTML C/C++
HappyHippyHippo Posted April 28, 2012 at 02:44 PM Report #451918 Posted April 28, 2012 at 02:44 PM new o istream.operator>>() nem o istream.get() não aceita argumentos do tipo string: http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/ http://www.cplusplus.com/reference/iostream/istream/get/ tenta usar um buffer to tipo char buffer[...]; e depois inicializar a string http://www.cplusplus.com/reference/string/string/string/ exemplo: char buffer[256] ... cin.get(buffer, 256); string s(buffer); uma segunda opção é o use de stringbuf's http://www.cplusplus.com/reference/iostream/stringbuf/ stringbuf buffer; cin.get(buffer); buffer.str(); // a tua string IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
jviana Posted April 28, 2012 at 08:47 PM Author Report #451953 Posted April 28, 2012 at 08:47 PM Muito obrigado! Cumps. J.Viana Learning VB.Net HTML C/C++
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now