我的程序将从用户获取一串字符并向后打印该句子,它将继续向用户询问另一个字符串,直到该字符串等于退出.我遇到的问题是,当用户进入退出时,它继续循环,当我希望它只打印"谢谢".我不知道为什么当用户进入退出时if和else语句不起作用.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char chr;
char *cPtr;
char someString[50];
int stringSize;
int indx;
printf("Enter a string of characters: ");
cPtr = someString;
while ((chr = getchar()) != '\n')
{
*cPtr = chr;
cPtr++;
}
*cPtr = '\0';
stringSize = strlen(someString);
if (someString == "quit" )
{
printf("Thank you.");
}
else
{
while (someString != "quit")
{
for (indx = stringSize; indx >= 0; indx--)
{
printf("%c",*cPtr--);
}
printf("\nEnter a string of characters: ");
cPtr = someString;
while ((chr=getchar())!= '\n')
{
*cPtr = chr;
cPtr++;
}
*cPtr = '\0';
stringSize = strlen(someString);
}
}
}
Run Code Online (Sandbox Code Playgroud)
if (someString == "quit" )没有比较两个字符串的内容.它正在比较它们的地址,这些地址总是不同的.请改用此strcmp功能.同样的while (someString != "quit")