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; }
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)
+ 1
Thank you very much!!