// CLR_Console_DataEntry_Part3.cpp : main project file. /* ______________________________________________________ I N T R O D U C T I O N T O D A T A E N T R Y P A R T 3 C++ Version Ron Kessler CLR Console Version Created 12/29/2016 for VS 2015 This project teaches you how to use simple math computations to compute sales tax and how to format the results to look like currency. I introduce the concept of 'casting' which simply means to convert one data type to another. Also shows how to use string formatting style ________________________________________________________ Updated 1/4/2017 Showed how to use Double::Parse instead of Convert class. Also how to use composite string formatting for summary */ #include "stdafx.h" using namespace System; int main(array ^args) { //---define our variables here String^ myTitle = " Welcome to Ron's Golf Store and Simple Calculations\n\n"; String^ whichProduct; double price = 0; double taxRate = .08; double salesTax = 0; double balanceDue = 0; Console::WriteLine(myTitle); //screen welcome msg //Get user input. Remember, all console data is String so we must convert it (cast it) Console::Write ("Enter name of item you wish to purchase: "); whichProduct = Console::ReadLine (); Console::Write ("Enter price of the item: "); //prompt for item purchased //---use the Convert Class to cast our data from string to a double price = Double::Parse(Console::ReadLine ()); //option 1 //price =Convert::ToDouble(Console::ReadLine ()); //option 2 salesTax = price * taxRate; //compute tax balanceDue = price + salesTax; //total due //---now display the results and format as currency //---format numbers using 2 decimals with no $ sign (n2) using differenct styles: Console::WriteLine ("\n\nSummary of Purchase\n\n"); //---composite formatting style Console::WriteLine ("You purchased: {0}. ", whichProduct); Console::WriteLine("The price of your item is {0}.", price.ToString ("n2")); Console::WriteLine ("Your sales tax is {0}.",salesTax.ToString ("n2")); //---concatenation Console::WriteLine ("\n\nYour total purchase is " + balanceDue.ToString ("c2")); //with a $ sign (currency) //---use composite formatting feature style Console::WriteLine ("\n\nYou purchased a: {0}, which cost {1}, and your total is {2}", whichProduct, price.ToString ("c2"), balanceDue.ToString ("c2")); Console::ReadKey (); return 0; }