Question Why is this not working?

StarJ3M

New member
Joined
Dec 8, 2021
Messages
1
Programming Experience
Beginner
Hello! Im a new and beginner coder in C#, Why is this not working? Im not seeming to get any errors but whenever I Input a number I don't get a response.

C#:
using System;

namespace C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int specialNumber = 7;

            Console.WriteLine("Pick a number from one to 10...");

            int chosenNumber = Convert.ToInt32( Console.ReadLine());

            if (chosenNumber == specialNumber)
            {
                Console.WriteLine("Correct! Time for you to advance to the next level...");

                Console.WriteLine("<({[Level 2 ]})>");
            }
            else
            {
                Console.WriteLine("Incorrect! try again!");
            }
        }
    }
}
 
Please provide a FULL and CLEAR explanation of the problem. Describe EXACTLY what you're trying to achieve (we shouldn't have to work it out form code that doesn't actually achieve it), how you're trying to achieve it (the code does that part), what happens when you try and how that differs from your expectation. Have you actually debugged that code, i.e. set a breakpoint at the top and stepped through it line by line while examining the state at each step? That's how a developer sees what his code is actually doing, rather than just acting like a regular user and watching the app from the outside.

It's impossible for that code to not provide some sort of response because you're writing on both branches of an if/else. You have nothing after that though, so the app will just continue, complete and exit before you can see anything. Unless you place a breakpoint at the end of the code, you will need a call to Console.ReadLine or the like, in order to pause execution long enough for you to actually see the output.
 
For future reference, please provide a meaningful thread title. Yours could describe the vast majority of threads on this site so is pretty much useless.
 
I suspect that the console window is closing immediately when the program ends and so he thinks he is not getting any response. He should try running without the debugger (Ctrl-F5) or run the program at the command line.
 

StarJ3M, Hello!​

Your program is working fine. Without any errors.​

Solution: try to rebuild your project again. (Create a new console project, insert this code without any changes, compile and check it).​

Solution: also I recomend Console.ReadKey(); at the end of your code;​

C#:
using System;

namespace C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int specialNumber = 7;

            Console.WriteLine("Pick a number from one to 10...");

            int chosenNumber = Convert.ToInt32(Console.ReadLine());

            if (chosenNumber == specialNumber)
            {
                Console.WriteLine("Correct! Time for you to advance to the next level...");

                Console.WriteLine("<({[Level 2 ]})>");
            }
            else
            {
                Console.WriteLine("Incorrect! try again!");
            }
            
            Console.ReadKey();
        }
    }
}

I did it!​

These are the simplest ways to solve your problem.​

 

Attachments

  • 1.JPG
    1.JPG
    17.3 KB · Views: 12
  • 2.JPG
    2.JPG
    14.2 KB · Views: 16
Last edited by a moderator:
If his program is working fine, why does he need to change the code by adding your Console.ReadKey(). It's either broken or it's not broken. If it's not broken, the why does he need to change it?
 

Latest posts

Back
Top Bottom