简体   繁体   English

C 语言字符串复制在 if else 语句中

[英]C Language String copy in if else stament

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

}

I was creating a program that is suppose to be a travelling payment with fares and class and totals as such.我正在创建一个程序,该程序假设是包含票价、等级和总计的旅行付款。 it runs now the only problem is the if else statement in destination which is i require 2 characters, when i enter the other destination codes eg kl,bk or tp it only outputs the statement in hk not the rest它现在运行唯一的问题是目标中的 if else 语句,我需要 2 个字符,当我输入其他目标代码(例如 kl、bk 或 tp)时,它只输出 hk 中的语句,而不输出其余的语句

You probably want to replace strcpy with strcmp你可能想用strcmp替换strcpy

strcpy(desti, "HK") will copy the text "HK" into desti the function will succeed and since strcpy returns a pointer to the destination it will be a non-zero value which wil execute the first if statement everytime. strcpy(desti, "HK")将文本 "HK" 复制到desti函数将成功,并且由于strcpy返回一个指向目标的指针,它将是一个非零值,每次都会执行第一个 if 语句。

The man page of strcpy strcpy的手册页

Note that strcmp returns 0 when they are equal.请注意,当它们相等时strcmp返回 0。

More information on strcmp有关strcmp 的更多信息

Also note that还要注意的是

  • You cannot do strcpy(desti, "HK");你不能做strcpy(desti, "HK"); into a char desti[2] , because you didn't allocate room for the null terminator.进入char desti[2] ,因为您没有为空终止符分配空间。 You would have to do char desti[3] ;你将不得不做char desti[3] ; if you wish to use it as a string.如果您想将其用作字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM