Here is the C code:
do
{
printf("\nOver eighteen (Y/N): ");
scanf("%c", over18);
if(over18 != "Y" || over18 != "N")
{
printf("\nInvalid 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
.
Your code should then be the following
strcmp(over18, "Y") != 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.