Home > database >  converting a line of code from c into c
converting a line of code from c into c

Time:11-24

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

the code above is a code for making a recipt, the problem i have is i cant convert " printf(" - %dx %s\n", memory_jumlah[i], aitem[memory[i]]); " from c languange into c , i dont know what code should i use for that. i have tried getline, changing it into cout, it still doesnt work.

from what i know, c use cout << instead of printf, ant c does not use %d %s.

CodePudding user response:

Who said that you can't use printf in c ?

C code :

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

C code :

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

The output would be the same because C supports both cout, cin and printf, scanf.

CodePudding user response:

c is superset of c so if you are writing any c code then it is a valid c code so their is no need to convert in c as i compiled your program it is working perfectlly fine .So dont worry and Happy Coding.

this is the result when i compile this code with extension c .pls do check it out thanks. [1]: https://i.stack.imgur.com/Qcfcr.png

  •  Tags:  
  • c c
  • Related