我遇到字符串对齐问题。
我正在尝试打印食品订单的收据,食品名称存储在多维数组(full_menu),价格(双倍价格)和数量(数量)中。在函数结束时,这些变量将转换为字符串并附加在一个字符串变量(orders)中。循环结束后打印出来。请忽略每个订单总价的计算。稍后会完成。
#include <iostream>
#include <typeinfo>
#include <iomanip>
using namespace std;
void menu() {
string full_menu [3][6] = {{"A. Chicken",
"1.chicken leg barbecue",
"2.general chicken",
"3.chicken liver abodo",
"4.buffalo wings",
"5.Permesan wings"},
{"B. Soup",
"1.mushroom soup",
"2.cream of corn soup",
"3.hototay soup",
"4.Papaitan soup",
"5.Bulalo soup"},
{"C. Beverages",
"1.coke",
"2.diet coke",
"3.cherry coke",
"4.sprite",
"5.Aqua-Panna"
}};
for(int parent=0; parent<3; parent++)
{
for(int child=0;child<6;child++)
{
cout<<full_menu[parent][child]<<endl;
}
}
//user select the choices and quantity
string orders;
int quantity;
double price;
for (int i =0; i <=i ;i++) {
//food_name//
cout << "Enter your orders(example A1 or b3: ";
int main_menu_number=0;
char main_menu;
int sub_menu = 0;
cin >> main_menu;
if (main_menu == 'a' || main_menu == 'A') {
main_menu_number = 0;
} else if (main_menu == 'b' || main_menu == 'B') {
main_menu_number = 1;
} else if (main_menu == 'c' || main_menu == 'C') {
main_menu_number = 2;
} else if(main_menu=='x'||main_menu=='X'){
break;
}
cin>>sub_menu;
//price//
if (main_menu_number==0&&sub_menu==1){
price=5.00;
} else if(main_menu_number==0&&sub_menu==2) {
price=7.00;
}else if(main_menu_number==0&&sub_menu==3) {
price=5.50;
}else if(main_menu_number==0&&sub_menu==4) {
price=8.00;
}else if(main_menu_number==0&&sub_menu==5) {
price=7.50;
}else if(main_menu_number==1&&sub_menu==1) {
price=4.00;
}else if(main_menu_number==1&&sub_menu==2) {
price=5.50;
}else if(main_menu_number==1&&sub_menu==3) {
price=7.00;
}else if(main_menu_number==1&&sub_menu==4) {
price=8.00;
}else if(main_menu_number==1&&sub_menu==5) {
price=7.50;
}else if(main_menu_number==2&&sub_menu==1) {
price=1.50;
}else if(main_menu_number==2&&sub_menu==2) {
price=1.50;
}else if(main_menu_number==2&&sub_menu==3) {
price=1.50;
}else if(main_menu_number==2&&sub_menu==4) {
price=1.50;
}else if(main_menu_number==2&&sub_menu==5) {
price=0.30;
}
cout<<"Enter quantity :";
cin>>quantity;
orders.append(full_menu[main_menu_number][sub_menu]+" "+to_string(quantity)+" "+to_string(price)+"\n");
}
cout<<orders;
}
int main()
{
menu();
}
输出
A. Chicken
1.chicken leg barbecue
2.general chicken
3.chicken liver abodo
4.buffalo wings
5.Permesan wings
B. Soup
1.mushroom soup
2.cream of corn soup
3.hototay soup
4.Papaitan soup
5.Bulalo soup
C. Beverages
1.coke
2.diet coke
3.cherry coke
4.sprite
5.Aqua-Panna
Enter your orders(example A1 or b3:a1
a1
Enter quantity :2
2
Enter your orders(example A1 or b3:c3
c3
Enter quantity :2
2
Enter your orders(example A1 or b3:x
x
1.chicken leg barbecue 2 5.000000
3.cherry coke 2 1.500000
我想要的预期输出是:
1.chicken leg barbecue 2 5.000000
3.cherry coke 2 1.500000
请建议。
答案 0 :(得分:0)
使用std::stringstream
,以便您可以使用输出运算符,而不是std::string
。
您还需要在项目名称前使用setw()
,以使它们具有相同的宽度。
std::stringstream orders;
...
orders << setw(20) << full_menu[main_menu_number][sub_menu] << setw(5) << quantity << setw(5) << price << "\n";
然后您可以打印最终结果:
cout << orders.str();