我想循环我的程序,并显示用户提示"是否要再次运行?是(Y),否(N)"。在 C 语言中



我有这个程序,我终于完成了,但我试图得到它后完成一次提示用户"你想再次运行吗?是的(Y),没有(N)"。但我也想让它要求s2,还有ch。保持s1是相同的随机字符串,如果有意义的话。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
void s1(char *random);
void s2(char *s2_input, int index);
void strfilter(char *random, char *s2_input, char replacement);
int main()
{
char run = 'Y';
while(run != 'N')
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '')
{
printf("size too small");
exit(0);
}
printf("ch = ");
char replacement = getchar();
printf("n");
int filter_index = 41;
strfilter(s1_random, s2_input, replacement);
printf("ns1 filtered = ");
puts(s1_random);
printf("Do you wish to run again? Yes(Y), No(N) ");
scanf("%c", &run);
}
}


void s1(char *random)
{
int limit = 0;
char characters;
while((characters = (('A' + (rand() % 26))))) /* random generatro */
{
if(limit == 41)
{
*(random + 41 - 1) = '';
break;
}
*(random + limit) = characters;
limit++;
}
}

void s2(char *s2_input, int index)
{
char array[21] = "123456789012345678901"; /* populated array to make sure no random memory is made */
char input;
int count = 0;
int check = 0;
while((input = getchar() ))
{
if(input == 'n')
{
*(s2_input + count) = '';
break;
}
else if(input < 65 || input > 90)
{
printf("invalid input");
exit(0);
}
*(s2_input + count) = input;
count++;
}
index = count;
}
void strfilter(char *random, char *s2_input, char replacement) /* replacement function */
{
while(*s2_input)
{
char *temp = random;
while(*temp)
{
if(*temp == *s2_input)
*temp = replacement;
temp++;
}
s2_input++;
}
}

一开始我尝试在main函数中使用do-while循环。但这似乎不起作用。它只是弄乱了我的程序的输出,仍然没有提示用户。我是否应该创建一个仅用于提示用户的新函数?如果有,我该怎么做?提前谢谢。

为了避免尾部数据的冲突,调用readchar(),直到在每个提示符之后得到换行符或EOF:

for(;;)
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '')
{
printf("size too small");
exit(0);
}
printf("ch = ");
int replacement = getchar();
if(replacement == EOF)
break;
while(getchar() != 'n');
printf("n");
strfilter(s1_random, s2_input, replacement);
printf("ns1 filtered = ");
puts(s1_random);
printf("Do you wish to run again? Yes(Y), No(N) ");
int run = getchar();
// or include ctype.h and do:
// run == EOF || toupper(run) == 'N'
if(run == EOF || run == 'N' || run == 'n')
break;
while(getchar() != 'n');
}

和示例:

s1 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
s2 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
ch = B

s1 filtered = BBBBBBBB
Do you wish to run again? Yes(Y), No(N) y
s1 = DBEFSARCBYNECDYGGXXPKLORELLNMPAPQFWKHOPK
s2 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
ch = B  

s1 filtered = BBBBBBBB
Do you wish to run again? Yes(Y), No(N) N

在C程序中使用getcharscanf从stdin获取用户输入的一个非常常见的错误是忘记了这些函数可能在输入流中留下换行符(或其他字符)。

在本例中,

scanf("%c", &run);

将在流中(至少)留下一个换行符。该换行符将被下一个getchar读取,从而使程序产生意想不到的行为。

所以你的代码应该删除scanf之后的换行符。实际上,它应该删除所有字符,直到它看到一个换行符。

此外,在扫描用户输入之前,我还将删除流中存在的任何空白。只需在%c

前加一个空格即可。最后,你的提示提示你希望用户输入Y或N,但你的程序继续输入任何不是N的

这样做应该可以解决上面提到的问题:

char run = 'Y';
while(run != 'N')
{
// ... do your stuff
while(1)
{
// notice the space before %c to remove initial white spaces
if (scanf(" %c", &run) != 1) exit(1);  // Input error
// Empty the input stream until a newline is found
while (1)
{
int temp = getchar();
if (temp == 'n') break;   // Found end-of-line so break this while-loop
if (temp == EOF) exit(1);  // Input error
}
if (run == 'Y' || run == 'N') break;  // Valid input so break this while-loop
}
}

最新更新