Click here to Skip to main content
15,888,157 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
static void Main(string[] args)
       { string continue1 = "yes";



           while (continue1 == "yes")

           {
               Console.WriteLine("your name?:");
               string jmeno = Console.ReadLine();
               Console.WriteLine(jmeno + " is the best!");

               Console.WriteLine("New name? (yes/no)");
               if (continue1 == "yes")

                   continue1 = Console.ReadLine();


               // the following doesn´t work

              else Console.WriteLine("What a shame :(");

           }
           Console.ReadKey();
       }


What I have tried:

I think I don´t properly understand how does "else" work.. otherwise it would have worked :D Please help, I want to learn the way this works :(
I want the program to write "what a shame" when u Write "no" (or anything else - thats how I feel "else" works), but no success, program just stops.
Posted
Updated 15-Sep-16 0:56am
v2
Comments
pkfox 15-Sep-16 5:49am    
What do you mean by it doesn't work ?
NoobisCoding 15-Sep-16 6:01am    
I want the program to write "what a shame" when u Write "no" (or anything else - thats how I feel "else" works), but no success, program just stops.
NoobisCoding 15-Sep-16 6:00am    
Solution 1 = visual studio debugger doesnt know what do I want so he won´t work..
Solution 2 = tried, still no success .. I wthe program works well when i write "yes", but I want the program to write "what a shame" when u Write no (or anything else - thats how I feel "else" works)

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

try to read the answer before the if.
 
Share this answer
 
Comments
NoobisCoding 15-Sep-16 6:50am    
YEP, readline answer before if :) nice job mate
You should format your if\else like this

C#
if (continue1 == "yes")
{
    continue1 = Console.ReadLine();
}
else
{
    Console.WriteLine("What a shame :(");
}


It avoids ambiguity.
 
Share this answer
 
Comments
NoobisCoding 15-Sep-16 6:09am    
unfortunately no success :(
F-ES Sitecore 15-Sep-16 6:33am    
There is a logic issue with your code, step through it line by line in the debugger to work out why it is behaving how it is.
Afzaal Ahmad Zeeshan 15-Sep-16 6:49am    
5ed for a good tip.
check the comments i made in the Code

static void Main(string[] args)
       { string continue1 = "yes";
 

 
           while (continue1 == "yes")
 
           {
               Console.WriteLine("your name?:");
               string jmeno = Console.ReadLine();
               Console.WriteLine(jmeno + " is the best!");
 
               Console.WriteLine("New name? (yes/no)");
               continue1 = Console.ReadLine(); //read the user input before checking it with the if statment
               if (continue1 == "yes")
               {
                   //do something hwne the input is yes or in this case repeat the loop
               }
              else Console.WriteLine("What a shame :(");
 
           }
           Console.ReadKey();
       }
 
Share this answer
 
Comments
NoobisCoding 15-Sep-16 6:39am    
oh, I thought the continue1 = Console.ReadLine(); is the think which is repeating .. so .. could u give me an advice what to write? ("//do something hwne the input is yes or in this case repeat the loop") how to repeat the loop in this case please? :)
NoobisCoding 15-Sep-16 6:44am    
LOL, I left the if { } empty and the program works how it should :O how come? :O
try this

C#
Console.WriteLine("New name? (yes/no)");
           continue1 = Console.ReadLine();
           if (continue1 == "no")
               Console.WriteLine("What a shame :(");
           else
               continue1 = "yes";
 
Share this answer
 
Comments
NoobisCoding 15-Sep-16 7:15am    
this one is unnecesary... i would have to define "no" + "else" has no loop inside
Karthik_Mahalingam 15-Sep-16 7:20am    
ok.
OK this is how it works.. but why does it work even when i left if { } EMPTY? :o

C#
static void Main(string[] args)


        {
            string continue1 = "yes";

            while (continue1 == "yes")

            {
                Console.WriteLine("your name?:");
                string jmeno = Console.ReadLine();
                Console.WriteLine(jmeno + " is the best!");

                Console.WriteLine("New name? (yes/no)");
                continue1 = Console.ReadLine();

                if (continue1 == "yes")
                {
                    

                }



                

                else

                    Console.WriteLine("What a shame :(");

            }
            Console.ReadKey();

        }
 
Share this answer
 

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