0

The error I receive is:

program3.cpp:13:21: warning: format ‘%c’ expects argument of type ‘char*’, but argument 2 has type ‘std::string* {aka std::basic_string<char>*}’ [-Wformat=]

I'm probably lacking a very fundamental part of knowledge about the C++ language and I apologise for asking this novice question (once again). Also I would appreciate it if you could give feedback or point out other flaws in my code. What is wrong in my C++ code?

#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
    int harga=20000;
    int diskon;
    char pelanggan;
    string jenisp;
    printf("Apakah Pelanggan : [Y/N]");
    scanf("%c",&pelanggan);
    if(pelanggan=='Y'){
        printf("Apa Jenis Pelanggan [Emas,Perak,Perunggu]:");
        scanf("%c",&jenisp);
        if(jenisp=="Emas"){
            diskon=harga-(0.5*harga);
            printf("Harga untuk pelanggan kartu Emas adalah : %d",diskon);
        }
        else if(jenisp=="Perak"){
            diskon=harga-(0.25*harga);
            printf("Harga untuk pelanggan kartu Perak adalah : %d",diskon);
        }
        else if(jenisp=="Perunggu"){
            diskon=harga-(0.1*harga);
            printf("Harga untuk pelanggan kartu Perunggu adalah : %d",diskon);
        }
    }
    else{
        printf("Harga untuk bukan pelanggan adalah : %d",harga);
    }
    return 0;
}
R_Kapp
  • 2,818
  • 1
  • 18
  • 32

1 Answers1

2

As the error message says, %c in scanf except char* argument and will store one character there. It won't accept std::string*.

Using this function may helpful for learning.

std::string read_string() {
    char buffer[10000]; // allocate enough memory
    if(scanf("%9999s", buffer) == 1) {
        // successfuly read something
        return buffer;
    } else {
        return "";
    }
}

To use this, paste this before int main() and replace scanf("%c",&jenisp); with jenisp = read_string();

MikeCAT
  • 73,922
  • 11
  • 45
  • 70