条件匹配时中断循环
break loop when condition match
这是一个猜字游戏。例如,hello
给出为 h___o
,用户必须猜测字母。
我在我的循环上设置了一个条件,但不知道为什么它没有打破 while
循环。
#include <stdio.h>
#include <string.h>
int main()
{
char word[] = "hello";
int length = strlen(word);
int check;
char spaceLetters[length];
int i, j;
spaceLetters[0] = word[0];
char *dash = "_";
for (i = 1; i < length; i++)
{
strncat(spaceLetters, dash, 1);
}
int attemptLeft = length;
printf("\n %s\n", spaceLetters);
printf("\t\t\tAttempt Left: %d\n", attemptLeft);
boolean start = T;
int userInput;
while (1)
{
printf("\n");
printf("Enter Letter:");
scanf("%c", &userInput);
for 循环检查输入的字母是否正确
for (j = 1; j < length;j++)
{
if (word[j] == userInput)
{
spaceLetters[j] = word[j];
printf("%s\n", spaceLetters);
printf("\t\t\tAttempt Left: %d\n", attemptLeft);
printf("\n");
}
}
这是我在 hello == hello break loop 时的 break loop 条件
if(word == spaceLetters){
break;
}
}
}
对于初学者而不是这个电话
scanf("%c", &userInput);
^^^
你必须使用这个电话
char userInput;
//...
scanf(" %c", &userInput);
^^^^
否则调用还将读取空格作为换行符 '\n'
,在用户按下 Enter 键后存储在输入缓冲区中。
数组没有比较运算符==。您必须逐个元素地比较数组。要比较存储在字符数组中的字符串,您应该使用标准函数 strcmp
#include <string.h>
//...
if( strcmp( word, spaceLetters ) == 0 ){
break;
}
但在使用此函数之前,数组 specialLetters
必须声明为
char spaceLetters[sizeof( word )] = ""`;
即数组应包含一个字符串。
否则程序中的这个 for 循环会调用未定义的行为。
for (i = 1; i < length; i++)
{
strncat(spaceLetters, dash, 1);
}
这并没有什么意义。
你可以直接写
char dash = '_';
memset( spaceLetters + 1, dash, sizeof( spaceLetters ) - 2 );
字符串用arrays/pointers表示。它们需要使用字符串库进行比较。替换
if ( word == spaceLetters )
与
if ( strcmp ( word, spaceLetters ) == 0 )
您还需要添加 #include <string.h>
。
这是一个猜字游戏。例如,hello
给出为 h___o
,用户必须猜测字母。
我在我的循环上设置了一个条件,但不知道为什么它没有打破 while
循环。
#include <stdio.h>
#include <string.h>
int main()
{
char word[] = "hello";
int length = strlen(word);
int check;
char spaceLetters[length];
int i, j;
spaceLetters[0] = word[0];
char *dash = "_";
for (i = 1; i < length; i++)
{
strncat(spaceLetters, dash, 1);
}
int attemptLeft = length;
printf("\n %s\n", spaceLetters);
printf("\t\t\tAttempt Left: %d\n", attemptLeft);
boolean start = T;
int userInput;
while (1)
{
printf("\n");
printf("Enter Letter:");
scanf("%c", &userInput);
for 循环检查输入的字母是否正确
for (j = 1; j < length;j++)
{
if (word[j] == userInput)
{
spaceLetters[j] = word[j];
printf("%s\n", spaceLetters);
printf("\t\t\tAttempt Left: %d\n", attemptLeft);
printf("\n");
}
}
这是我在 hello == hello break loop 时的 break loop 条件
if(word == spaceLetters){
break;
}
}
}
对于初学者而不是这个电话
scanf("%c", &userInput);
^^^
你必须使用这个电话
char userInput;
//...
scanf(" %c", &userInput);
^^^^
否则调用还将读取空格作为换行符 '\n'
,在用户按下 Enter 键后存储在输入缓冲区中。
数组没有比较运算符==。您必须逐个元素地比较数组。要比较存储在字符数组中的字符串,您应该使用标准函数 strcmp
#include <string.h>
//...
if( strcmp( word, spaceLetters ) == 0 ){
break;
}
但在使用此函数之前,数组 specialLetters
必须声明为
char spaceLetters[sizeof( word )] = ""`;
即数组应包含一个字符串。
否则程序中的这个 for 循环会调用未定义的行为。
for (i = 1; i < length; i++)
{
strncat(spaceLetters, dash, 1);
}
这并没有什么意义。
你可以直接写
char dash = '_';
memset( spaceLetters + 1, dash, sizeof( spaceLetters ) - 2 );
字符串用arrays/pointers表示。它们需要使用字符串库进行比较。替换
if ( word == spaceLetters )
与
if ( strcmp ( word, spaceLetters ) == 0 )
您还需要添加 #include <string.h>
。