Home > Enterprise >  i want the program to ask user which payment method they wanna use (ONLINE/CARD) . But my code will
i want the program to ask user which payment method they wanna use (ONLINE/CARD) . But my code will

Time:01-16

void Payment(){

    do{

        printf("\nPLEASE ENTER YOUR PREFERED PAYMENT METHOD (ONLINE/CARD):");
        scanf("%s", &PaymentMethod);
        
        if(strcmp(PaymentMethod, "CARD")){
            
            Option = 1;
            printf("\nYOU HAVE SELECTED CARD PAYMENT METHOD");
        }
        else if(strcmp(PaymentMethod, "ONLINE")){

            Option = 1;    
            printf("\nYOU HAVE SELECTED ONLINE PAYMENT METHOD");
            printf("\n|DRUM E-BOOK ACCOUNT DETAILS : 6734-343-8621 (GOODBANK BERHAD)");
            printf("\nYOU HAVE SELECTED CARD PAYMENT METHOD");
            
        }
        else{
            printf("\nYOU HAVE ENTERED WRONG PAYMENT METHOD! PLEASE TRY AGAIN");
            Option=0;
        }
    }while(Option == 0);
    printf("\nPress any number to continue:");
    scanf("%d", &Option);
 Receipt();

each time i choose CARD , it will show the ONLINE output and show the opposite if i choose ONLINE

CodePudding user response:

if(strcmp(PaymentMethod, "CARD"))

is equivalent to:

if (strcmp(PaymentMethod, "CARD") != 0)

strcmp returns 0 on equality, but the if construct considers zero to be false, so the block is never entered.

Change the condition to:

if (strcmp(PaymentMethod, "CARD") == 0)

Re: "should i change to PaymentMethod == "CARD"?"

Answer: No, you can't compare strings with the equality == operator. That only compares the pointer values.


Also note that you do not need to use the & operator with the %s format specifier in the call to scanf.

  •  Tags:  
  • c
  • Related