Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading