关于本程序
我有一个显示菜单的程序。该菜单包含一系列项目(字符串)和价格(双重类型值)。该程序没有其他目的,但显示一个菜单。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