【问题标题】:converting a line of code from c into c++将一行代码从 c 转换为 c++
【发布时间】: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 &lt;&lt; " - " &lt;&lt; memory_jumlah[i] &lt;&lt; ' ' &lt;&lt; aitem[memory[i]] &lt;&lt;'\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 调用中跳过变量和硬编码值。)
  • 注明,感谢批评和建议

标签: c++ c


【解决方案1】:

谁说c++中不能使用printf?

C 代码:

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

C++ 代码:

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

输出将是相同的,因为 C++ 同时支持 cout、cin 和 printf、scanf。

【讨论】:

  • 好吧,你可以使用它,但是 stdio.h 是一个危险的废话库,在 C 和 C++ 中都应该避免使用......所以如果 C++ 给你一个更好的选择,使用它。
  • @Lundin 刀可能非常危险,但也非常有用。也许我们应该禁止刀具和 printf 以及像 C 这样的语言,因为它们可能很危险。虽然我同意 printf 不应该在 C++ 中使用,但我不同意它不应该在 C 中使用。我倾向于提倡更多地学习如何使用正确的工具来完成工作。
【解决方案2】:

c++ 是 c 的超集,因此如果您正在编写任何 c 代码,那么它是有效的 c++ 代码,因此无需在 c++ 中进行转换 当我编译你的程序时,它工作得很好。

这是我用扩展名 c++ 编译这段代码时的结果:

【讨论】:

  • C++ 不是 c 的超集。典型的例子是需要强制转换 malloc 的结果和结构/联合/枚举的自动 typedef。还有其他一些问题,比如 c++ 有额外的关键字并且缺少 C99 的一些特性。
  • 它几乎是 C 的超集,因为 98% 的 C 源代码将编译为 C++,几乎不需要修改。它的意图是成为 C 的超集,这就是 C++ 得名的原因。
【解决方案3】:
cout << " - " << memory_jumlah[i] << "x " << aitem[memory[i]] << endl;

这应该可行。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多