Thread: Help x.x

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Help x.x

    This code makes it multiply by .65 every time. Can someone explain to me why it's doing this?

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                double NumberOfPieces;
                double EarnedPay = 0;
    
                NumberOfPieces = Double.Parse(PiecesTextBox.Text);
    
                if (NumberOfPieces > 0 && NumberOfPieces <= 199 )
                {
                    EarnedPay = NumberOfPieces * .50;
                }
                if (NumberOfPieces >= 200 && NumberOfPieces <= 399)
                {
                    EarnedPay = NumberOfPieces * .55;
                }
                if (NumberOfPieces >= 400 && NumberOfPieces <= 599)
                {
                    EarnedPay = NumberOfPieces * .60;
                }
                if (NumberOfPieces <= 600)
                {
                    EarnedPay = NumberOfPieces * .65;
                }
    
                EarnedTextBox.Text = EarnedPay.ToString("C");

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    (NumberOfPieces <= 600)
    everything is less than 600 before it, maybe you meant to put >= ?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    That was it! Thank you very much! .

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    This is how I took care of the crash and burn of my program if they entered a letter where a numeric value would go but I don't know how to set one up for the NameTextBox.

    What would the code be for making them type only letters into a text box and give them an error if they put numbers?

    Code:
                try
                {
                    NumberOfPieces = int.Parse(PiecesTextBox.Text);
                }
                catch (FormatException) { MessageBox.Show("Please enter a Numeric Value"); }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A better way might be to use int.TryParse(). Try this:
    Code:
                string text;
                int num;
    
                do
                {
                    Console.Write("Enter some text (no numbers): ");
                    text = Console.ReadLine();
                } while (text.Any(c => Char.IsDigit(c)));
    
                do
                {
                    Console.Write("Enter a number: ");
                } while (!int.TryParse(Console.ReadLine(), out num));
    
                Console.WriteLine("The text you entered was: {0}", text);
                Console.WriteLine("The number you entered was: {0}", num);
    If you don't like lambdas or LINQ (you'd have to be crazy not to!) you can always make a ContainsDigit() method that looks like:
    Code:
            private static bool ContainsDigit(string str)
            {
                foreach (char c in str)
                    if (Char.IsDigit(c))
                        return true;
                return false;
            }
    And then you just change the do/while loop to: while(ContainsDigit(text));
    Last edited by itsme86; 05-02-2011 at 08:23 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    My program isn't a console though man. Its a form. I think i can use this though =). Thank you :P.

Popular pages Recent additions subscribe to a feed