Skip to main content

Input

Input Statement/Function

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;

Example 2: Single statement
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

Popular posts from this blog

Robot

Robot is drawn from an old Church Slavonic word, robota, for “servitude,” “forced labor” or “drudgery.” The word, which also has cognates in German, Russian, Polish and Czech, was a product of the central European system of serfdom by which a tenant’s rent was paid for in forced labor or service. The word robot was coined by artist Josef Čapek, the brother of famed Czechoslovakian author Karel Čapek. As a word, robot is a relative newcomer to the English language. It was the brainchild of a brilliant Czech playwright, novelist and journalist named Karel Čapek (1880-1938) who introduced it in his 1920 hit play, R.U.R., or Rossum’s Universal Robots. The robots in this play were not what we would call robots today, and they weren’t made of steel, plastic, and lines of code. Those robots were manufactured as pseudo-organic components out of a substance that acted like protoplasm in a factory, then “assembled” into humanoids. Watch video --> Saudi Arabia grants citizenship to huma...

The 120 year old light bulb that never been turned off.

Light bulb that never been turned off since 1901. ivermore's Centennial Light Bulb The Centennial Light is the world's longest-lasting light bulb, burning since 1901 , and almost never switched off. Due to its longevity, the bulb has been noted by The Guinness Book of World Records , Ripley's Believe It or Not!, and General Electric.  The Centennial Light was originally a 30-watt(or 60-watt) bulb, but is now very dim, emitting about the same light as a 4-watt nightlight. The hand-blown, carbon-filament common light bulb was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s and was invented by Adolphe A. Chaillet. The hand-blown, carbon-filament common light bulb was invented by Adolphe Chaillet, a French engineer who filed a patent for this technology. It was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s; many just like it still exist and can be found functioning. According to Zylpha Bernal Beck, the bulb was...

Wireless power transfer

Wireless power transfer Inventor and engineer Nikola Tesla, 'the man who invented the 20th century' theorized about wireless electricity back in the 1890s. He even demonstrated the principle by lighting up glass tubes with wireless power transmission. Nikola Tesla wanted to create the way to supply power without stringing wires. He almost accomplished his goal when his experiment led him to creation of the Tesla coil. It was the first system that could wirelessly transmit electricity. Wireless power transfer (WPT), wireless power transmission, wireless energy transmission (WET), or electromagnetic power transfer is the transmission of electrical energy without wires as a physical link. Wireless power transfer (WPT) is one of the hottest topics being actively studied, and it is being widely commercialized. In particular, there has been a rapid expansion of WPT in mobile phone chargers, stationary charging electric vehicles (EVs), and dynamic charging EVs, also called road-po...