How to make the console read the command typed?

Updated on May 4, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
4 on April 29, 2017

I created a console application for the user to enter commands and get a response but this isnt working….
Heres the code.


string cmd = "!HelloWorld";
if (Console.ReadLine = cmd)

{

Console.ForegroundColor = normColor;

Console.WriteLine("The Commands are: ");

Console.ForegroundColor = cmdColor;

Console.WriteLine("Hello World!");

}

  • Liked by
Reply
3 on April 29, 2017

To store information from “console.ReadLine” you should use this:

string stringName = Console.ReadLine ();
//or
int intName = Convert.ToInt32 (Console.ReadLine ());

Also, this code make no sense. Re watch Brackeys C# tutorials (https://www.youtube.com/playlist?list=PLPV2KyIb3jR6ZkG8gZwJYSjnXxmfPAl51).

Google can help you as well.

on April 29, 2017

Heres the full code:
So how can I use it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginningToScript
{
 class Program
 {
 static void Main(string[] args)
 {
 ConsoleColor normColor = ConsoleColor.White;
 ConsoleColor executedCmd = ConsoleColor.DarkCyan;
 ConsoleColor cmdColor = ConsoleColor.DarkGreen;
 string cmd = "!HelloWorld";
 //if (Console.TextInput)
 //{
 // Console.ForegroundColor = normColor;
 // Console.WriteLine("The Commands are: ");
 // Console.ForegroundColor = cmdColor;
 // Console.WriteLine("Hello World!");
 //}
 while (true)
 {
 // Works the Infinite Writing Setup
 Console.ForegroundColor = normColor;
 Console.Write("Type a Command!: ");
 Console.ForegroundColor = ConsoleColor.DarkGray;
 string questionString = Console.ReadLine();
 // Sets Executed Command & Text Color
 Console.ForegroundColor = executedCmd;
 Console.WriteLine("Executed!");
 // Random randomObject = new Random();
 // Console.WriteLine("{0}", randomObject.Next(10 + 1));
 }
 }
 }
}

on April 30, 2017

I’m also new to coding, so i can’t differ what i don’t know and what is wrong in your code. I never played whit Console.Color. And the way you are using the while loop, i guess it’s incorrect.
/
Tell me what u are trying to do, and i will try to figure a way to it, and it will also help others to understand your question.
/
Watch the tutorial i linked! It’s really helpful!

Helpful
on May 4, 2017

Ok first you need to understand that unless otherwise specified code is always executed from top to bottom. There for where you have your if Method nothing is going to happen because it is never going to be true. Since the string cmd is defined as “!Hello World”. Now to how you can get your program to execute something if you type in cmd. The shortest way would be if (“cmd” == Console.ReadLine()) {} . But that is not exactly elegant because it won’t work if you want to have multiple options. For that you need to create a string variable, let’s call it string cmd and assign what is typed into the console to it.
Next we need to make a switch or an if else statement that executes different things depending on what you type into the console.

Also I wouldn’t use a while (true), actually give the user an option to leave your program 😛 .

Heres the full code:
So how can I use it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginningToScript
{
 class Program
 {
 static void Main(string[] args)
 {
 ConsoleColor normColor = ConsoleColor.White;
 ConsoleColor executedCmd = ConsoleColor.DarkCyan;
 ConsoleColor cmdColor = ConsoleColor.DarkGreen;
 string cmd = "!HelloWorld";
bool exitProgram = false;
 // Works the Infinite Writing Setup until command "quit" is chosen.
 while (exitProgram == false)
 {
Console.Write("Type a Command!: "); // Here I would use Console.WriteLine() but that is just personal preference.
cmd = Console.ReadLine (); 
 if (cmd == "normColor")
 {
 Console.ForegroundColor = normColor;
Console.WriteLine("The color has been changed");
 } else if (cmd == "cmdColor") {
Console.ForegroundColor = cmdColor;
Console.WriteLine("The color has been changed");
} else if (cmd == "quit") {
ecitProgram = true;
} else {
Console.WriteLine ("Command Unknown")
}
 Console.WriteLine("Executed!");
 // Random randomObject = new Random();
 // Console.WriteLine("{0}", randomObject.Next(10 + 1));
 }
 }
 }
}
From Archie

 
Is an example that will work, but I am not 100 % sure what you want to accomplish. In my opinion you should probably give yourself a task that you want to accomplish like writing a calculator that can do different calculations and stay on it until you think it is done. Don’t make it too hard, though.

Show more replies
  • Liked by
Reply
Cancel