關於本程序
我有一個顯示菜單的程序。該菜單包含一系列項目(字符串)和價格(雙重類型值)。該程序沒有其他目的,但顯示一個菜單。C++轉換爲貨幣值
問題
當然,默認情況下,雙精度型值以小數顯示。這在這種情況下很有用,因爲我們列出了物品的價格;然而,通常價格最初始於美元符號($)。據我所知, iomanip標題包含「put_money」&「put_money」函數,但這些不需要做我需要他們做的事情。我需要的是菜單中的每個價格都與美元符號一起顯示在實際值旁邊。例如:
我所需的輸出是這樣的:
Please select an item to purchase:
______________________________________________________
Item: Prices: Item: Prices: Item: Prices:
______________________________________________________
Water $100 Milk $200 Juice $300
Wine $400 Bread $500 Apple $600
Tuna $700 Steak $800 Bandage $900
Med-Kit $1000 Splint $1100 Thread $1200
我目前的計劃會產生這樣:
Please select an item to purchase:
______________________________________________________
Item: Prices: Item: Prices: Item: Prices:
______________________________________________________
Water 100 Milk 200 Juice 300
Wine 400 Bread 500 Apple 600
Tuna 700 Steak 800 Bandage 900
Med-Kit 1000 Splint 1100 Thread 1200
有沒有美元符號添加到所有的更有效方式我的價值,而不必手動添加每個單獨的美元符號?我只是覺得手動添加它們可能在一個大型項目中很乏味(當然不是這樣,但可能類似)。
我的代碼
#include <iostream>
#include <iomanip>
#include <string>
int main(void) {
unsigned short int width = 10;
unsigned short int prec = 6;
float prices[12] = {100.00, 200.00, 300.00, 400.00, 500.00, 600.00, 700.00, 800.00, 900.00, 1000.00, 1100.00, 1200.00};
std::string items[12] = {"Water", "Milk", "Juice", "Wine", "Bread", "Apple", "Tuna", "Steak", "Bandage", "Med-Kit", "Splint","Thread"};
std::cout << "\nPlease select an item to purchase:\n\n";
std::cout << "_________________________________________________________________\n\n";
std::cout << std::setw(width) << "Item:" << std::setw(width) << "Prices:" <<std::setw(width) << "Item:" <<std::setw(width) << "Prices;" <<std::setw(width) << "Item:" <<std::setw(width) << "Prices:" << "\n";
std::cout << "_________________________________________________________________\n\n";
std::cout << std::setw(width) <<items[0] <<std::setw(width) << prices[0] <<std::setw(width) << items[1] <<std::setw(width) << prices[1] <<std::setw(width) << items[2] <<std::setw(width) << prices[2] << "\n";
std::cout << std::setw(width) <<items[3] <<std::setw(width) << prices[3] <<std::setw(width) << items[4] <<std::setw(width) << prices[4] <<std::setw(width) << items[5] <<std::setw(width) << prices[5] << "\n";
std::cout << std::setw(width) <<items[6] <<std::setw(width) << prices[6] <<std::setw(width) << items[7] <<std::setw(width) << prices[7] <<std::setw(width) << items[8] <<std::setw(width) << prices[8] << "\n";
std::cout << std::setw(width) <<items[9] <<std::setw(width) << prices[9] <<std::setw(width) << items[10] <<std::setw(width) << prices[10] <<std::setw(width) << items[11] <<std::setw(width) << prices[11] << "\n";
std::cin.get();
std::cin.get();
return 0;
}
請原諒的代碼過長。感謝大家提前幫助。
我不是要求檢查我的代碼,但有助於顯示貨幣值。@JustSid – Jake2k13
嘗試'std :: cout << std :: setw(width)<< items [0] << std :: setw(width)<<「$」<< prices [0] << std :: setw (width)<< items [1] << std :: setw(width)<<「$」<< prices [1] << std :: setw(width)<< items [2] << std :: setw (width)<<「$」<< prices [2] <<「\ n」;' –
這可行,但我想知道是否有更有效的方法,而不是手動將其添加到每個值。這很容易變成一個單調乏味且壓倒一切的過程。但這是一個很好的建議。 @Ankit B – Jake2k13