[英]If statement inside of a while loop that fails to execute and exit program
所以我的主要問題是這段代碼的后半部分,尤其是:
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)
的相同問題Do you want to evaluate another teacher? (y/n)
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;
}
}
您確實需要while
和 for
循環嗎? for
循環在做什么?
在您的代碼中,您有兩個循環,並且稍加小心,您應該能夠將其簡化為其中一個循環。 特別是,您的return
似乎在while
循環內,這意味着它非常混亂。 弄清楚循環應該做什么,然后再去做。
if語句
if(loop != 'y' && loop != 3)
loop='n';
應該在for循環之外。 無論如何,您都不需要兩個循環。 您應該將其更改為僅使用一個循環。 並且此if
語句應該是該循環結束之前的最后一條語句。
您無緣無故地使用了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;
}
這是更正的版本。 我只關注您的問題,而不關注您的代碼可能存在的任何其他問題。
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 ***
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.