need help with code

I am trying to write a program kinda like a receipt and I am having trouble with this.

What I need it to do is look like this:

Please enter the price of the item:

Please enter the quantity of the item:

Please enter the total of the item:

It needs to continue until you want to end it with like(-1) or something.

Any suggestions It would be greatly appreciated. Thanks

Answer this question

need help with code

  • Arnaud H

    absolutely.

    you just have to change the way the inputs are created/prompted/calculated. (reading from console than from a textbox as an example)

    The functionality posted is the same, the only difference - the kind of "platform" from Winforms to console.

     

     



  • Eva Gonzalez

    Now I am getting errors in the beginning where the variables are...
  • Andrew Buyan

    Your first problem is this line:
    int Quanity = Convert.ToDecimal(Quantity);

    You call Convert.ToDecimal, and then store that decimal into an int. You want either
    decimal Quanity = Convert.ToDecimal(Quantity);
    or (more likely)
    int Quanity = Convert.ToInt32(Quantity);

    However, you have a bigger problem. You have "Quantity" on both sides of the assignment. (and a similar problem in each of the other lines). You should be getting error like
    The name "Quantity" does not exist in current context.

    From where are you getting the number you are trying to convert





  • Recep TARAKÇIOĞLU

    what is the error

  • zx5000

    James: they would like it in a console app, not Winforms :-)

     

    are you able to post the entire code

    As said, you are doing something like:

    decimal ItemPrice = Convert.ToDecimal(ItemPrice); //Correct

    int Quanity = Convert.ToDecimal(Quantity); //Wrong

    double Total=Convert.ToDecimal(Total); //Wrong

     

     

    you are declare a variable of type int but you are converting a value to decimal - completely different type therefore you will get the compiler error.

    you also seem to be converting the same declaration variable which does not make sense. You should be taking in the input from user and converting that and storing it to the variable.....

     

    decimal ItemPrice = Convert.ToDecimal(ItemPriceInput);

    int Quantity = Convert.ToInt32(quantityInput);

    double Total = Convert.ToDouble(Quantity * ItemPrice);



  • Poma

    hmm.....its still not working right...I have so far:

    decimal ItemPrice = Convert.ToDecimal(ItemPrice);

    int Quanity = Convert.ToDecimal(Quantity);

    double Total=Convert.ToDecimal(Total);

    Total = (ItemPrice * Quantity)

    There is an error saying cannot convert an integer to a decimal.

    How do I correct this


  • Moim Hossain

    I need to be able to type any number in that field.

    I need to be able to add whatever number to the receipt.


  • Yassi

    it says cannot convert the decimal to an int

    It also says cannot convert decimal to double.

    In each of them It asks if I am missing a cast....


  • AlexBB

    To convert from one type to another, use the Convert class, like I have done in my example.

    As an example, to convert TO decimal:

    Convert.ToDecimal(value)



  • ravindra_pn

    please post the errors and code you are using

  • Expressman

    Will this be a console app or a Winform app

    I will assume its a winform app.

     

    This is easily done.

     

  • Create a new Windows Application project

  • Drag a few labels and textboxes on the form. Labels are descriptors for the textboxes, which are inputs (such as qty etc...)

  • Next, drag a button or so, which will total up the inputs.

     

    On the button click event, make sure you validate your inputs before proceeding to calculate the total. If the validation fails, prompt them to enter the appropriate data in the textboxes, with a MessageBox for example and focusing on that textbox which requires some input.

     

    so you may have something like this in the code:

     



  • //Assume we have successfully validated our input. Then....
    decimal thePrice = Convert.ToDecimal(this.txtPrice.Text);
    int theQuantity = Convert.ToInt32(this.txtQty.Text);
     
    decimal totalPrice = thePrice * theQuantity
     
    this.lblTotalPrice.Text = totalPrice.ToString();

     

     

    remember, that is just a quick example, just to guide you

     

    now, you will then have another button, which will clear the inputs and reset the form, ready for the next inputs.

    This will run until the user quits the application.

    If you need to do a printout of the receipt, you need to take a look on the Printer classes:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemdrawingprintingprintdocumentclasstopic.asp

     

    hope this helps in some way!

     



  • sorsh

    >> I need to be able to type any number in that field.

    You seem to be a bit confused. You can't type anything at all into an int or a decimal value. What you need are TextBox fields, say txtQuantity and txtPrice. Then you would write:

    int Quantity;
    decimal Price;

    void btnSave_Click(object sender EventArgs e)
    {
    Quantity = Convert.ToInt32(txtQuantity.Text);
    Price = Convert.ToDecimal(txtPrice.Text);
    TotalCost = Quantity * Price;
    labelTotal.Text = TotalCost.ToString();
    }



  • iamdon

    decimal ItemPrice = ItemPrice;

    int Quantity= Quantity;

    double = Total

    Total= ItemPrice * Quantity


  • Anu Viswan

    The only thing is that it has to be in Console Application...Is is still along the same lines though
  • need help with code