so in here, i'm trying to pass the dokter value to main function, and from what i know, function can only return one value at a time, so is there any other way to return the dokter value to main function? Any help/advice will be appreciated, thanks!
void booking(int argc, char** argv){
int *dokter;
system("cls");
printf("\nSilahkan pilih Dokter yang tersedia\n1. Dr. Saburo\n2. Dr. Bineka\n3. Dr. Saskeh\n4. Dr. Lur Bor\n5. Dr. Frutang");
printf("\nPilih (1/2/3/4/5) >: ");
scanf("%d", &dokter);
if(dokter == 1){
printf("Hari yang tersedia : Senin, Rabu, Kamis, Jumat");
} else if(dokter == 2){
printf("Hari yang tersedia : Selasa, Rabu, Jumat, Sabtu");
} else if(dokter == 3){
printf("Hari yang tersedia : Senin, Rabu, Kamis, Sabtu, Minggu");
} else if(dokter == 4){
printf("Hari yang tersedia : Senin, Kamis, Jumat");
} else if(dokter == 5){
printf("Hari yang tersedia : Selasa, Rabu, Sabtu, Minggu");
} else{
printf("Invalid");
}
jadwal = (char*) malloc(100*sizeof(char));
printf("\nJadwal >: ");
scanf(" 1[^\n]s", jadwal);
return jadwal;
free(jadwal);
}
CodePudding user response:
You have a few options, here's two:
- Make a struct, which contains members of what you want to return. In your case it looks like you want to return a
char*
and anint
.
struct MyStruct {
char* string;
int foo;
};
- Make one or more of the things you are returning a global variable.
int *dokter;
...
void booking(int argc, char** argv){
system("cls");
...
}
CodePudding user response:
Quite a few things:
int *dokter;
does not allocate any memory for anint
, but rather memory for the address of anint
. This should simply beint dokter;
.The field-width specifier in
scanf("1[^\n]s", jadwal)
should be one less than the size of your buffer, which would be99
. Additionally,%[]
and%s
are two different specifiers.%[]s
is the first of these, followed by a literals
.- You must also consider that the previous
scanf
has left the newline in the buffer, and as such a properly formatted specifier of
- You must also consider that the previous