GithubHelp home page GithubHelp logo

nakov / nakov.io.cin Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 8.0 65 KB

Nakov.IO.Cin is console input for C# in the C++ style: cin >> x >> y;

Home Page: https://www.nakov.com/tag/nakov-io-cin/

License: MIT License

C# 100.00%
c-sharp console cin cout csharp console-tool iostream cplusplus

nakov.io.cin's Introduction

Nakov.IO.Cin: the C# console input working as cin in C++

Nakov.IO.Cin is a console-based input reader for C#, which reads numbers and text in the C++ cin / cout / iostream style.

Install the NuGet Package

First, install the NuGet package Nakov.IO.Cin:

Install-Package Nakov.IO.Cin

Now you are ready to translate C++ cin / cout / iostream code to C#.

Sample C++ Code

#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] << ' ';
}

Corresponsing C# Code

using System;
using Nakov.IO; // see http://www.nakov.com/tags/cin
 
public class EnteringNumbers
{
    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

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.

Learn more at: http://www.nakov.com/tags/cin.

nakov.io.cin's People

Contributors

nakov avatar reathh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.