Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i want the program to ask user which payment method they wanna use (ONLINE/CARD) . But my code will show the wrong output

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading