Search code examples
c#.netconsoleconsole-application

Add new value to existing value in C# Console app


I'm new to c# console and currently have a Console application which asks users to buy tickets, after selecting the tickets and amount, the app shows the total price and asks the user if they want to add more tickets, if the users enters y, they can add more tickets, how can I add the new total price with the existing total price?

The code works if I press N first time, but I'm not sure what to put to after pressing N the second, or third time.

Any help is appreciated!


Solution

  • If I understood your problem correctly you just want to add a value to totalprice in every iteration.

    This can be done in two different ways.

    First you nedd to initialize totalprice with 0.

    totalprice =0;
    

    Then everytime you want to add something to totalprice you either assign the value directly to it with:

    totalprice = totalprice + (subTotal * ADDITIONAL_TAX) + subTotal;
    

    Or the nicer solution. You use the += Operator. The += Operator is essentially a shortform for what I did above.

    totalprice += (subTotal * ADDITIONAL_TAX) + subTotal;