12 Replies - 9901 Views - Last Post: 14 May 2008 - 11:01 PM Rate Topic: -----

#1 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Using Largest to find the biggest number entered

Post icon  Posted 28 April 2008 - 12:35 PM

I know it has to be something really small that I'm missing but for some odd reason i can't find it. The problem with my code is that the WHILE isn't working like is suppose to. It's not counting all 10 entries it just counts 1.

If someone can find where the error is, it would be of great help. Thank you guys in advance.


		private void button1_Click(object sender, EventArgs e)
		{

			int largest = 0;
			int number = 0;
			int counter = 1;
			

			//EXECUTIING BUTTON

			number = int.Parse(txt_Cant.Text);

			
			while (counter < 10)
			{
				
				
				if (number >= largest)
					  {
					largest = number;
					  }
				 else 
					 {
					largest = largest;
					 }


				 lbl_Mejor.Text = largest.ToString();
				 txt_Cant.Focus();
				 txt_Cant.SelectAll();
				 counter = counter + 1;
								
			}
		}




Is This A Good Question/Topic? 0
  • +

Replies To: Using Largest to find the biggest number entered

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Using Largest to find the biggest number entered

Posted 28 April 2008 - 01:11 PM

For this I think I would use a for loop, I would also set the value of the label and textbox outside the loop


for(int i=0; i< 10; i++)
{
     if(number >= largest)
     {
           largest = number;
     }
     else
      }
            largest = largest;
       }
}    

//now set the values of the GUI items
lbl_Mejor.Text = largest.ToString();
txt_Cant.Focus();
txt_Cant.SelectAll();      


Was This Post Helpful? 0
  • +
  • -

#3 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: Using Largest to find the biggest number entered

Posted 28 April 2008 - 11:44 PM

For-loop would be my style also.

But otherwise, it would be easier to help if you told us what you are trying to do. Supposedly selecting the largest value from the ones in the TextBox? How is the data laid out in the TextBox - divided with space (4 12 3...), comma (1,15,32...) or how?
Was This Post Helpful? 0
  • +
  • -

#4 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 29 April 2008 - 06:52 AM

It should ask the user 10 times to enter a value, not to enter all the values at once. Psyco I used the code that you provided, but it just gives me 0 for the largest number everytime. It's getting the starting value of the variable.
....I'm a noob sorry.

private void button1_Click(object sender, EventArgs e)
		{
			int largest = 0;
			int number =0;


			for (int i = 0; i < 10; i++)
			{
				if (number > largest)
				{
					largest = number;
				}
				else
				{
					largest = largest;
				}
			} 

		//LARGEST TO TXTBOX
		lbl_Mejor.Text = largest.ToString();
		txt_Cant.Focus();
		txt_Cant.SelectAll();	  
		
		}



Was This Post Helpful? 0
  • +
  • -

#5 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Using Largest to find the biggest number entered

Posted 29 April 2008 - 07:22 AM

you need more code. Where is the input value from the user stored. This code just replaces a number with the larger number if that number is smaller that the Largest number. since you declare and initialize int largest = 0; and int number =0; and never change there value 0 will always be the largest number.
Was This Post Helpful? 0
  • +
  • -

#6 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 29 April 2008 - 07:41 AM

thank you, I will look into it.


Kenji
Was This Post Helpful? 0
  • +
  • -

#7 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 29 April 2008 - 11:53 AM

ya I ate the
number = int.Parse(txt_Cant.Text);



which is the input from the user. :S
Was This Post Helpful? 0
  • +
  • -

#8 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 30 April 2008 - 08:48 AM

I have another question. Right now the loop its working fine, but I can only enter 1 number. It's there a way that I can pause it and let the user enter another value. I know I'm asking kind of like the same question again, but it's eating my brain :blink: .


		private void button1_Click(object sender, EventArgs e)
		{

			
			int largest = 0;
			int number = int.Parse(txt_Cant.Text);
						
						
				for (int i = 0; i < 10; i++)
			
			{
				if (number > largest)
				{
					largest = number;
				}
				else
				{
					largest = largest;
				}
				txt_Cant.Focus(); txt_Cant.SelectAll();
				//MessageBox.Show("Ingrese el siguiente");
			
			} 

		//LARGEST TO TXTBOX
		lbl_Mejor.Text = largest.ToString();
		txt_Cant.Focus();txt_Cant.SelectAll();	  
		
		}



Was This Post Helpful? 0
  • +
  • -

#9 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Using Largest to find the biggest number entered

Posted 30 April 2008 - 09:08 AM

No. you can pause a loop, but you can't enter the loop again without starting over. What you can do in create more text boxes and handle 2nd and 3rd values that way.
Was This Post Helpful? 0
  • +
  • -

#10 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 30 April 2008 - 09:30 AM

:( i thought I could do all from the same textbox. Thank you Zak
Was This Post Helpful? 0
  • +
  • -

#11 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: Using Largest to find the biggest number entered

Posted 05 May 2008 - 05:42 AM

Well you can do the whole thing with single TextBox, but it gets tricky. You have to save the previous numbers somewhere while asking more values until the tenth is read. After that you can loop those numbers and find the largest the same way you have already done.

I don't know if this helps at all, but I hope so.
Was This Post Helpful? 0
  • +
  • -

#12 kenji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 07

Re: Using Largest to find the biggest number entered

Posted 14 May 2008 - 02:23 PM

So, it would be like for example saving the info into a .txt and then reading the info from it?
Was This Post Helpful? 0
  • +
  • -

#13 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: Using Largest to find the biggest number entered

Posted 14 May 2008 - 11:01 PM

You don't need to save entered values in any textfile - a simple class variable will do.
Something like this:
public class MainForm {
	private List<int> numbers = new List<int>(10);
	...
	private void btnAddNum_Click(object sender, EventArgs e)
	{
		numbers.Add( int.Parse(txt_Cant.Text) );
		if (numbers.Length == 10) {
			// Do the largest comparison, clear list, etc
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1