,所以我试图循环名称询问部分以及年龄部分,年龄部分工作得很好,但是当我尝试使用名称部分时,它不起作用。我想要实现的是,当你把一个数字放在名字部分,或者反过来,你会得到一个错误信息,它会把你循环到同样的问题--
#include <stdio.h>
int vek;
char name[20];
int result1;
int result2;
int main()
{
FindName();
void FindName() { // it wants me to put a ";" which doesn't make sense to me and doesn't work
printf("Napis svoje meno \n");
result2 = scanf("%s",&name);
while (gethar() != '\n');
if(result2 == 1){
printf("Ahoj %s \n",name);
break;
system("pause");
}
else {
printf("nepis sem cisla ty kokot \n");
}
findAge();
}
void findAge() {
printf("Napis svoj vek \n");
result1 = scanf("%d",&vek);
while (getchar() != '\n');
if(result1 == 1){
printf("%s si krasny %d rocny priklad downoveho syndromu \n ",&name,vek);
}
else {
printf("co si jebnuty \n");
findAge();
}
}我试着打破循环,如果它是正确的答案,但这也是行不通的,我只是一个初学者
发布于 2021-04-24 21:12:28
只要条件为真,while循环将运行体内的所有内容。您需要将需要在{和}之间的块中重复的代码放在一起。你在后面放了个分号。这意味着空语句,或者什么都不做。这样,就会对条件进行检查,直到它不再为真,但不会做任何其他操作。例如:
int i=0;
while(i < 3) {
printf("%d\n", i);
i++;
}它将打印数字0、1和2,然后停止,因为条件不再为真。
你想让程序看起来像这样。伪码
main:
call findName
call findAge
findName:
print "Something Eastern European asking for a name"
result = 0;
while result != 1:
result = read input
if result == 0
print "Try again"findAge也是如此
注意,这些函数永远不会调用自己。他们只是运行循环直到输入是有效的。
https://stackoverflow.com/questions/67247175
复制相似问题