如何包含美元符号?

时间:2020-05-06 20:09:49

标签: 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;
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

只需将其添加到您的值之前

cout << "You chose regular. The total price of gas is: $" 
<< (numberOfGallons * PRICE_OF_REGULAR);