#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ticket,tnotran,tran;
char pass[40],desti[2],cla,destina[20],clana[20];
float tfare,cfare,nfare,gtnfare;
int main(int argc, char *argv[]) {
printf("\n\n\t\tTicket NO.: ");
scanf("%d",&ticket);
while (ticket != 0){
head();
con();
system("pause");
system("cls");
netcomp();
out();
system("pause");
system("cls");
printf("\n\n\t\tTicket NO.: ");
scanf("%d",&ticket);
}
printf("\n\n\t\tTotal No. of Transactions: %d", tran);
printf("\n\n\t\tGrand Total of Net Fare: %f", gtnfare);
}
head(){
printf("\n\t\tPassenger's Name: ");
scanf("%s",&pass);
printf("\n\t\tDestination Code: ");
scanf("%s",&desti);
printf("\n\t\tClass Code: ");
scanf("%s",&cla);
}
con(){
if(strcpy(desti, "HK")||strcpy(desti, "hk")){
tfare= 8500;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Hong Kong");
}
else if(strcpy(desti, "BK")||strcpy(desti, "bk")){
tfare= 7500;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Bangkok");
}
else if(strcpy(desti, "KL")||strcpy(desti, "kl")){
tfare= 7900;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Kualala Lumpur");
}
else if(strcpy(desti, "TP")||strcpy(desti, "tp")){
tfare= 8300;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Taipe");
}
else{
printf("\n\t\tInvalid Input");
tfare=0;
}
if(cla== 'E'|| cla=='e'){
cfare= 1500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"Economy");
}
else if(cla== 'T'|| cla=='t'){
cfare= 2500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"Tourist");
}
else if(cla== 'F'|| cla=='f'){
cfare= 3500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"First Class");
}
}
netcomp(){
nfare = tfare+ cfare;
gtnfare +=nfare;
tran += 1;
}
out(){
printf("\n\n\t\tTicket NO.: %d", ticket);
printf("\n\t\tPassenger's Name: %s", pass);
printf("\n\t\tDestination: %s", destina);
printf("\n\t\tClass: %s", clana);
printf("\n\t\tNet Fare: %f", nfare);
}
我正在创建一个程序,假设该程序是包含车费,舱位和总计的旅费。它现在运行唯一的问题是目标中的if else语句,我需要2个字符,当我输入其他目标代码(例如kl,bk或tp)时,它仅在hk中输出该语句
最佳答案
您可能想将strcpy
替换为strcmp
strcpy(desti, "HK")
将文本“ HK”复制到desti
中,该函数将成功执行,并且由于strcpy
返回指向目标的指针,因此它将是一个非零值,该值将每次执行第一个if语句。
strcpy的手册页
请注意,strcmp
相等时返回0。
有关strcmp的更多信息
另请注意
您不能将strcpy(desti, "HK");
转换为char desti[2]
,因为您没有为空终止符分配空间。您将必须执行char desti[3]
;如果您希望将其用作字符串。
关于c - C语言字符串在if else语句中复制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52524105/