首页 > 解决方案 > 如何包含美元符号?

问题描述

对于一个项目,我必须编写一个计算天然气价格的应用程序。该代码运行良好,但我注意到一些可能会被扣分的东西。我的总价不包括美元符号。我被困在哪里添加它们以及如何添加它们。下面是我的代码。请帮忙!

// 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;
}

标签: c++

解决方案


只需在数字后打印一个美元符号:

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


推荐阅读