[英]The loop will skip the macro do while loop when y is entered
I need help regarding this... the problem in my c is when I execute this program and when I want to repeat the roll, the roll will not be displayed from 2nd time onwards... what do I do? 我需要有关此的帮助...我的c语言中的问题是当我执行此程序时,当我要重复滚动时,第二次以后将不显示滚动...我该怎么办? I dont know what to do to fix this.. got stuck here
我不知道该怎么办才能解决此问题。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, a, n, z;
char player[5][150], b, c;
float ave, total, f1, f2, f3;
total = 0;
ave = 0;
printf("\nPlease enter number of players : ");
scanf("%d", &a);
for (i = 0; i < a; i++)
{
printf("\nEnter player %d's name : ", i + 1);
scanf("%s", &player[i][150]);
}
printf("\nChoose the amount of dice used : ");
scanf(" %d", &n);
do
{
for (z = 1; z <= a; z++)
{
printf("\n\t%s\n ", player[z]);
if (n == 1)
{
do
{
f1 = 1.0 + 6.0 * ((float) rand() / RAND_MAX);
printf("\nRoll : %.0f\n", f1);
total = f1;
printf("Total : %.0f\n", total);
}while (f1 == 6);
}
else if (n == 2)
{
do
{
f1 = 1 + (rand() % 6);
f2 = 1 + (rand() % 6);
printf("\nRoll : %.0f,%.0f\n", f1, f2);
total = f1 + f2;
printf("Total : %.0f\n", total);
}while (f1 == f2);
}
else if (n == 3)
{
do
{
f1 = 1 + (rand() % 6);
f2 = 1 + (rand() % 6);
f3 = 1 + (rand() % 6);
printf("\nRoll : %.0f,%.0f,%.0f\n", f1, f2, f3);
total = f1 + f2 + f3;
printf("Total : %.0f\n", total);
}while (f1 == f2 && f2 == f3);
}
}
printf("\nRoll again ? (y/n) = ");
scanf("%s", &b);
}while (b == 'y');
printf("\n");
ave = total / n;
printf("Average : %.2f\n\n", ave);
return 0;
}
First thing is 第一件事是
scanf("%s", &b);
should be 应该
scanf("%c", &b);
And you have to flush your stdin for your do while
to work. 而且你必须刷新你的标准输入你
do while
工作。
while ((c = getchar()) != '\n' && c != EOF);
The portable way to flush stdin
冲洗
stdin
的便携式方法
%s
reads a C string, that is a null terminated char array. %s
读取一个C字符串,它是一个以null结尾的char数组。 The shortest string containing 'y'
is a 2 character array: "y"
or { 'y', '\\0'}
. 包含
'y'
的最短字符串是2个字符的数组: "y"
或{ 'y', '\\0'}
。
So you should change char b;
所以你应该改变
char b;
with char b[2];
用
char b[2];
, and use it that way: ,并以这种方式使用:
scanf("%1s", b);
}while (*b == 'y');
You current code writes (at least) a null after character b
invoking undefined behaviour: anything can happen after that. 您当前的代码在字符
b
调用未定义的行为之后(至少)写了一个null:在那之后可能发生任何事情。 But scanf("%1s", b);
但是
scanf("%1s", b);
only reads first non blank character in b[0]
and puts a null in b[1]
: correct. 仅读取
b[0]
第一个非空白字符,并在b[1]
放置null:正确。
But IMHO, never mix input ignoring blank (space, tab, end of line) ( %d%s%f...
) with input explicitely processing them ( %c
, fgets
), unless you are sure why you do it. 但是恕我直言,切勿将忽略空格(空格,制表符,行尾)(
%d%s%f...
)的输入与明确处理它们的输入( %c
, fgets
)混合使用,除非您确定为什么要这样做。 So I will not advise you to use scanf("%c", b);
因此,我不建议您使用
scanf("%c", b);
regarding this line: 关于这条线:
scanf("%s", &b);
variable 'b' is a character, so it must be read as a character. 变量“ b”是一个字符,因此必须将其作为字符读取。
scanf(" %c", &b );
note: leading space in format string so leading white space will be skipped over 注意:格式字符串中的前导空格,因此前导空格将被跳过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.