Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.
You start with:
string item = "Coffee Mug";
int price = 12;
int quantity = 3;
Build:
Coffee Mug x3 Total: $36
Rules:
- Compute
total = price * quantity. - First line:
"<item> x<quantity>"(e.g."Coffee Mug x3"). - Second line:
"Total: $<total>". - Join the two lines with a newline
"\n"into one stringresult, then print it withcout << result << endl.
Use
to_string(quantity) and to_string(total) when concatenating numbers to a string.
line1 + "\n" + line2 joins both lines into one string.
string item = "Coffee Mug";
int price = 12;
int quantity = 3;
int total = price * quantity;
string line1 = item + " x" + to_string(quantity);
string line2 = "Total: $" + to_string(total);
string result = line1 + "\n" + line2;
cout << result << endl;
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
