Resolved Do while loop until Enter key

Paradoxz1

Member
Joined
Feb 8, 2021
Messages
14
Programming Experience
Beginner
Hello! I'm new to programming in C# and I'm trying an exercise where I have to write a console application that loops until the enter key is pressed.
My issue is that I don't know how to do a "Do While" loop to keep looping until the Enter key has been pressed since I need to press enter key to submit my string to Console.ReadLine. And also the application doesn't loop straight after Console.WriteLine on line 36 in SelectionAndIteration.cs has been executed, it waits for an input from the user before line 25 is executed.


Main:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_2
{
    class MainProgram
    {
        static void Main()
        {
            Console.Title = "Selection and iteration in C#";
            SelectionAndIteration SAndI = new SelectionAndIteration();

        }
    }
}



SelectionAndIteration:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_2
{
    class SelectionAndIteration
    {
        public SelectionAndIteration()
        {

            Console.WriteLine();
            Console.WriteLine("My name is Ai, I'm a console applicaiton!");
            Console.WriteLine();
            
            ShowStringLength();
        }

        private void ShowStringLength()
        {

            do
            {
                Console.WriteLine("\nLet me calculate the length of strings for you!");
                Console.WriteLine("Give me a text of any length, or press 'Enter' to exit\n");

                string userString = Console.ReadLine();

                string upperUserString = userString.ToUpper();

                int userStringLength = userString.Length;

                Console.WriteLine(upperUserString);
                Console.WriteLine("Number of characters in your string = " + userStringLength);

            } while(Console.ReadKey(true).Key != ConsoleKey.Enter);

            return;
        }

    }
}

After the application has run once and user has input a string its a blank line and then after a key has been pressed the ShowStringLength loops again, also when enter key is pressed the same thing happens it doesn't imidietly exit. I know I have done something wrong or missed something as I said I'm very new to this and I'm having some issues because I can't for the life of me figure out what I need to do in order for it to loop until enter key is pressed (when blank, so no text ios submitted). I hope you understand me and my issue I'm sorry if my explanation of the issue was vague, this was very hard to explain.

//Thanks in advance
 
Solution
If user just press Enter the input will be an empty string (length 0), so you just use that expression in while. Algorithm in pseudo code:
C#:
do
   write explanation
   read input
   write length
while (input.length>0)

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,197
Location
Norway
Programming Experience
10+
If user just press Enter the input will be an empty string (length 0), so you just use that expression in while. Algorithm in pseudo code:
C#:
do
   write explanation
   read input
   write length
while (input.length>0)
 
Solution
Top Bottom