Console Application
-
Hi, I have little experience writing code, so your help is much needed, and will be really appreciated....please help!! I need to build a CONSOLE application, written in C#, It should cycle thru the states as shown: Stop -> Prepare to Go, Prepare to Go -> Go, Go -> Prepare to Stop. Thanks...in advance __________________________________________ This is what I have come up with till now, but it seems wrong: using System; namespace traffic_light { /// /// Summary description for Class1. /// enum State { prepare_to_go, go, prepare_to_stop, stop, } class Class1 { private State _state; public Class1() { _state = State.prepare_to_go; Console.WriteLine ( _state ); _state = State.go; Console.WriteLine ( _state ); _state = State.prepare_to_stop; Console.WriteLine ( _state ); _state = State.stop; Console.WriteLine ( _state ); } } }
-
Hi, I have little experience writing code, so your help is much needed, and will be really appreciated....please help!! I need to build a CONSOLE application, written in C#, It should cycle thru the states as shown: Stop -> Prepare to Go, Prepare to Go -> Go, Go -> Prepare to Stop. Thanks...in advance __________________________________________ This is what I have come up with till now, but it seems wrong: using System; namespace traffic_light { /// /// Summary description for Class1. /// enum State { prepare_to_go, go, prepare_to_stop, stop, } class Class1 { private State _state; public Class1() { _state = State.prepare_to_go; Console.WriteLine ( _state ); _state = State.go; Console.WriteLine ( _state ); _state = State.prepare_to_stop; Console.WriteLine ( _state ); _state = State.stop; Console.WriteLine ( _state ); } } }
You're not too far away. Since you're obviously trying to learn something new, I'll provide leading questions rather than actual answers. 1) How many times is it supposed to cycle through these states? 2) How would you write code to do something that many times? 3) Does the code you have successfully perform one complete state change cycle? 4) (Less important) Would a separate class method (not the constructor) be a better place to do this work? John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -
Hi, I have little experience writing code, so your help is much needed, and will be really appreciated....please help!! I need to build a CONSOLE application, written in C#, It should cycle thru the states as shown: Stop -> Prepare to Go, Prepare to Go -> Go, Go -> Prepare to Stop. Thanks...in advance __________________________________________ This is what I have come up with till now, but it seems wrong: using System; namespace traffic_light { /// /// Summary description for Class1. /// enum State { prepare_to_go, go, prepare_to_stop, stop, } class Class1 { private State _state; public Class1() { _state = State.prepare_to_go; Console.WriteLine ( _state ); _state = State.go; Console.WriteLine ( _state ); _state = State.prepare_to_stop; Console.WriteLine ( _state ); _state = State.stop; Console.WriteLine ( _state ); } } }
-
The error I receive when I run the above piece of code is: 'ConsoleApplication1.exe' does not have an entry point defined. I don't know hopw to rectify the above problem, please help!! Thanks...
-
You're not too far away. Since you're obviously trying to learn something new, I'll provide leading questions rather than actual answers. 1) How many times is it supposed to cycle through these states? 2) How would you write code to do something that many times? 3) Does the code you have successfully perform one complete state change cycle? 4) (Less important) Would a separate class method (not the constructor) be a better place to do this work? John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.Hi, Thanks for your assistance, John. It is meant to cycle through these states continuosly...lets say about 5 times. Its meant to pause at each state for a certain amount of time, say about 5 seconds on each state. Could you give me some pointers in sorting this out. Also the code posted above does not run, because during compilation I get this error: "ConsoleApplication1.exe" does not have an entry point defined. Thanks alot for your help...much appreciated!!
-
Hi, Thanks for your assistance, John. It is meant to cycle through these states continuosly...lets say about 5 times. Its meant to pause at each state for a certain amount of time, say about 5 seconds on each state. Could you give me some pointers in sorting this out. Also the code posted above does not run, because during compilation I get this error: "ConsoleApplication1.exe" does not have an entry point defined. Thanks alot for your help...much appreciated!!
Here is one simple way to loop 5 times and pause for 5 seconds each time. Hope this gets you going in the right direction.
for(int i = 0; i < 5;i++)
{
// Sleep for 5 seconds.
System.Thread.Sleep(5000);
}- Nick Parker
My Blog -
The error I receive when I run the above piece of code is: 'ConsoleApplication1.exe' does not have an entry point defined. I don't know hopw to rectify the above problem, please help!! Thanks...
hi, here is everything u need ;-) i hope it helps u!
using System; using System.Threading; // needs to use Thread class namespace ConsoleApplication1 { // the class with main function class Class { /// /// The main entry point for the application. /// static void Main() { // create new Traffic_light object Traffic_light t_l = new Traffic_light(); // loop 10 times for(int i = 0; i < 10; i ++) { t_l.ShowState(); // show current state Thread.Sleep(5000); // wait 5 seconds } // show exit msg. Console.WriteLine("press enter to exit"); Console.ReadLine(); } } // the class u need class Traffic_light { // array with ur states private string[] Array; // index of currrent state private int State; // Constructor to set up default values public Traffic_light() { State = 0; Array = new string[] { "prepare to go", "go", "prepare to stop", "stop" }; } // function u need to show current state // new call - new state public void ShowState() { State %= Array.Length; // same as: State = State % Array.Length; Console.WriteLine(Array[State ++]); // same as: Console.WriteLine(Array[State]); State = State + 1; } } }
output u see: ==================== prepare to go go prepare to stop stop prepare to go go prepare to stop stop prepare to go go press enter to exit ==================== best regards !:)