[英]Comparisons in C VS VB
Here is the C code:这是 C 代码:
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.嘿,我是 C 的新手,我正在尝试将我学习时的旧 VB 代码翻译成 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:这是VB代码:
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.在 C 中,字符串只是字符的 arrays,转换为字符指针。
So, to compare strings, you must use the function strcmp
.因此,要比较字符串,您必须使用 function strcmp
。
Your code should then be the following您的代码应如下所示
strcmp(over18, "Y") != 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.