Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My exercise is focus of this program is mainly on looping and repetition.
I have to create a menu and then to choose p,s,d or x.

My problem is that when Im entering a non valid data , the program doesn't respond as I wanted to.

I would really appreciate it if someone runs the code and help me to understand what I'm missing.

SOLVED.
Posted
Updated 22-Aug-12 16:07pm
v3
Comments
malakas1821 22-Aug-12 5:34am    
Yeah you right about that. Thanks,
I.explore.code 22-Aug-12 5:34am    
Please don't the whole code, ask specific questions. Anyway, this line: choise = (char)Console.Read(); should be at the end of the menu and get rid of the vtemp = Console.ReadLine(). In its present form, your code doesn't get a chance to display the menu at all because your programs waits for user input first and as soon as it recieves it, jumps to the switch...case. Also, the menu should be outside the do...while loop not inside. You need to display the menu only once.
malakas1821 22-Aug-12 5:37am    
I need the menu on the loop because when im finished editing for example the personal details , I should go back to menu and choose s,d or x.
ridoy 22-Aug-12 6:20am    
seems to be a homework.
malakas1821 22-Aug-12 6:21am    
Yes it is a h/w .

try rewriting your code this way (i m not doing the full code for you, but only a sample):

C#
Console.WriteLine("----------Menu:----------");
            Console.WriteLine("Enter personal details (p)");
            Console.WriteLine();
            Console.WriteLine("Enter salary details (s)");
            Console.WriteLine();
            Console.WriteLine("Calculate and display (d)");
            Console.WriteLine();
            Console.WriteLine("Exit (x)");
            Console.WriteLine();
            Console.Write("Enter p,s,d or x to proceed :");
            Console.WriteLine();

  do
            {
                choise = (char)Console.Read();
                switch (choise)
                {

                    case 'p':

                        Console.Clear();
                        Console.WriteLine("You choose to enter persons data :");
                        Console.WriteLine();
                        Console.WriteLine("Enter your full name >");
                        full_name = Console.ReadLine();

                        while (string.IsNullOrWhiteSpace(full_name))
                        {
                            Console.WriteLine("Please enter correct data.");
                            full_name = Console.ReadLine();
                        }
...


Menu should be outside the main loop (unless you want to display it everytime) and use while loops inside the main loop rather than do...while loop. Give this a go.This should take care of invalid data to some extent.
 
Share this answer
 
Comments
malakas1821 22-Aug-12 5:49am    
When I press 'p' and then when Im about to add persons data. It says : Please enter your full name
Please enter correct data
I dont want the "Please enter correct data" there. I want it when it's something wrong with the input data.
I.explore.code 22-Aug-12 6:04am    
change this: choise = (char)Console.Read(); to choise = char.Parse(Console.ReadLine()); the problem is the way Read() works which ReadLine() takes care of, it reads everything until you hit "Enter".
I.explore.code 22-Aug-12 6:24am    
swtich(...)
{
case(...):
...;
break;
case(...):
...;
break;
default:
...;
break;
}
malakas1821 22-Aug-12 6:31am    
Like that ?
default:

Console.WriteLine("Please enter correct data");
Console.ReadLine();
break;
I.explore.code 22-Aug-12 6:36am    
no! i thought you were asking how to add a default case in general. I didn't mean for you to use to validate your inputs. In its current form, your code seems to validate fine surely there can be a better way of doing it, but since it works as of now i won't touch there.Just change choise = (char)Console.Read(); to choise = char.Parse(Console.ReadLine());, this would cause the error message to stop coming up even if the data is right.
You have not put a case for 'x' and put a default case to handle the non valid data

Otherwise outside the do while loop put the code you want to execute in case of non valid entry.
 
Share this answer
 
Comments
malakas1821 22-Aug-12 5:32am    
I put x there : while (choise == 'p' | choise == 's' | choise == 'd' | choise != 'x');

because x = exit.
malakas1821 22-Aug-12 6:13am    
I took care of this but regardless of what I'm inputting it displays the error , even if the input is valid.
I.explore.code 22-Aug-12 6:17am    
Read my last comment, that should fix your problem
malakas1821 22-Aug-12 6:19am    
how exactly should I put default case ?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900