Nakov.IO.Cin
2.0.2.1
dotnet add package Nakov.IO.Cin --version 2.0.2.1
NuGet\Install-Package Nakov.IO.Cin -Version 2.0.2.1
<PackageReference Include="Nakov.IO.Cin" Version="2.0.2.1" />
paket add Nakov.IO.Cin --version 2.0.2.1
#r "nuget: Nakov.IO.Cin, 2.0.2.1"
// Install Nakov.IO.Cin as a Cake Addin
#addin nuget:?package=Nakov.IO.Cin&version=2.0.2.1
// Install Nakov.IO.Cin as a Cake Tool
#tool nuget:?package=Nakov.IO.Cin&version=2.0.2.1
C++ style cin
console-based input for C# developers
Nakov.IO.Cin
is a console-based input parser for C#, which reads numbers and text in the C++ cin
/ cout
/ iostream
style.
For example, in C++ we can read two integers using this code:
int x, y;
cin >> x >> y;
With Nakov.IO.Cin
we can write the same code in C# like this:
int x = Cin.NextInt();
int y = Cin.NextInt();
Notes
Like in C++, the whitespace will be skipped so users can enter the input numbers on the same line, separated by spaces, or on separate lines.
Nakov.IO.Cin
supports reading int
, double
, decimal
and string
tokens.
Examples
In this example we read two integers in C++:
int rows, cols;
cin >> rows >> cols;
In C# we can read two integers (using tuples) like this:
(int rows, int cols) = (Cin.NextInt(), Cin.NextInt());
Another C++ example:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int* numbers = new int[n];
for (int i = 0; i < n; i++)
cin >> numbers[i];
for (int i = 0; i < n; i++)
cout << numbers[i] << ' ';
}
The corresponding C# code:
using System;
using Nakov.IO;
public class EnterNumbers
{
static void Main()
{
int n = Cin.NextInt();
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
numbers[i] = Cin.NextInt();
for (int i = 0; i < n; i++)
Console.Write(numbers[i] + " ");
}
}
More Detailed Example
This is a more-detailed example how to use the Nakov.IO.Cin
class:
using System;
using Nakov.IO; // See http://www.nakov.com/tags/cin
public class CinExample
{
static void Main()
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter two integers x and y separated by whitespace: ");
// cin >> x >> y;
int x = Cin.NextInt();
double y = Cin.NextDouble();
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("Name: {0}, Age: {1}", name, age);
Console.WriteLine("x={0}, y={1}", x, y);
Console.Write("Enter a positive integer number N: ");
// cin >> n;
int n = Cin.NextInt();
Console.Write("Enter N decimal numbers separated by a space: ");
decimal[] numbers = new decimal[n];
for (int i = 0; i < n; i++)
{
// cin >> numbers[i];
numbers[i] = Cin.NextDecimal();
}
Array.Sort(numbers);
Console.WriteLine("The numbers in ascending order: {0}",
string.Join(' ', numbers));
Console.Write("Enter two strings seperated by a space: ");
// cin >> firstStr >> secondStr;
string firstStr = Cin.NextToken();
string secondStr = Cin.NextToken();
Console.WriteLine("First str={0}", firstStr);
Console.WriteLine("Second str={0}", secondStr);
}
}
This is a sample input and output from the above example:
Enter your name: Albert Einstein
Enter two integers x and y separated by whitespace:
10
20
Enter your age: 25
Name: Albert Einstein, Age: 25
x=10, y=20
Enter a positive integer number N:
5
Enter N decimal numbers separated by a space: 10 30 40
50
20
The numbers in ascending order: 10 20 30 40 50
Enter two strings seperated by a space:
Visual Studio
First str=Visual
Second str=Studio
Note that input numbers and string tokens can be separated by single space, by a new line or by a sequence of white space characters.
More info
Learn more at: https://github.com/nakov/Nakov.io.cin.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net20 net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETFramework 2.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
This release supports the .NET Standard (.NET Core and .NET Framework).