Input statement/function in C language
Input statement/function in C++ language
Input statement/function in C language -scanf() function
#include <stdio.h>scanf( format-string[,arguments,...]);
STDIO.H contains the prototypes for the standard input/output functions. printf(), puts(), and scanf() are three of these standard functions. Try running a program without the STDIO.H header and see the errors and warnings you get.
scanf() is a function that uses a conversion specifiers in a given format-string to place values into variable arguments. The arguments should be the addresses of the variables rather than the actual variables themselves. For numeric variables, you can pass the address by putting the address-of operator (&) at the beginning of the variable name. When using scanf(), you should include the STDIO.H header file.
The scanf() function reads data from the keyboard according to a specified format and assigns the input data to one or more program variables. Like printf(), scanf() uses a format string to describe the format of the input. The format string utilizes the same conversion specifiers as the printf() function. The & symbol is C's address-of operator. For example, the statement
scanf("%d", &num);
reads a decimal integer from the keyboard and assigns it to the integer variable x. Likewise, the following statement reads a floating-point value from the keyboard and assigns it to the variable rate:
scanf("%f", &rate); A single scanf() can input more than one value if you include multiple conversion specifiers in the format string and variable names. scanf("%d %f", &num, &rate);Example 1:
int num, num2, num3; scanf( "%d %d %d", &num, &num2, &num3);
Example 2
#include <stdio.h>
main()
{
float rate;
int num;
puts( "Enter a float, then an int" );
scanf( "%f %d", &rate, &num);
printf( "\nYou entered %f and %d ", rate, num );
return 0;
}
Input statement/function in C++ language - Standard input (cin)
the standard input by default is the keyboard, and the C++ stream object defined to access it is cin.#include <iostream.h>
include is a preprocessor instruction that says, "What follows is a filename. Find that file and read it in right here." The angle brackets around the filename tell the preprocessor to look in all the usual places for this file. If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file iostream.h in the directory that holds all the H files for your compiler. The file iostream.h (Input-Output-Stream) is used by cout, which assists with writing to the screen.
"cin" and its related object "cout". These two objects, cout and cin, are used in C++ to print strings and values to the screen.
cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:int num;
cin >> num;
cin >> a >> b;
This is equivalent to:
cin >> a;
cin >> b;
Input statement/function in C#.NET language
Read() The Read() reads the next characters from the standard input stream. If a key is pressed on the console, then it would close.int num = Console.Read()
Console.WriteLine(num);
Console.ReadLine() to get user input. The Console.ReadLine() method returns a string. Console.ReadLine() will read the console input from the user, up until the next newline is detected (usually upon pressing the Enter or Return key).The ReadLine Function reads a single line from the standard input stream or the command line. Basically it reads all the characters a user types until he presses enter.
The Console.ReadLine() statement will read our input, but it is of type string and what we need is an int type. So, we need to convert it with the Convert.ToInt32() statement.
namespace FullNameGenerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your first name:");
string name = Console.ReadLine();
Console.WriteLine("What is your last name:");
string lastName = Console.ReadLine();
string fullName = name + " " + lastName;
Console.WriteLine($"Your full name is: {fullName}");
Console.ReadKey();
}
}
}
Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen).
This method is used to get the next character or function key pressed by the user. The pressed key is displayed in the console window.
Syntax: public static ConsoleKeyInfo ReadKey ();
// C# program to illustrate the
// Console.ReadKey Method
using System;
class readKeyFunction {
public static void Main()
{ int c = 0;
Console.WriteLine("The series is:");
for (int i = 1; i < 10; i++)
{
c = c + i;
Console.Write(c + " ");
}
Console.WriteLine("\npress any key to exit the process...");
// basic use of "Console.ReadKey()" method
Console.ReadKey();
}
}
Output:
The series is:
1 3 6 10 15 21 28 36 45 press any key to exit the process...
Input statement/function in VB.NET
Three different functions that can be used, the Read() function, the ReadLine() Function and the ReadKey() Function.Console.ReadLine() to get user input, The Console.ReadLine() method returns a string. Console.ReadLine() will read the console input from the user, up until the next newline is detected (usually upon pressing the Enter or Return key).The ReadLine Function reads a single line from the standard input stream or the command line. Basically it reads all the characters a user types until he presses enter.
Dim input as String = Console.ReadLine()
Dim result As String
result = Console.ReadLine()
Console.WriteLine(result)
The Read Function reads only one single character from the standard input stream. It returns the next character from the input stream, or returns -1 if there are no more characters to be read.
Dim result As String
result = Console.Read()
Console.WriteLine(result)
The ReadKey function reads only one single character from the standard input stream or command line. It is typically used when you’re giving the user multiple options to select from, such as select A, B or C. Another common example is, Press Y or N to continue.
The difference between the ReadKey and Read is that ReadKey returns a character.
Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen).
Comments
Post a Comment