#include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const float TAX_RATE = .0675; const float SHIP_RATE = 13.43; int main() { string name; float priceItem1 = 42.79; float priceItem2 = 33.95; float priceItem3 = 78.89; float priceItem4 = 29.25; float charge = 0, item1 = 0, item2 = 0, item3 = 0, item4 = 0, taxTotal = 0, total = 0; int qty1; int qty2; int qty3; int qty4; int totQty; int shipping; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout<<" Select quantity of item #1 "<<endl; cin>>qty1; cout << " # of Items: " <<qty1<<endl; item1 = qty1*priceItem1; cout << " Item #1 Total: $" <<setprecision(2)<<item1<<endl; cout<< " " <<endl; cout<<" Select quantity of item #2 "<<endl; cin>>qty2; cout << " # of Items: " <<qty2<<endl; item2 = qty2*priceItem2; cout << " Item #2 Total: $" <<setprecision(2)<<item2<<endl; cout<< " " <<endl; cout<<" Select quantity of item #3 "<<endl; cin>>qty3; cout << " # of Items: " <<qty3<<endl; item3 = qty3*priceItem3; cout << " Item #3 Total: $" <<setprecision(2)<<item3<<endl; cout<< " " <<endl; cout<<" Select quantity of item #4 "<<endl; cin>>qty4; cout << " # of Items: " <<qty4<<endl; item4 = qty4*priceItem4; cout << " Item #4 Total: $" <<setprecision(2)<<item4<<endl; cout<< " " <<endl; totQty = qty1+qty2+qty3+qty4; cout<<" Total item Qty: "<<totQty<<endl; charge = item1 + item2 + item3 + item4; cout << " Total Spent: $" <<setprecision(2)<<charge<<endl; taxTotal = charge * TAX_RATE; cout << " Total tax amount: $" <<setprecision(2)<<taxTotal<<endl; shipping = (SHIP_RATE * totQty)*.435; cout << " Shipping Cost: $" <<shipping<<endl; total = taxTotal + charge + shipping; cout << " Your total with tax: $" <<setprecision(2)<<total<<endl; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}