2 Replies - 980 Views - Last Post: 08 October 2012 - 09:07 PM Rate Topic: -----

#1 DavisonH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-April 11

Input string was not in the correct format

Posted 08 October 2012 - 07:53 PM

Upon running the program below, coded using Visual Studio 2010, I received the error "Input string was not in the correct format". I have attempted to fix it for some time now, but I fail to understand just why I receive that message.

The program is simple. The user enters the amount of tickets sold for each class of tickets. There is class A, B, and C. Each ticket costs a certain amount, and the only calculation done is multiplying the amount they enter by the cost of the ticket. Finally, the result of each of these calculations is added together for the total cost of all tickets combined.

1) The user input is collected in a textbox in the string format. For the purpose of calculations, I converted it to decimal.

2)After the calculations for the three numbers entered have been made, it is added together for the total cost. Then, I converted each of the results to a string, formatted them to be displayed in $ format, and finally, print them in the four boxes.

I do not understand why I receive the error message once the numbers are calculated. The code is below. Thank you for the help.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            try
            {
                //Stores the user input for amount of tickets sold
                string boxA;
                string boxB;
                string boxC;

                //Stores the value of the tickets, converted from string to double
                decimal ValueA;
                decimal ValueB;
                decimal ValueC;

                //Stores the total combined value of each class of ticket sold
                decimal boxAtotal;
                decimal boxBtotal;
                decimal boxCtotal;
                decimal abctotal = 0;

                //Value of each ticket
                decimal boxAticket = 15;
                decimal boxBticket = 12;
                decimal boxCticket = 9;

                //Stores the final value of the tickets sold of each class to convert to the proper format for output
                string SeatA;
                string SeatB;
                string SeatC;
                string SeatsTotal;

                //Calculates total value of tickets sold and total value

                decimal Box1;
                decimal Box2;
                decimal Box3;

                //Takes user input
                boxA = boxAtext.Text;
                boxB = boxBtext.Text;
                boxC = boxCtext.Text;

                //Converts input to decimal for calculations
                ValueA = Convert.ToDecimal(boxA);
                ValueB = Convert.ToDecimal(boxB);
                ValueC = Convert.ToDecimal(boxC);

                //Calculates the total value of tickets sold
               
                
                boxAtotal = ValueA * boxAticket;
                Box1 = boxAtotal;

                boxBtotal = ValueB * boxBticket;
                Box2 = boxBtotal;

                boxCtotal = ValueC * boxCticket;
                Box3 = boxCtotal;

                abctotal = Box1 + Box2 + Box3;

                
                //Converts the value to $ format
                SeatA = String.Format("{0:C}", boxAtotal);
                SeatB = String.Format("{0:C}", boxBtotal);
                SeatC = String.Format("{0:C}", boxCtotal);
                SeatsTotal = String.Format("{0;C}", abctotal);
                

                //Displays final values in text box
                totalA.Text = SeatA;
                totalB.Text = SeatB;
                totalC.Text = SeatC;
                totalABC.Text = SeatsTotal;
                        
 }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            boxAtext.Text = "";
            boxBtext.Text = "";
            boxCtext.Text = "";
            totalA.Text = "";
            totalB.Text = "";
            totalC.Text = "";
            totalABC.Text = "";

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Input string was not in the correct format

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5519
  • View blog
  • Posts: 11,827
  • Joined: 02-June 10

Re: Input string was not in the correct format

Posted 08 October 2012 - 08:33 PM

Take a look at the other threads from today - several students must have the same homework as there are 3 questions on this from today alone.

Don't use a textbox for numeric input. "Yogi bear" cannot be converted to a number like 5. So replace your textboxes with numericUpDown controls and use the .Value property.

http://www.dreaminco...1&#entry1718704

Another example of mis-using a textbox when another tool is more correct:
http://www.dreaminco...1&#entry1718536
Was This Post Helpful? 0
  • +
  • -

#3 DavisonH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-April 11

Re: Input string was not in the correct format

Posted 08 October 2012 - 09:07 PM

Thank you for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1