变换一行代码从c入c++

0

的问题

#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;
}

代码以上是代码,用于制作一个recipt,我的问题是我无法转换" printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]); "从c的更新为c++,我不知道是什么代码我应该使用。 我已经尝试过正确的单词,改变了它入状况,它仍然没有工作。

从我知道,c++使用状<<而不是printf,蚂蚁c++不使用%d%。

c++
2021-11-24 05:49:04
3

最好的答案

2

谁说你不能使用printf在c++?

C码:

printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]);

C++编码:

printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]);

输出将是相同的,因为C++支持诉讼,cin和printf,scanf.

2021-11-24 06:54:40

嗯,你 可以 使用它,但stdio.h是一个危险的垃圾库,并应避免在C和C++两个...所以如果C++给你一个更好的选择,使用它。
Lundin

@Lundin刀可是很危险的,也是非常有用的。 也许我们应该禁止刀和printf和语言等C,因为他们可能是危险的。 虽然我同意 printf 不应该是用C++,我不同意,它不应该被用来在C;我倾向于提倡更多的学习如何使用正确的工具。
autistic
0
cout << " - " << memory_jumlah[i] << "x " << aitem[memory[i]] << endl;

这个应该的工作。

2021-11-24 06:43:28
-2

c++是超集c所以如果你正在写的任何c码然后这是一个有效c++编码,所以没有必要转换在c++ 因为我编译程序,它的工作是完美的罚款。

这是结果的时候我编这个代码扩展c++:

1

2021-11-24 15:14:54

C++不是一个超集c。 典型的例子是必要投的结果malloc和自动typedef对于结构/工会/枚举。 还有其他问题,如c++具有额外的关键词以及缺乏一些C99的功能。
martinkunev

这几乎是一个超集C,在98%的C源代码将汇编为C++,很少或没有修改。 其意图是一个超集C是如何C++得到了它的名称。
Jeremy Friesner

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................