1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lotto
{
class Program
{

    static void Main(string[] args)
    {
        char k = 'l';
        while (!(k == 'k'))
        {
            Random rnd = new Random();
            int[] tablica = new int[6];
            for (int i = 0; i < 6; i++)
            {

                    tablica[i] = 0;

            }
                for (int i = 0, z; i < 6; i++)
                {
                   {
                    z = rnd.Next(1, 49);
                    while (tablica.Contains(z))
                    {
                        z = rnd.Next(1, 49);
                    }
                    tablica[i] = z;
                }
            }
            Array.Sort(tablica);
            foreach (int q in tablica)
            {
                Console.Write(q);
                Console.Write(", ");
            }
            k = Convert.ToChar(Console.Read()) ;
            Console.WriteLine("\n\n\n");
        }
    }


    }
 }

It works alright. When I use the step by step clickage (F10 in visual studio), it runs fine. But when I run it normally, then after the

k=Convert.ToChar(Console.Read());

when I supply 'k', the program stops, as intended. when I supply nothing, it does the

 foreach (int q in tablica)
        {
            Console.Write(q);
            Console.Write(", ");
        }
        k = Convert.ToChar(Console.Read()) ;
        Console.WriteLine("\n\n\n");

two times, and when I supply anything other than 'k' it does it three times. What.The.Hell.

svick
  • 236,525
  • 50
  • 385
  • 514

3 Answers3

6

Console.Read reads a single character at a time from the input stream (doc). When you press enter you are supplying two characters: \r then \n, so there are two characters to read before pausing for further user input.

I'm confused how it ran fine when you used F10 as I did this to see what was looping, I just so happened to use Enter when supplying "nothing" and saw the characters pop through.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

Did you try capturing the input from Console.Read() and see what it is giving you? It might be something to do with a control character being read in, and it not being able to convert to char properly.

Also, completely aside, I would recommend reformatting your code (you have superfluous "{}" in there in the second for loop). Also, it is always good practice to give meaningful names to your variables. i, j, k, etc. have no inherent meaning. It will just be easier to refactor/maintaining your code in the future.

I didn't take a real close look at the rest of your code, but I'm guessing there are shorter/simpler ways to do a lot of it. This many for/foreach/while loop in one method has a code smell.

ChrisC
  • 1,161
  • 12
  • 26
1

By using Console.Read(), you are actually getting your input plus "\r\n",so when you use ENTER you just get "\r\n"(2 chars), when you input a char"m", your input is "m\r\n"(3 chars), that's the reason why there is a "one" difference.

Besides, why use int i = 0, z;? the , z does not mean anything here for this is C++ usage.

JoeyD
  • 21
  • 2