0

I have literally started c# today and I have hit a block.

I am following beginner guides on YouTube but I have not found my answer yet.

What I am trying to get is the question below, all on the same line:

"what is * divided by *"


but what I am getting is:

what is * (here at this point I press Return to confirm the digits which I think is my problem)

divided by * (really here is the only place I want to be hitting return)

Start:
  int num03;
  int num04;
  int answer;

  Console.Write ("what is "); 
  num03 = Convert.ToInt32 (Console.ReadLine ()); 
  Console.Write ("divided by "); 
  num04 = Convert.ToInt32 (Console.ReadLine ());

  Console.WriteLine ("");
  Console.WriteLine ("is the answer? ");
  answer = Convert.ToInt32 (Console.ReadLine());

  if (num03 / num04 < answer) {
    Console.WriteLine ("a bit lower next time");
    Console.WriteLine ("");
    Console.WriteLine ("");
    goto Start;
  } else if ( num03 / num04 > answer ) {
    Console.WriteLine ("a bit higher next time");
    Console.WriteLine ("");
    Console.WriteLine ("");
    goto Start;
  } else if ( num03 / num04 == answer ) ; {
    Console.WriteLine ("correct!!! please try another");
    Console.WriteLine ("");
    Console.WriteLine ("");
  }

  goto Start;

I think the hardest part is trying to search the correct phrase for help when I get stuck.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
Jake Ryan
  • 1
  • 1
  • 2
    A tad off topic maybe but I would have a read up on `goto` and maybe consider replacing your `goto` construct with a loop. – petelids Sep 19 '14 at 22:45
  • 2
    In most cases manipulating the Input other than the standard way of using `Return` key, will be the wrong and dangerous way, Specially for a beginner. I will suggest you otherwise. – Orel Eraki Sep 19 '14 at 22:48

3 Answers3

1

Be careful with this requirement, the Return key is a very standard way of entering input. The ReadLine function reads until this character is encountered.

You have two options:

  1. Read the keys in a loop

You would do this by calling ReadKey instead of ReadLine in some sort of while loop. The big problem here is you need some delimiter to determine the end of input, and whatever it is, the user won't be expecting it.

  1. Mess with the console cursor

Not a terrible idea, but also not really a good practice to be trying to learn as a novice. You can do this with Console.SetCursorPosition (MSDN)

Honestly, I would just leave it as is, maybe change your input format so it makes sense on multiple lines. It will be what your users are expecting anyways.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Thank you - i did not expect such a quick response - i have spoken to a friend who suggested trying setcursorposition but i don't think my couple of hours of expertise is ready just yet – Jake Ryan Sep 19 '14 at 22:57
  • @JakeRyan Since you are new, I would learn a few input norms, like a new line terminating the input. You don't need to bother with `SetCursorPosition` because you *dont need to move the cursor* the newlines are fine. – BradleyDotNET Sep 19 '14 at 22:59
0

Take a look at Can Console.Clear be used to only clear a line instead of whole console?

You can clear the last line of the console and print it again with the new information, or be creative with the options available by moving the cursor.

EDIT: I just realized that the problem was much simpler than I had expected, but I will leave this answer here in case it is of use anyway.

Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
0

Extending Bradleys answer with a example:

As you can see the cursor left is set with the length of "What is " plus your answer. Also the top is made 1 higher which compensates for your enter. I wouldn't recommend ReadKey since you have lots of scenarios where you have to catch backspace etc.

Start:
        int num03;
        int num04;
        int answer;

        Console.Write("what is ");

        string input = Console.ReadLine();
        num03 = Convert.ToInt32(input);
        Console.CursorLeft = "what is ".Length + input.Length;
        Console.CursorTop--;
        Console.Write(" divided by ");
        num04 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("is the answer? ");
        answer = Convert.ToInt32(Console.ReadLine());

        if (num03 / num04 < answer)
        {
            Console.WriteLine("a bit lower next time");
            Console.WriteLine("");
            Console.WriteLine("");
            goto Start;
        }
        else if (num03 / num04 > answer)
        {
            Console.WriteLine("a bit higher next time");
            Console.WriteLine("");
            Console.WriteLine("");
            goto Start;
        }
        else if (num03 / num04 == answer) ;
        {
            Console.WriteLine("correct!!! please try another");

            Console.WriteLine("");
            Console.WriteLine("");
        }

        goto Start;
Roy
  • 656
  • 12
  • 28