954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

while loop

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class While
    {
        private double sum;

        public void Star()
        {
            WriteProgramInfo();
            ReadInPutAndSumNumbers();
            ShowResults();
        }
        private void ReadInPutAndSumNumbers()
        {
            double num = 0.0;
            bool done = false;
            while (!done)
            {
                {

                }
            }
        }
        private void WriteProgramInfo()
        {
            Console.WriteLine("\n\n ++++++ Finnish input type 0 ++++++");

        }
        private void ShowResults()
        {
        }

    }

}


hi.

I cant figuer out how to do this.

When the program start i can put in as meny numbers that i want in to it until i put in 0 then it will finnish and i can sum up the values but i dont know how im going to do it. i know how to do while loops but not to to like this whit out saying how many times it going to put it out

so can any one explane to my how to do it? its a school work but i dont need to send it in but i need to learn how to do it if im going to do same things like this in the future

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Did you delete your Main() method?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

no but i only have this in it:

While obj = new While();
obj.Star();

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Look at something like this:

using System;

namespace DW_409519_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         string strData = "";
         int intData = 0;

         while (!strData.Equals("0"))
         {
            Console.WriteLine("Enter a number.  Enter 0 to quit");
            strData = Console.ReadLine().Trim();

            if (strData.Equals("0"))
            {
               continue;
            }

            if (!int.TryParse(strData, out intData))
            {
               Console.WriteLine("Not a good number: " + strData);
               continue;
            }

            Console.WriteLine("Doing something with: " + strData);
            //Add code here to do something...
         } 
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

thx for it but om going only to put it in my While class and only have
While obj = new While();
obj.Star();

in the main

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

thx for it but om going only to put it in my While class and only have While obj = new While(); obj.Star();

in the main

He wasn't writing your program for you. He was giving you a very simple and clear implementation of how your program is supposed to function. The simplest way for him to give you a complete program was to implement the whole thing in Main().

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class While
{
private double sum=0;

public void Star()
{
WriteProgramInfo();
ReadInPutAndSumNumbers();
ShowResults();

}
private void ReadInPutAndSumNumbers()
{
double num = 0.0;
bool done = false;
while (!done)
{
Console.WriteLine("Enter the Number:");
try
{
num = Convert.ToDouble(Console.ReadLine().Trim());
sum += num;
if (num == 0)
{
done = true;
}
}
catch
{
Console.WriteLine("Enter An Valid value");
}
}
}
private void WriteProgramInfo()
{
Console.WriteLine("\n\n ++++++ Finnish input type 0 ++++++");

}
private void ShowResults()
{
Console.WriteLine("\n\n ++++++ Finnished output type "+sum.ToString() +" ++++++");
}

}

}

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: