Nakov.IO.Cin 2.0.2.1

Install-Package Nakov.IO.Cin -Version 2.0.2.1
dotnet add package Nakov.IO.Cin --version 2.0.2.1
<PackageReference Include="Nakov.IO.Cin" Version="2.0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nakov.IO.Cin --version 2.0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Nakov.IO.Cin, 2.0.2.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

  • .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.

Version Downloads Last updated
2.0.2.1 127 2/26/2021
2.0.0 103 2/26/2021
1.0.0 1,279 10/10/2015

This release supports the .NET Standard (.NET Core and .NET Framework).