-4

My C++ Program has an error like this "ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] ". And it happens on all code inside inbus function. I need help for this, please help me :(

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <ctype.h>
#include <string>
#define MAX 100
using namespace std;
struct Transport {
    char KotaAwal[20]; //Kota asal
    char KotaAkhir[20]; //Kota tujuan
    int Jarak; //Jarak antar kota
    char Skip;
 };
struct stack {
    char KotaAwal[20]; //Kota asal
    char KotaAkhir[20]; //Kota tujuan
    int Jrk; //Jarak antar kota };
};
struct Transport bus[MAX]; //struct menggunakan jalur Bus
struct stack btstack[MAX]; //struct menggunakan traking
int buspos=0;
int Findpos=0;
int Tos;
void setup();
void route(char *KotaAkhir);
void inbus(char *KotaAwal, char *KotaAkhir, int Jarak);
void push(char *KotaAwal, char *KotaAkhir, int Jrk);
void pop(char *KotaAwal, char *KotaAkhir, int Jrk);
void isbus(char *KotaAwal, char *KotaAkhir);
int find(char *KotaAwal, char *AnyWhere);
int match(char *KotaAwal, char *KotaAkhir);

int main()
{
    char KotaAwal[20], KotaAkhir[20];
    setup();
    cout <<"Masukkan Perjalanannya"<<endl;
    cout <<"~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout <<"Dari : "; cin>>KotaAwal;
    cout <<"Ke   : "; cin>>KotaAkhir;
    isbus(KotaAwal, KotaAkhir);
    route(KotaAkhir);
cin.get(); }

void setup() {
    inbus("Jakarta", "Jogyakarta", 650);
    inbus("Jakarta", "Semarang", 450);
    inbus("Jakarta", "Malang", 850);
    inbus("Jogyakarta", "Malang", 300);
    inbus("Semarang", "Surabaya", 450);
    inbus("Semarang", "Jogyakarta", 75);
    inbus("Semarang", "Banyuwangi", 750);
    inbus("Malang", "Madiun", 250);
    inbus("Malang", "Nganjuk",300);
    inbus("Malang", "Sidoarjo",30);
    }

void inbus(char *KotaAwal, char *KotaAkhir, int Jarak){
    int Jrk;
    if(buspos <MAX) {
        strcpy(bus[buspos].KotaAwal, KotaAwal);
        strcpy(bus[buspos].KotaAkhir, KotaAkhir);
        bus[buspos].Jarak = Jrk;
        bus[buspos].Skip = 0;
        buspos++; }
    else
    cout<<"Database Penuh"<<endl;
}

void isbus(char *KotaAwal, char *KotaAkhir)
{
    int d,Jarak, Jrk; //deklarasi jarak
    char AnyWhere[20]; //nama kota perantara
    if(d=match(KotaAwal, KotaAkhir)) //find hubungan lain
    {
    push(KotaAwal, KotaAkhir, d);
    return ; 
    }
    if(Jarak =find(KotaAwal, AnyWhere)) //cara hubungan lain
    {
    push(KotaAwal, KotaAkhir, Jarak);
    isbus(AnyWhere, KotaAkhir);
    }
    else
        if(Tos > 0) //Tracking
        {
        pop(KotaAwal, KotaAkhir, Jrk);
        isbus(KotaAwal, KotaAkhir);
        }
}

// menentukan jalur antar 2 kota, jika ada jalur, maka 
// tampilkan nilai panjang jarak antar kota
int match(char *KotaAwal, char *KotaAkhir)
{
    int t;
    for(t=buspos; t>-1; t--) 
    {
        if((!strcmp(bus[t].KotaAwal,KotaAwal)) && (!strcmp(bus[t].KotaAkhir,KotaAkhir)))
        return bus[t].Jarak;
    }
    return 0; 
    }
    
//Mencari jalur penghubung dari kota asal ke kota lain dari daftar lain
int find(char *KotaAwal, char *AnyWhere)
{
    char KotaAkhir[20];
    Findpos=0;
        while (Findpos < buspos) 
        {
        if((!strcmp(bus[Findpos].KotaAwal, KotaAwal)) && (!strcmp(bus[Findpos].KotaAkhir, KotaAkhir)))
        {
            strcmp(AnyWhere, bus[Findpos].KotaAkhir);
            bus[Findpos].Skip =1;
            return bus[Findpos].Jarak;
        }
        Findpos++; 
        }
    return 0;
    }
//Memasukkan node kedalam stack
void push(char *KotaAwal, char *KotaAkhir, int *Jrk)
{
    if(Tos<MAX) {
        strcpy(btstack[Tos].KotaAwal, KotaAwal);
        strcpy(btstack[Tos].KotaAkhir, KotaAkhir);
        bus[Tos].Jarak = *Jrk;
        Tos++; }
    else
    cout <<"Stack penuh !!!"<<endl;
}

//Membebaskan node dari dalam stack
void pop(char *KotaAwal, char *KotaAkhir, int *Jrk)
 { 
    if(Tos>0) 
    {
        Tos--;
        strcpy(KotaAwal, btstack[Tos].KotaAwal);
        strcpy(KotaAkhir, btstack[Tos].KotaAkhir);
        *Jrk = btstack[Tos].Jrk;
        Tos++; 
        }
    else
    cout <<"Stack kosong !!!"<<endl;
}
//Menampilkan jalur perjalan dan jarak tempuh
void route(char *KotaAkhir) {
    int Jrk, t;
    Jrk=0;
    t=0;
    while(t<Tos) {
        cout <<"Tujuan " <<btstack[t].KotaAwal<<endl;
        Jrk += btstack[t].Jrk;
        t++;
    }
    cout<<KotaAkhir<<endl;
    cout<<Jrk<<endl; }

All code in inbus function has same error, ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]. here is the error message: https://i.stack.imgur.com/MHnhf.png

Alex
  • 1
  • 1
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and research"* and you'll find plenty of related posts for this. – Jason Jan 26 '23 at 07:45

1 Answers1

0

String literals are constant, you cannot modify them. Therefore in C++ the type of a string literal is const char[N] (for some value of N). It is not possible to assign const char[N] to char* because you lose the const but it is possible to assign to const char*.

So you need to change

void isbus(char *KotaAwal, char *KotaAkhir)

to

void isbus(const char *KotaAwal, const char *KotaAkhir)

Looks like you need to do the same to match and find.

john
  • 85,011
  • 4
  • 57
  • 81
  • its still have same error, "ISO C++ forbids..." and "undefined references to function ( like `pop(), `bus(), `match(), `isbus(), `find(), `push() ). And collect2.exe: error: ld returned 1 exit status – Alex Jan 26 '23 at 07:48
  • @Alex You need to change the function **and** the prototype. – john Jan 26 '23 at 07:51
  • @Alex Also look at your `inbus` prototype `void inbus(char *KotaAwal, char *KotaAkhir, int Jarak);` and your `inbus` function `void inbus(char *KotaAwal, char *KotaAkhir) { ... }` See the problem? Your prototype has three parameters but your function only has two. – john Jan 26 '23 at 07:52
  • @Alex Similar problem with `push` and `pop` the prototypes and the functions are not the same. – john Jan 26 '23 at 07:54