0

How can I make a char into a string

Hello! I was wondering how could I make 'y' and 'n' into yes and no because i tried it and it wasn't working. I wrote the following code: #include <stdio.h> #include <stdlib.h> int main(){ int age; char unemployed; printf("how old are you?\n"); scanf("%d",&age); if(age>0 && age<18){ printf("ticket price is 10€"); } else if(age>18) { printf("are you unemployed\n"); scanf(" %c",&unemployed); if(unemployed=='y'){ printf("your ticket price is 8 euros"); } else if(unemployed=='n'){ printf("your ticket price is 20€ euros"); } } return 0; }

27th Nov 2018, 6:54 PM
MargaritaK
2 Answers
+ 8
//Make a char array char unemployed[4]; //input in array scanf("%s", unemployed); //compare them using strcmp function if(strcmp(unemployed, "yes") == 0) //true statement else if(strcmp(unemployed, "no") == 0) //false statement Remember: strcmp function returns 0 only if both arrays are equal and it is defined in the string.h library (not sure)
27th Nov 2018, 7:18 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Thank you very much!!
27th Nov 2018, 9:21 PM
MargaritaK