0

Since C doesn't have boolean types how could I make the user use yes or no as an answer to an if?

6th Nov 2018, 1:47 PM
MargaritaK
6 odpowiedzi
+ 3
Oh, then it seems you want to input a string. You can use char unemployed[10]; scanf("%s", &unemployed); and unemplyed!="no", etc in the relevant places. That should work out, but let me know if it doesn't. My C is a little rusty.
6th Nov 2018, 3:08 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 10
You can use stdbool.h library like Babak said or you can use integers for that too. int isTrue = 1 //if true isTrue = 0 //if false
6th Nov 2018, 7:20 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 5
Oh C'mon Kishalaya ! :D It's not rusty, it's trusty! ;D MargaritaK include <stdbool.h> header to your code then you have access to bool true false _____ https://en.cppreference.com/w/c/types/boolean
6th Nov 2018, 4:09 PM
Babak
Babak - avatar
+ 3
I'm not sure I understand your question completely, but using the integers 1 and 0 instead of the boolean data types True and False should work.
6th Nov 2018, 2:00 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Thank you very very much! I'll try them out and I'll let you know!:-)
6th Nov 2018, 6:28 PM
MargaritaK
+ 1
Thank you for answering, I've written this code: #include <stdio.h> #include <stdlib.h> int main (){ int age,unemployed; printf("how old are you?\n"); scanf("%d",&age); printf("are you unemployed?\n"); scanf("%d", &unemployed); if(age<18){ printf("your ticket price is 14 euros"); } else if((age>18)&&(age<60)&&(unemployed!=0)){ printf("your ticket price is 16 euros");} else if((age>18)&&(age<60)&&(unemployed==0)){ printf("your ticket price is 13 euros"); } else if(age>=60){ printf("your ticket price is 13 euros"); } return 0;} And on the output of the question "are you unemployed" it only executes the non employed command only if I type 0. I wondered if there was a way to replace 0 with no. Thank you }
6th Nov 2018, 2:05 PM
MargaritaK