Doubt with introduction of data by the user

1

I'm starting in this of the programming in C ++ and I have to say that it is one of the few programming languages that I really liked.

Now I'm with the topic of data entry by the user, but I have a question, because in some sites I see that they use "cin >> " and in others they use cin.getline() .

How are they different?

Which is better?

    
asked by Jogofus 22.11.2016 в 22:04
source

2 answers

2

Within object-oriented programming, it is usual for objects to have a series of methods that allow interacting with the object. It is the example of cin.getline .

On the other hand, languages like C ++ support operator overload. In some languages such as C, each operator fulfills a very specific mission and only for native types (those predefined by the language) and its use can not be modified under any circumstances. The sum operator is an example:

int a = 5, b = 2;
int c = a + b; // correcto

struct test
{
  int valor;
};

struct test a,b;
a.valor = 5;
b.valor = 2;
struct test c = a + b; // ERROR, el compilador no sabe como aplicar este operando a una estructura

As we have said, in C ++ it is possible to redefine the behavior of certain operands to adapt them to particular objects of our program. So we could do the following:

struct test
{
  int valor;

  test& operator+(test& otro)
  {
    test toReturn;
    toReturn.valor = valor + otro.valor;
    return toReturn;
  }
};

struct test a, b;
a.valor = 3;
b.valor = 2;
struct c = a + b; // Correcto
std::cout << c.valor << '\n'; // Imprime 5

In the same line, other operators that can be overloaded are insertion% << and% extraction >> :

struct test
{
  int valor;

  test& operator+(test& otro)
  {
    test toReturn;
    toReturn.valor = valor + otro.valor;
    return toReturn;
  }

  friend std::ostream&& operator<<(std::ostream& out, test& dato);
  friend std::istream&& operator>>(std::istream& in, test& dato);
};

std::ostream&& operator<<(std::ostream& out, test& dato)
{
  return out << dato.valor;
}

std::istream&& operator>>(std::istream& in, test& dato)
{
  return out >> dato.valor;
}

struct a;
std::cin >> a; // Se recibirá un entero y se almacenará en a.valor;
std::cout << a; // Se imprimirá el valor almacenado en a.valor;

I will not go into details about the use of friend because that would lead me to deal with a series of topics that exceed the scope of the question.

Of course, overloading operators is something to do only in very specific cases to avoid unexpected behavior due to silly little details of the language such as implicit conversions.

As an additional detail, say that operators can be invoked just like any other function. So we can do the following:

struct test a, b;
a.valor = 3;
b.valor = 2;
struct c = a.operator+(b); // Correcto
std::cout << c << '\n'; // Imprime 5

Thus, the use of the operator >> is determined by a series of overloads of said operator, while cin.getline is not more than a member function of cin .

Of course, when deciding which option to use pay close attention to the details of each function. The standard library has few redundant options. This means that two functions can work the same only in appearance.

Thus, for example, cin.getline reads a text string until a line break (or a null character) is encountered, while the operator >> stops when it also finds a space.

If, for example, typing "Esto es una prueba" is typed:

  • cin.getline will recover "Esto es una prueba"
  • cin >> var will recover "Esto"

This does not include the fact that the operator >> allows you to recover integers, floating point numbers, ... while cin.getline only allows you to read text strings.

    
answered by 22.11.2016 / 22:53
source
0

cin can "read" (convert, cast in reality) any type of data and save it in a variable, instead cin.getline only saves strings (wait char* as parameter).

To show an example of in which case you would use which:

#include <iostream>
using namespace std;

int main() {
  int numero;
  char cadena[11];

  cout << "Ingrese su edad: ";
  cin >> numero;
  cin.ignore(); // consumir Enter (\n)

  cout << "Ingrese su nombre: ";
  cin.getline(cadena, 10);

  cout << "Su nombre es: " << cadena << " Y su edad: " << numero;

  return 0;
}
    
answered by 22.11.2016 в 22:18