How to use gets() in C++?

Asked

Viewed 18,584 times

4

How to use gets() in the C++, when asked to type it and store it in a type variable char?

  • 4

    Do not use, utilize fgets. :)

  • @leonardovIlarinhoe need to include some library to use gets()?

  • depends, if to read a single character do not need, but if to read a string you have to include the string. h, sorry I had withdrawn the answer because I was wrong, I fixed it and put it again

  • 1

    Nape gets!

3 answers

11


One should avoid using the gets, because no data entry is checked, when using it you assume that the user will enter a lower or equal amount of characters allocated to the buffer, In doing this, your code may be subject to bursting of buffer.

  1. Buffer computer
  2. What is the buffer overflow?

Note: This function has become obsolete in C++11 and removed from C++14.

Some alternatives are: fgets, getline

The fgets how different from the gets, allows to stipulate the limit of characters that will be read, even if the received value is higher, thus preventing the overflow of buffer. See the example below:

#include <stdio.h>

#define LIMITE 10

int main(void) {
    char linha [LIMITE];
    puts("Digite alguma coisa: "); // Stack Overflow em Português!

    if (fgets(linha, sizeof(linha), stdin) != NULL) {
        printf("Voce digitou: %s\n", linha); // Stack Ove
    }
    return 0;
}

See demonstração

You may also prefer to use the class std::string instead of char, because it manages its own memory, which as needed, allocates more memory dynamically. The std::string generally protects against overflow of buffer, but there are still situations where programming errors can lead to the overflow of buffer.

Example with the std::getline:

#include <iostream>

int main ()
{
  std::string linha;

  std::cout << "Digite um valor: " << std::endl;
  std::getline (std::cin, linha);
  std::cout << "Voce digitou: " << linha << std::endl;

  return 0;
}

See demonstração

If you prefer to continue using char, you can do so:

#include <iostream>

int main() {
    char linha[10];

    std::cout << "Digite um valor: " << std::endl; // Stack Overflow em Portguês!
    std::cin.getline(linha, sizeof(linha));
    std::cout << linha << std::endl; // Stack Ove

    return 0;
}

See demonstração

5

Avoid using gets. To store only one char in C++ use the standard input object (Cin).

#include <iostream>

int main()
{
    char ch;

    std::cout << "Digite um caractere: ";
    std::cin >> ch;

    std::cout << "Caractere: " << ch << std::endl;
}

If you want to store multiple chars (in a string format) up to a space or enter also use the standard input object:

#include <iostream>

int main()
{
   std::string minhaString;

   std::cout << "Digite uma string: ";
   std::cin >> minhaString;

   std::cout << "String: " << minhaString << std::endl;
}

If you want to read strings with spaces do:

#include <iostream>

int main()
{
   std::string minhaString;

   std::cout << "Digite uma string: ";
   getline(std::cin, minhaString);

   std::cout << "String: " << minhaString << std::endl;
}

but, if you really like the gets, and wants to use it on a whim to only read a single char do:

#include <stdio.h>

int main()
{
   char ch[1];

   gets(ch);
   printf("Caractere: %c\n", *ch);
}

4

gets replaces the scanf would look like this:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(){
    char lista[100];
    printf("Digite seu nome: ");
    gets(lista);
    printf(lista);
}

Sorry if I missed something, I’m used to C.

The function gets is most used for string creation (string), so vector is used to store each letter.

To store only one character the getchar or fgetc:

#include<stdlib.h>
#include<stdio.h>
int main(){
    char letra;
    printf("Digite uma letra: ");
    letra = getchar();
    printf("%c", letra);
}
  • 1

    -1 for gets and +1 for getchar = 0 rating :)

  • kk just gave an example of how to use gets, answered the question of gúri, also know that using gets is not the best option

  • In the last code, the variable letra there’s got to be some kind int.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.