How to use gets()
in C++
, when prompted to type and store in a variable type char
?
How to use gets()
in C++
, when prompted to type and store in a variable type char
?
You should avoid using gets
because no verification of the data entry, when you use it you assume that the user will enter a less than or equal number of characters allocated to the buffer When doing this, your code may be subject to the buffer in> .
Note : This function has become obsolete in C ++ 11 and removed from C ++ 14 .
Some alternatives are: fgets
, getline
fgets
other than gets
, allows you to set the character limit which will be read even if the received value is larger, thus making it impossible to populate 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;
}
You may also prefer to use the std::string
class instead of char
because it does managing your own memory, which as needed, allocates more memory dynamically. std::string
generally protects against buffer overflow, but there are still situations where programming errors can lead to buffer overflow.
Example with 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;
}
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;
}
Avoid using gets. To store only one char in C ++ use the default 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) to a space or enter, use the default 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 gets
, and want to use it on a whim to just read a single char do:
#include <stdio.h>
int main()
{
char ch[1];
gets(ch);
printf("Caractere: %c\n", *ch);
}
gets
replaces 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 made a mistake, I'm used to C.
The gets
function is most commonly used to create strings, so we use vector to store each letter.
To store only one character, the getchar
or fgetc
is used most:
#include<stdlib.h>
#include<stdio.h>
int main(){
char letra;
printf("Digite uma letra: ");
letra = getchar();
printf("%c", letra);
}