Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I'm a beginner in C#.I wanted to create a basic program in which we add numbers repeatedly.I was successful but I also wanted to make it more simple by not displaying the line "The current answer is xxx" every time I enter a number.I wanted the text to stay in just one place and only the number to change when we have a new answer.That's why I wrote the line Console.Write(@"") but it doesn't seem to work.Please tell me if I'm doing something wrong and please tell me the correct code.


C#
namespace Tutorial1
{
    class Program
    {
        static void Main(string[] args)
        {
            int answer, current;
            string currenttranslated;

            answer = 0;
            do
            {
                currenttranslated = Console.ReadLine();
                current = int.Parse(currenttranslated);
                answer = current + answer;
                    Console.Write(@"The current answer is {0}",answer);
                Console.WriteLine();
            }
            while (0 == 0);

        }
    }
}
Posted
Updated 11-Jul-14 22:56pm
v2

This can actually get quite complicated. Generally when you use Console.Write(Line) you are outputting to the process' standard output stream - something which has been around on every platform since the dawn of time. So as such, what you want isn't generally a good idea.

For instance, you can redirect input and output from you application to files to work in a batch manner without user interaction.

The Console class does contain quite a bit of stuff for manipulation of text within a console window though. Probably the easiest way to do this would be to issue a Console.Clear() in your loop so that the window starts afresh each time.

Otherwise, you can get involved with determining the window size, then positioning the cursor prior to writing but then you get into the world of blanking stuff out etc.

Have a look at the class reference for Console if you need more info.
 
Share this answer
 
Hi swapnil999

You can try to clear the console before Input like this:

C#
class Program
 {
     static void Main(string[] args)
     {
         int answer, current;
         string currenttranslated;

         answer = 0;
         do
         {
             Console.Write("Enter new number: ");
             currenttranslated = Console.ReadLine();
             Console.Clear();
             current = int.Parse(currenttranslated);
             answer = current + answer;
             Console.Write(@"The current answer is {0}", answer);
             Console.WriteLine();
         }
         while (true);
     }
 }


Also think about a possibillity to exit the program (no endless loop), maybe if a non valid number is entered? You could check with int.TryParse and exit the loop
happy coding

Johannes
 
Share this answer
 
It's not that simple, I'm afraid!
The console is pretty basic - it emulates a very old device called a teletype where a roll of paper was printed on, so you can't "go back" to the previous line and erase the old info and write new!

Fortunately, you can do it with the Console - but not via Write.
You need to use Console.SetCursorPosition[^] instead, then write.
 
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