assistance with moving through loops

G

gordon

Hi

I have some code that will prepare a text file that can be read by another
application. The code is supposed to collect information on the starting
point of a range, then end point of the range and the interval to create
categories. For example if the start is 5 and the end is 25 and the
interval is 5, then the application should return a text file with the body
as follows:

<name>
5 - 10 ='5 to 10'
10-15='10 to 15
15-20='15 to 20'
20-25='20 to 25'
......

I am having trouble getting the interval to work as i dont fully understand
the complexity(or simplicity) of the loop. I appear to be putting bandaids
on errors and getting my code to complex. Could someone help me to simplify
this and I will hopefully learn a bit more about the program logic.
My form has 4 fields in test boxes - tbName, tbStart, tbEnd, tbInterval
private void btnOK_Click(object sender, System.EventArgs e)

{

if (int.Parse(tbStart.Text)>int.Parse(tbEnd.Text))

MessageBox.Show("The value in the End box is less than the Start");


double Vend =System.Convert.ToDouble(tbEnd.Text);

double Interval=System.Convert.ToDouble(tbInterval.Text);

double Start=System.Convert.ToDouble(tbStart.Text);

double Ends=System.Convert.ToDouble(tbEnd.Text);

double counter=(Vend/Start)-Interval;



if ((Vend-Start) %Interval>0 )

MessageBox.Show("The interval is not a readily divisible into the End
value");

string fmtname=tbName.Text.ToString();

string fmtstart=tbStart.Text.ToString();

string fmtend=tbEnd.Text.ToString();

double starts=Convert.ToDouble(fmtstart);

double ends=Convert.ToDouble(fmtend);

double first =starts+Interval;

using (StreamWriter sw = new StreamWriter(string.Format(@"c:\{0}.txt",
fmtname)))

{

sw.WriteLine("Proc format; ");

sw.WriteLine("value {0}",fmtname) ;


sw.WriteLine("{0} - {1} = '{0} to {1}'",starts, first);

for (int i = 1; i <= counter; i++)

{

starts=starts+Interval;

ends=starts+Interval;

sw.WriteLine("{0} - {1} = '{0} to {1}'",starts, ends);

}

sw.WriteLine("; ");

sw.WriteLine("run;");

MessageBox.Show(string.Format("File written to {0}.txt", fmtname));

}

}



when i run this i only get one element output in the file.



Any help appreciated - especially pointing out which bits i can do better.



thanks



Doug
 
A

Aboulfazl Hadi

Hi Gordon
you can use the following code :

int end = 30;
int start = 10;
int interval = 5;
int count = end / start;
for (int i = start; i < end; i += interval)
MessageBox.Show (string.Format("{0} - {1} = '{0} to
{1}'", i , i + interval ));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top