Jump to content

Cin.get() - Entao??


Recommended Posts

Posted
#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++

Posted
#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++

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.