0

So my main issue is on the last half of this code in particular this:

      printf("Do you want to evaluate another teacher? (y/n) : ");
      printf("\n");
      scanf(" %c", &loop);
      if(loop != 'y' && loop != 3)
        loop='n';

I'm creating a program that takes a survey. However, the student can only preform up to 3 surveys and they are asked at the end of every survey if they'd like to preform another. My problem occurs after they have preformed a third survey. After that the survey prompts the same question of Do you want to evaluate another teacher? (y/n) and if the student answers ythe code loops back and lets them take another survey instead of listening to my conditions that state after three surveys the program should automatically end. If they answer n it still re-enters the loop too!

I'm extremely confused as to how to get this portion of my code to coexist with the rest of my code in unison and work. Help would be greatly appreciated!

Here's the entirety of my code if you'd like:

#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;
  }
}
4
  • 1
    if(loop != 'y' && loop != 3) that's not logical. Is loop the counter or the variable to store user answer in? choose. Commented May 8, 2017 at 19:25
  • 1
    Shouldn't if(loop != 'y' && loop != 3) loop='n'; be outside the for loop? Commented May 8, 2017 at 19:26
  • 1
    scanf( "%s/n", &teacher); is interestingly a problem, yet not a problem. (Drop the /n). Use scanf( "%29s", teacher); Commented May 8, 2017 at 19:28
  • 1
    It looks like the return statement is inside the while loop, and that the if-statement should be outside the for loop. Edit: Based on what your description of the problem, it sounds like you only want the for-loop, and that the while loop isn't necessary. 'loop != 3' should probably be 'i != 3' Commented May 8, 2017 at 19:29

4 Answers 4

1

Do you really need both the while and the for loop? What is the for loop doing?

In your code, you have both loops, and with a little care, you should be able to get it down to just one of them. In particular, it looks like your return is inside the while loop, which means it's ... pretty confused. Work out what your loops should be doing and have another go.

Sign up to request clarification or add additional context in comments.

Comments

1

The if statement

if(loop != 'y' && loop != 3)
        loop='n';

should be outside the for loop. In any case, you don't need both the loops. You should change it to use only a single loop. And this if statement should be the last statement before the end of that loop.

Comments

1

You used 2 loops without reason, also you asked for the name of the user everytime....

Here is how you should do it :

#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;
}

Comments

0

Here is a corrected version. I am only focusing on what you asked about, not any other issues that might exist with your code.

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 ***
  }
}

2 Comments

This is actually a case where a do...while loop is probably a better choice. Using the if this way, special knowledge about the meaning of the iterations variable i is in two different places when it could be in just one, which is unfortunate. What if the number of valid iterations changes later to 5 instead of 3? You’d have to loop while i < 5 and check if i == 4. Awkward. What if the number of valid iterations becomes variable, based on some other input or program state?
@Craig Good point about special knowledge being required, but I'm still not sure a do...while loop is best. Take a look at my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.