【发布时间】:2022-01-02 13:25:10
【问题描述】:
#include <iostream>
#include <stdio.h>
using namespace std;
int item, jumlahbarang, total = 0, total_belanja = 0, uang, kembalian, sum = 1, memory[100], memory_jumlah[100];
const char* aitem[11] = { "fillthe0","ITEM 1", "ITEM 2"};
string repeat;
int price1= 53000;
int price2= 76000;
int main(){
cout << " | ID | Nama Barang | Harga Barang |" << endl;
cout << " |------|---------------------------------|--------------|" << endl;
cout << " | 1. | ITEM 1 | Rp. 53000 |" << endl;
cout << " | 2. | ITEM 2 | Rp. 76000 |" << endl;
while (repeat.compare("n") != 0) {
label:
cout << " Input item id : ";
cin >> item;
memory[sum] = item;
if (item == 1) {
cout << " Item anda : " << aitem[1] << endl;
cout << " How much item do you want ot buy? : ";
cin >> jumlahbarang;
memory_jumlah[sum] = jumlahbarang;
sum++;
total = price1 * jumlahbarang;
total_belanja = total_belanja + total;
}
else if (item == 2) {
cout << " Item anda : " << aitem[2] << endl;
cout << " How much item do you want ot buy? : ";
cin >> jumlahbarang;
memory_jumlah[sum] = jumlahbarang;
sum++;
total = price2 * jumlahbarang;
total_belanja = total_belanja + total;
}
cout << " Beli Lagi?(y/n)";
cin >> repeat;
}
cout << "\n\n Struk Belanja\n";
cout << " -------------\n";
cout << " Item list : \n";
for (int i = 1; i < sum; i++) {
printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]);
}
return 0;
}
上面的代码是用于制作收据的代码,我遇到的问题是我无法将“printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]);”从 c 语言转换为 c++,我不知道我应该使用什么代码。我试过getline,改成cout,还是不行。
据我所知,c++ 使用 cout
【问题讨论】:
-
std::cout << " - " << memory_jumlah[i] << ' ' << aitem[memory[i]] <<'\n';? -
布鲁我忘了输入
-
为什么需要这么多代码来呈现您的问题?为什么不只是
int memory[100], memory_jumlah[100]; const char* aitem[11] = { "fillthe0","ITEM 1", "ITEM 2"}; int main(){ /* Setting values */ printf(" - %dx %s\n", memory_jumlah[3], aitem[memory[3]]); }?你还需要多少才能理解那条线的语法意义? -
“它仍然不起作用。” - 这是相当无信息的。选择您认为最接近正确的尝试,在minimal reproducible example 中向我们展示,并给我们预期的和实际的输出。 (为此,您可能需要实际分配值,而不是使用注释
/* Setting values */将其抽象出来。或者,直接在printf调用中跳过变量和硬编码值。) -
注明,感谢批评和建议