tags:

views:

105

answers:

2

I wish to write a program in C# that executes line to line.

By which I mean the equivalent of C++

#include <stdio.h>

int main ()
{
  char c;
  puts ("Enter q to exit:");
  do {
    c = getchar();
    putchar(c);
  } while (c != 'q');
  return 0;
}

What would the equivalent in c# be?

+1  A: 

Use Console.Read (closest to getchar()), Console.ReadKey and/or Console.ReadLine.

itowlson
+2  A: 

The idiomatic c# implementation would be via the System.Console class.

Specifically one of Read or ReadKey ReadLine

Read is closest to the spirit of getchar though ReadKey is easier and safer to use. If you wish to operate line by line ratehr than character by character then ReadLine is your best bet.

using System;
using System.IO;

public static void Main(String[] args)
{
  ConsoleKeyInfo k;
  Console.WriteLine("Enter q to exit:");
  do {
    k = Console.ReadKey();
    Console.WriteLine(k.KeyChar);
  } while (k.KeyChar != 'q');
  return 0;
}
ShuggyCoUk