c++ - 如何包含美元符号?
问题描述
对于一个项目,我必须编写一个计算天然气价格的应用程序。该代码运行良好,但我注意到一些可能会被扣分的东西。我的总价不包括美元符号。我被困在哪里添加它们以及如何添加它们。下面是我的代码。请帮忙!
// FinalProject1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
const double PRICE_OF_REGULAR = 1.67;
const double PRICE_OF_SPECIAL = 1.87;
const double PRICE_OF_SUPER = 1.99;
int main()
{
cout << "Gas Pump Calculator!" << endl;
double numberOfGallons;
cout << "Please enter number of gallons needed: ";
cin >> numberOfGallons;
cout << endl;
cout << "1. Regular" << endl;
cout << "2. Special" << endl;
cout << "3. Super+" << endl;
cout << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
cout << endl;
cout << "You chose regular. The total price of gas is: " << (numberOfGallons * PRICE_OF_REGULAR);
cout << endl;
break;
case 2:
cout << endl;
cout << "You chose special. The total price of gas is: " << (numberOfGallons * PRICE_OF_SPECIAL);
cout << endl;
break;
case 3:
cout << endl;
cout << "You chose super+. The total price of gas is: " << (numberOfGallons * PRICE_OF_SUPER);
cout << endl;
break;
}
return 0;
}
解决方案
只需在数字后打印一个美元符号:
cout << "You chose regular. The total price of gas is: "
<< (numberOfGallons * PRICE_OF_REGULAR) << "$";
//^ add dollar sign
或在数字之前,取决于您要如何打印出来。
cout << "You chose regular. The total price of gas is: $"
<< (numberOfGallons * PRICE_OF_REGULAR); //^ add dollar sign
推荐阅读
- django - 如何在django rest框架中覆盖通用RetrieveAPIView中的get方法以过滤结果
- firebase - 披露 firebase apiKey
- java - 如何使用 Sqlite 执行 jar maven 项目
- php - 每隔 10 分钟执行两个任务的 cron 作业行为,间隔为 8 小时
- c++17 - 使用placement new构造数组数组的语法是什么?
- c# - 如何在 Excel 女士中计算 DayOfWeek
- flutter - Flutter 提取小部件需要返回小部件
- r - 如何顺序连接对象列表中的每个 nth:(nth+j) 对象
- python - 无法对画布使用 .delete 属性
- javascript - 从 python 到 javascript 的变量