0
I know that getchar()
gets keyboard characters by stdin
, but I want to know how to enter numbers into a variable.
For I use getchar()
and type a 2
, for example, the variable becomes 50
, and it’s obviously not what I want.
0
I know that getchar()
gets keyboard characters by stdin
, but I want to know how to enter numbers into a variable.
For I use getchar()
and type a 2
, for example, the variable becomes 50
, and it’s obviously not what I want.
5
Well, you didn’t specify the language, so let’s go:
To read a number, I will assume that it is an int, in C the following code is used:
int meuNumero; scanf("%d", &meuNumero);
To read a number in C++ we use the stream cin
with the operator >>
:
int meuNumero; cin >> meuNumero;
In C# we have to call the method Console.ReadLine
to read the entrance and Convert.ToInt32
or Int32.Parse
to turn to a number:
int meuNumero = Convert.ToInt32(Console.ReadLine())
3
If it is C# you can use the Console.ReadLine()
but do not forget that the ReadLine()
returns a String, so you need to convert. You can use the Convert.ToInt32(Console.ReadLine());
.
Then stay:
int input = Convert.ToInt32(Console.ReadLine());
.
If you type a String that is not int, you probably get an exception.
If you are using C, with the stdio library. h, you will be able to catch an int using the famous scanf()
.
Then stay:
int input;
scanf("%d", &input);
Finally, if you are using C++, you can always use the sdt namespace.
Then stay:
int input;
std::cin >> input;
I hope I’ve helped.
1
In C language, use getch()
to capture the first character typed, example:
char op;
op=getch(); //Lê a primeira tecla que for digitado no teclado, e grava na variável
printf("Caractere digitado: %c", op); //Printa na tela o caractere digitado
If you want to save more than one character in a variable use scanf()
as already exemplified in the other answers.
If you want to receive only numbers and make calculations with them use the scanf()
using variables of type int for integers and float for integers, for example:
//Fazendo cálculos com números inteiros
int a, b, result_int;
printf("Digite o primeiro valor: \n");
scanf("%d", &a);
printf("Digite o segundo valor: \n");
scanf("%d", &b);
result_int = a + b;
printf("a + b = %d\n", result_int); //Printa na tela o resultado da soma
//Fazendo cálculos com números não inteiros
float c, d, result_float;
printf("Digite o primeiro valor: \n");
scanf("%f", &c);
printf("Digite o segundo valor: \n");
scanf("%f", &d);
result_float = c + d;
printf("c + d = %f\n", result_float); //Printa na tela o resultado da soma
printf("c + d = %.2f\n", result_float); //Printa na tela o resultado da soma, limitando as casas depois da virgula para melhor visual
Compile the code for you to better understand rsrs.
Browser other questions tagged c# windows
You are not signed in. Login or sign up in order to post.
C, C# or C++? I’m confused.
– Jéf Bueno
The answers here, so far, work for a quick exercise, but for general use it takes a little more to ensure that the input is correct.
– Maniero
Has
stdin
in C#? Wait, there’s the C tag, but the question title is C#, wait, there’s C++ too, but what a mess!– CypherPotato
Please when posting your question ask clearly and objectively with detailed information about what you need and what languages you are using. Your question became very complicated to understand,and used the tags still only to complicate more. So I will give -1 to your question.
– Falion