所以我的主要问题是这段代码的后半部分,特别是:
printf("Do you want to evaluate another teacher? (y/n) : ");
printf("\n");
scanf(" %c", &loop);
if(loop != 'y' && loop != 3)
loop='n';
我正在创建一个进行调查的程序。但是,学生最多只能进行3次调查,并且在每次调查结束时都会询问他们是否要预先形成另一次调查。我们的问题发生在他们进行了第三次调查之后。之后,调查提示相同的Do you want to evaluate another teacher? (y/n)
问题,如果学生回答y
,代码会循环回来并让他们进行另一次调查,而不是听我说三次调查后的状况,程序应自动结束。如果他们回答n
,它仍然会重新进入循环!
我非常困惑如何让我的代码中的这部分与我的其余代码齐声并共存。非常感谢帮助!
如果您愿意,这是我的全部代码:
#include <stdio.h>
int main()
{
int i = 0;
char loop='y';
while(loop == 'y' ){
for(i = 0; i<4; i++){
int num1,num2,num3,num4,num5,num6,num7,num8;
int result;
int input;
char name[30];
char teacher[30];
printf("Enter your name : ");
scanf("%s", &name);
printf("\n");
printf("Which teacher do you want to evaluate : ");
scanf( "%s/n", &teacher);
printf("\n");
printf("Answer with 1 for Never upto 7 for Frequently\n");
printf("\n");
printf("How often does the teacher indicate where the class is going? \n ");
scanf("%d",&num1);
printf("How often does the teacher explain material clearly? \n ");
scanf("%d",&num2);
printf("How often is the teacher available outside of class? \n ");
scanf("%d",&num3);
printf("How often does the teacher provide helpful comments on papers and exams? \n ");
scanf("%d",&num4);
printf("How often does the teacher stimulate interest in material? \n ");
scanf("%d",&num5);
printf("How often does the teacher adjust the pace of class to the students' level of understanding? \n ");
scanf("%d",&num6);
printf("How often does the teacher effectively encourage students to ask questions and give answers? \n ");
scanf("%d",&num7);
printf("How is the teacher tolerant of different opinions expressed in class? \n ");
scanf("%d",&num8);
printf("******************************************************************************\n");
printf("******************************************************************************\n");
printf("Student's name : %s.\n", name);
printf("Teacher's name : %s.\n", teacher);
printf("How often does the teacher indicate where the class is going: %d\n",num1);
printf("How often does the teacher explain material clearly : %d\n",num2);
printf("How often is the teacher available outside of class : %d\n",num3);
printf("How often does the teacher provide helpful comments on papers and exams: %d\n",num4);
printf("How often does the teacher stimulate interest in material: %d\n",num5);
printf("How often does the teacher adjust the pace of class to the students' level of understanding: %d\n",num6);
printf("How often does the teacher effectively encourage students to ask questions and give answers: %d\n",num7);
printf("How is the teacher tolerant of different opinions expressed in class: %d\n",num8);
printf("******************************************************************************\n");
printf("******************************************************************************\n");
printf("Do you want to evaluate another teacher? (y/n) : ");
printf("\n");
scanf(" %c", &loop);
if(loop != 'y' && loop != 3)
loop='n';
}
return 0;
}
}
答案 0 :(得分:1)
您真的需要while
和 for
循环吗? for
循环在做什么?
在你的代码中,你有两个循环,并且有点小心,你应该能够把它归结为其中一个循环。特别是,您的return
似乎在while
循环内,这意味着它......非常困惑。弄清楚你的循环应该做什么,然后再去做。
答案 1 :(得分:1)
if语句
if(loop != 'y' && loop != 3)
loop='n';
应该在for循环之外。无论如何,你不需要两个循环。您应该将其更改为仅使用单个循环。这个if
语句应该是该循环结束之前的最后一个语句。
答案 2 :(得分:1)
你没有理由地使用了2个循环,你每次都要求提供用户名....
您应该如何做到这一点:
#include <stdio.h>
int main()
{
int i = 0;
char loop;
int count=0; //new variable to know how many times he filled the survey
//You dont need to ask about your name everytime...
char name[30];
printf("Enter your name : ");
scanf("%s", &name);
printf("\n");
do{
int num1,num2,num3,num4,num5,num6,num7,num8;
int result;
int input;
char teacher[30];
printf("Which teacher do you want to evaluate : ");
scanf( "%s/n", &teacher);
printf("\n");
printf("Answer with 1 for Never upto 7 for Frequently\n");
printf("\n");
printf("How often does the teacher indicate where the class is going? \n ");
scanf("%d",&num1);
printf("How often does the teacher explain material clearly? \n ");
scanf("%d",&num2);
printf("How often is the teacher available outside of class? \n ");
scanf("%d",&num3);
printf("How often does the teacher provide helpful comments on papers and exams? \n ");
scanf("%d",&num4);
printf("How often does the teacher stimulate interest in material? \n ");
scanf("%d",&num5);
printf("How often does the teacher adjust the pace of class to the students' level of understanding? \n ");
scanf("%d",&num6);
printf("How often does the teacher effectively encourage students to ask questions and give answers? \n ");
scanf("%d",&num7);
printf("How is the teacher tolerant of different opinions expressed in class? \n ");
scanf("%d",&num8);
printf("******************************************************************************\n");
printf("******************************************************************************\n");
printf("Student's name : %s.\n", name);
printf("Teacher's name : %s.\n", teacher);
printf("How often does the teacher indicate where the class is going: %d\n",num1);
printf("How often does the teacher explain material clearly : %d\n",num2);
printf("How often is the teacher available outside of class : %d\n",num3);
printf("How often does the teacher provide helpful comments on papers and exams: %d\n",num4);
printf("How often does the teacher stimulate interest in material: %d\n",num5);
printf("How often does the teacher adjust the pace of class to the students' level of understanding: %d\n",num6);
printf("How often does the teacher effectively encourage students to ask questions and give answers: %d\n",num7);
printf("How is the teacher tolerant of different opinions expressed in class: %d\n",num8);
printf("******************************************************************************\n");
printf("******************************************************************************\n");
count++;
if (count<3) {
printf("Do you want to evaluate another teacher? (y/n) : ");
printf("\n");
scanf("%s", &loop);
}
} while (loop=='y' && count<3);
return 0;
}
答案 3 :(得分:0)
这是更正后的版本。我只专注于您提出的问题,而不是您的代码可能存在的任何其他问题。
int main() {
int i;
char loop;
for(i = 0; i < 3; i++) {
if(i != 0) { //After the first survey, prompt before we interview them
printf("Do you want to evaluate another teacher? (y/n) : \n");
scanf(" %c", &loop);
if(loop != 'y')
break; //Quit the loop
}
//*** Survey prompts here ***
}
}