Here is the C code:
do { printf(" Over eighteen (Y/N): "); scanf("%c", over18); if(over18 != "Y" || over18 != "N") { printf(" Invalid input"); } } while(over18 != "Y" && over18 != "N");
Hey, I am new to C and I am trying to sort-off translate my old VB code from when I was learning that into C. I have attempted to do that for this part of a program, but that gives the error "warning: comparison between pointer and integer" for all my comparisons using "!=". Here is the VB code:
Do Console.WriteLine("Are you over eighteen (Y/N)?") over18 = Console.ReadLine() If over18 <> "Y" And over18 <> "N" Then Console.WriteLine("That is not a valid answer") End If Loop Until over18 = "Y" Or over18 = "N"
Why is this happening?
In C, strings are just arrays of chars, which translates into a char pointer.
So, to compare strings, you must use the function strcmp.
strcmp
Your code should then be the following
strcmp(over18, "Y") != 0
1.4m articles
1.4m replys
5 comments
57.0k users