Jump to content

Console.ReadLine not getting correct input (C#)

RileyTheFox
Go to solution Solved by Bartholomew,
12 minutes ago, EvilCat70 said:

This is the entire thing. Mine is also in a for loop 


for(; ; )
            {
                if (Console.ReadLine() == "reload")
                {
                    try
                    {
                        Bot.users.Clear();
                        Utils.cooldowns.Clear();
                        Utils.disabledMods.Clear();
                        Prices.prices.Clear();

                        Utils.GetUsersFile(out Bot.users);
                        Utils.GetCooldowns();
                        Utils.GetDisabledMods();
                        Prices.GetAllPrices();

                        Console.WriteLine("Reloaded!");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Error while trying to reload. Please restart the program");
                    }
                }
                if (Console.ReadLine() == "hello") Console.WriteLine("Hello.");

 

Store the result of readline in a variable.

 

The first if is where your first hello is being ignored due to testing against reload.

 

Readline once, compare result multiple. Now you ask for a new inout line for each comparison, so on each attempt only one word can work (compare equal).

Hey. I'm making a console application and I need to get user input to do things with the program.

 

However, upon testing, I need to enter the correct 'command' 2 or 3 times for the code to register that I've typed what it's looking for. I type it the exact same way every time but it will only execute code on the 2nd or 3rd attempt.

 

if (Console.ReadLine() == "hello") Console.WriteLine("Hello.");

DDVIVNi7R8.png

 

As you can see, I had to type 'hello' twice for the program to respond with 'Hello.'

 

What am I doing wrong? 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

I just tried this myself, it works for me first readline.

 

I put it into a for loop and it also worked...do you have any code surrounding what you posted??

 

Thanks!

 

Chris R.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Chris_R. said:

I just tried this myself, it works for me first readline.

 

I put it into a for loop and it also worked...do you have any code surrounding what you posted??

 

This is the entire thing. Mine is also in a for loop 

for(; ; )
            {
                if (Console.ReadLine() == "reload")
                {
                    try
                    {
                        Bot.users.Clear();
                        Utils.cooldowns.Clear();
                        Utils.disabledMods.Clear();
                        Prices.prices.Clear();

                        Utils.GetUsersFile(out Bot.users);
                        Utils.GetCooldowns();
                        Utils.GetDisabledMods();
                        Prices.GetAllPrices();

                        Console.WriteLine("Reloaded!");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Error while trying to reload. Please restart the program");
                    }
                }
                if (Console.ReadLine() == "hello") Console.WriteLine("Hello.");

 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, EvilCat70 said:

This is the entire thing. Mine is also in a for loop 


for(; ; )
            {
                if (Console.ReadLine() == "reload")
                {
                    try
                    {
                        Bot.users.Clear();
                        Utils.cooldowns.Clear();
                        Utils.disabledMods.Clear();
                        Prices.prices.Clear();

                        Utils.GetUsersFile(out Bot.users);
                        Utils.GetCooldowns();
                        Utils.GetDisabledMods();
                        Prices.GetAllPrices();

                        Console.WriteLine("Reloaded!");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Error while trying to reload. Please restart the program");
                    }
                }
                if (Console.ReadLine() == "hello") Console.WriteLine("Hello.");

 

Store the result of readline in a variable.

 

The first if is where your first hello is being ignored due to testing against reload.

 

Readline once, compare result multiple. Now you ask for a new inout line for each comparison, so on each attempt only one word can work (compare equal).

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Bartholomew said:

Store the result of readline in a variable.

 

The first if is where your first hello is being ignored due to testing against reload.

 

Readline once, compare result multiple. Now you ask for a new inout line for each comparison, so on each attempt only one word can work (compare equal).

Thank you!

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

As an aside, while

for ( ; ; )
  // some stuff

works, the following is highly preferable:

while (True)
  // do stuff

Because the for loop method might indicate a mistake where the author meant to go back and write the condition, but forgot to do so. In contrast, the while loop method explicitly states that the author meant the loop to be infinite.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, straight_stewie said:

In contrast, the while loop method explicitly states that the author meant the loop to be infinite.

+1 for writing explicit self documenting code

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×