5

I made an infinity donkey and i want console to stop scrolling to the end automatically so people can scroll to end by themselves.

I tried Console.SetCurserPosition(5 , 5); Didn't help.

Here is my code :

using System;
using System.Threading;
namespace Infinite_Donkey
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Press 'Enter' and wait to see the magic!");
            Console.ReadLine();
            Console.WriteLine("            ^__^");
            Console.WriteLine("            (oo)_______");
            Console.WriteLine("            (__)       )\\");
            Console.WriteLine("                ||---||  ");
            Console.WriteLine("                ||   ||  ");
            Thread.Sleep (2000);
            do
            {
                Console.WriteLine("                ||   ||");
            }while(true);
        }
    }
}

Note : Program works kind of fine but i just want it to stop scrolling down automatically.

  • One thing you could do, as like a safe-gard, is add a `Console.ReadLine()` after the writelines. This will stop of the console there until the user enters a value – JamesS Aug 28 '19 at 11:05
  • Possible duplicate of [C# is there a way to set the scroll position of a console application](https://stackoverflow.com/questions/8729823/c-sharp-is-there-a-way-to-set-the-scroll-position-of-a-console-application) – Ruud Helderman Aug 28 '19 at 11:09
  • Ruud Helderman, it did'nt work for me. –  Aug 28 '19 at 11:12
  • What exactly are you try to do? "Hold" the program when it reaches the last line of the console? What should then happen? Waiting for a specific key to "scroll", or maybe even scroll up again? – nabuchodonossor Aug 28 '19 at 11:13
  • 2
    @1mpossible - there is no way to do this, with any kind of commands, because of infinite loop – Kuba Do Aug 28 '19 at 11:14
  • So there is no way to stop console output from scrolling? –  Aug 28 '19 at 11:15
  • @1mpossible: You have to PROGRAM that behavior. And then everything is possible - see my answer. – nabuchodonossor Aug 28 '19 at 11:25
  • it says "unexpected character '$'" –  Aug 28 '19 at 11:27

5 Answers5

3

This is what I usually do:

Console.SetWindowPosition(0 , currentItem);
Lorenzo Isidori
  • 1,809
  • 2
  • 20
  • 31
1

try the following code .... inside your main()

        int numPages = 1;
        int maxLine = 20;

        int currentLine = 0;

        while (true)
        {
            Console.WriteLine(String.Format($"Page {numPages}; Line {currentLine}"));
            currentLine++;
            if (currentLine > maxLine)
            {
                Console.WriteLine("Press ENTER to continue, 'x' to stop");
                string answer = Console.ReadLine();
                if (answer.Length > 0 && (answer[0] == 'x' || answer[0] == 'X'))
                {
                    break;
                }
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                currentLine = 0;
                numPages++;
            }
        }

        Console.ReadLine();
nabuchodonossor
  • 2,095
  • 20
  • 18
  • unepcepted character '$' –  Aug 28 '19 at 11:27
  • 1
    That's a C# 6.0 feature I think and you're using an older version. Replace `$"Page {numPages}; Line {currentLine}"` by `string.Format("Page {0}: Line {1}", numPages, currentLine)` – Paul Karam Aug 28 '19 at 11:37
  • What version of C# do you use? If you cant use interpolated strings due to an OLD version, you have to change the String.Format to use Parameters: String.Format("Page {0}; Line {1}", numPages, currentLine) – nabuchodonossor Aug 28 '19 at 11:38
  • Show us exact what you did! And, first, what do you mean with "not worked": worked as you expected or garbage happend? – nabuchodonossor Aug 28 '19 at 11:45
  • This sample should give you a clue how the console works with user interaction. You did not include the paging logic in your sample. So i suggest: Try this paging logic, and apply the learned to your app. – nabuchodonossor Aug 28 '19 at 12:03
1

Console.SetCursorPosition sets the position of the cursor. Which actually means that you're telling the console to start writing from that exact position and forward. That logically shouldn't stop the scrolling with a do {} while();

Console.SetWindowPosition is more fit to do the job, but you need to be aware of a Console buffer size. Even if you make the buffer size equal to the max value and use the set window position function, it won't do the trick, the donkey will keep showing, but you can't scroll and it will feel like jumping, have a look with this code:

static void Main(string[] args)
{

    Console.WriteLine("            ^__^");
    Console.WriteLine("            (oo)_______");
    Console.WriteLine("            (__)       )\\");
    Console.WriteLine("                ||---||  ");
    Console.SetBufferSize(1000, Int16.MaxValue-1);
    do
    {

        Console.WriteLine("                ||   ||");
        Console.SetWindowPosition(0, 0);
    } while (true);
}

So, what can you actually do to achieve your goal, or at least a part of it, set the buffer size to a maximum, write all your lines, then set the window position to the start, this way, you can scroll and keep scrolling until you hit the bottom, but I highly doubt that you could achieve your primary goal within a loop, but I could be wrong.

Check the code of the way to do it without an infinite loop:

static void Main(string[] args)
{

    Console.WriteLine("            ^__^");
    Console.WriteLine("            (oo)_______");
    Console.WriteLine("            (__)       )\\");
    Console.WriteLine("                ||---||  ");
    Console.SetBufferSize(1000, Int16.MaxValue - 1);

    for (int i = 0; i < Int16.MaxValue-10; i++)
    {
        Console.WriteLine("                ||   ||");
    }

    Console.SetWindowPosition(0, 0);
    Console.ReadLine();
}

I can't think of any other way to partially achieve your goal without user interaction and within a loop.

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
0

I couldn't get the answer i wanted but i improved my code with the tips you gave. Here is the code :

using System;
using System.Threading;
namespace Infinite_Donkey
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("EN : Press 'Enter' and wait to see the magic!");
            Console.WriteLine("TR : 'Enter' tuşuna basip bekle.");
            Console.Read();
            Console.WriteLine("            ^__^");
            Console.WriteLine("            (oo)_______");
            Console.WriteLine("            (__)       )\\");
            Console.WriteLine("                ||---||  ");
            Console.WriteLine("                ||   ||  ");
            Thread.Sleep(2000);
            do
            {
                Console.WriteLine("                ||   ||");
                Thread.Sleep(100);
            }while(true);
        }
    }
}
0

@user9257990: you can do it by setting console buffer size.


using System;
using System.Threading;
namespace Infinite_Donkey
{
    class Program
    {
        public static void Main(string[] args)
        {

            var largestWindowX = Console.WindowWidth;
            var largestWindowY = Console.WindowHeight;

            Console.BufferWidth = Console.WindowWidth = largestWindowX;
            Console.BufferHeight = Console.WindowHeight = largestWindowY;


            Console.WriteLine("Press 'Enter' and wait to see the magic!");
            Console.ReadLine();
            Console.WriteLine("            ^__^");
            Console.WriteLine("            (oo)_______");
            Console.WriteLine("            (__)       )\\");
            Console.WriteLine("                ||---||  ");
            Console.WriteLine("                ||   ||  ");
            Thread.Sleep (2000);
            do
            {
                Console.WriteLine("                ||   ||");
            }while(true);
        }
    }
}
haseakash
  • 31
  • 1
  • 6