Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello Experts,
 
Am i doing something wrong here pls? I think i might have a problem in the while loop since all the Console.WriteLine commangs are underlined in red after that line.
 
namespace myDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int option;
            bool state = true;
            public int Option
            {
                get
                {
                    return option;
                }
                set
                {
                    if (value == 0 || value > 16)
                    {
                         Console.WriteLine("Invalid Option");
                    }
                    else
                    {option = value;}
                }
            }
 
            while(state = true)
            {
                Console.WriteLine("Please Enter the Ocupation");
                Console.WriteLine("1. Director");
                Console.WriteLine("2. Councilor");
                Console.Clear();
                Option = Console.ReadLine();
 

                switch(int Option)
                {
                    case 1:
                    {
                        Console.WriteLine("Displaying Director Options:");
                        Console.WriteLine("2. Display Students Details:");
                        Console.WriteLine("3. Display Councilors Details: ");
                        Console.WriteLine("4. Display Visits Details:");
                        Console.WriteLine("5. Add Student:");
                        Console.WriteLine("6. Remove Student:");
                        Console.WriteLine("7. Add Councilor:");
                        Console.WriteLine("8. Remove Councilor");
                        Console.WriteLine("9. Change Councilor Available Hours");
                        Console.WriteLine("10. Add Visit");
 
                    }
 

                }
            }
        }
    }
}
 
Screenshot: http://s2.postimage.org/osh5yy9op/Capture.jpg[^]
Posted 28-Dec-12 5:55am
Edited 28-Dec-12 6:20am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Change
 switch(int Option)
into:
switch (Option)
Also, change
 case 1:
 {
        Console.WriteLine("Displaying Director Options:");
        Console.WriteLine("2. Display Students Details:");
        Console.WriteLine("3. Display Councilors Details: ");
        Console.WriteLine("4. Display Visits Details:");
        Console.WriteLine("5. Add Student:");
        Console.WriteLine("6. Remove Student:");
        Console.WriteLine("7. Add Councilor:");
        Console.WriteLine("8. Remove Councilor");
        Console.WriteLine("9. Change Councilor Available Hours");
        Console.WriteLine("10. Add Visit");
 
  }
into:
 case 1:
        Console.WriteLine("Displaying Director Options:");
        Console.WriteLine("2. Display Students Details:");
        Console.WriteLine("3. Display Councilors Details: ");
        Console.WriteLine("4. Display Visits Details:");
        Console.WriteLine("5. Add Student:");
        Console.WriteLine("6. Remove Student:");
        Console.WriteLine("7. Add Councilor:");
        Console.WriteLine("8. Remove Councilor");
        Console.WriteLine("9. Change Councilor Available Hours");
        Console.WriteLine("10. Add Visit");
        break;
You don't need to use brackets. Don't forget break; before a next case of before the end of the switch.
Read more about switch here:
http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.110%29.aspx[^]
 
[EDIT]
 
Also, change this:
Option = Console.ReadLine();
into this:
try
{
      Option = Convert.ToInt32(Console.ReadLine());
}
catch
{
      Console.WriteLine("Input string isn't in a correct format.");
     continue; // use 'continue' only in a loop
}
 
[EDIT]
 
I'm sorry, but I found a few mistakes after I saw your screen shot.
First:
 public int Option
            {
                get
                {
                    return option;
                }
                set
                {
                    if (value == 0 || value > 16)
                    {
                         Console.WriteLine("Invalid Option");
                    }
                    else
                    {option = value;}
                }
            }
You create a property INTO a function. That's not possible. Read more about properties:
http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx[^]
Second:
Don't use
while (state = true)
Use
while (state == true)
Read more about the == operator:
http://msdn.microsoft.com/en-us/library/53k8ybth.aspx[^]
  Permalink  
v5
Comments
Gilmore Sacco at 28-Dec-12 12:12pm
   
Hello ProgramFOX, Thanks for your quick reply and detailed solotion. I made the suggested solution but still the problem persists. I would like to upload a screenshot but i dont know how :(
ProgramFOX at 28-Dec-12 12:15pm
   
You can't upload a screen shot. If you want to show an image, you need to upload an image on another site and posting a link here.
Gilmore Sacco at 28-Dec-12 12:20pm
   
I have added a screenshot in the question :D
ProgramFOX at 28-Dec-12 12:25pm
   
I updated my answer.
Gilmore Sacco at 28-Dec-12 12:55pm
   
Good bless you sir. I was instruced my my lecturer to always use properties as this can help me control the user input. I did not know that i could not usee it thaht way. I did all the suggestions and it howks great now. Thanks alot and i appreciate alot.
ProgramFOX at 28-Dec-12 12:57pm
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

while (state = true)
is always true, you mean:
while (state == true)
Given the above, and your link, I think you should spend some more time rereading your reference guide on C# expressions and statements.
  Permalink  
Comments
ProgramFOX at 28-Dec-12 12:26pm
   
It's more than that. He created a property into the Main method.
Richard MacCutchan at 28-Dec-12 12:42pm
   
I didn't read beyond that as you seem to have covered all the other pieces.
Gilmore Sacco at 28-Dec-12 12:52pm
   
hey guys a very big thanks to both of you for your support :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,838
1 Tadit Dash 310
2 OriginalGriff 283
3 Sergey Alexandrovich Kryukov 260
4 Peter Leow 220
0 Sergey Alexandrovich Kryukov 9,440
1 OriginalGriff 5,618
2 Peter Leow 4,280
3 Maciej Los 3,540
4 Abhinav S 3,363


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid