Very Beginner Question(While Loop)

Hello, I'm VERY new to C and C++ and I am trying to write code for a college course. I'm not asking anyone to write the code, I just do not understand why my while loop is not working? The program seems to stop after saying the instructions.

Any help appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 int main(){
    int tooLow = 0;
    int tooHigh = 100;
    int guess = 50;
    int tries = 1;
    char answer[20] = "'x'";
  printf("Think of a number between 1-100. \n");
  printf("I'm going to guess the number in 7 or less tries. \n");
  printf("Tell me my guess is too high by typing H, tell me it's too low by typing L, or tell me it's correct with C. \n");
  while (*answer != 'C');
    printf("Is your number %d? Try #%d \n", guess, tries);
    scanf("%s", answer);
      tries++;
      if (*answer == 'H');
        tooHigh = guess;
        guess = rand()%(tooLow-tooHigh);
      if (*answer == 'L');
        tooLow == guess;
        guess = rand()%(tooLow-tooHigh);
}//end main 
your code, indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() {
	int tooLow = 0;
	int tooHigh = 100;
	int guess = 50;
	int tries = 1;
	char answer[20] = "'x'";
	printf("Think of a number between 1-100. \n");
	printf("I'm going to guess the number in 7 or less tries. \n");
	printf("Tell me my guess is too high by typing H, tell me it's too low by "
	       "typing L, or tell me it's correct with C. \n");
	while(*answer != 'C')
		; //NOTE
	printf("Is your number %d? Try #%d \n", guess, tries);
	scanf("%s", answer);
	tries++;
	if(*answer == 'H')
		; //NOTE
	tooHigh = guess;
	guess = rand() % (tooLow - tooHigh);
	if(*answer == 'L')
		; //NOTE
	tooLow == guess;
	guess = rand() % (tooLow - tooHigh);
} // end main 
you've got semicolons as the body of the while and the ifs.
If you want several statements, then enclose them with {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() {
	int tooLow = 0;
	int tooHigh = 100;
	int guess = 50;
	int tries = 1;
	char answer[20] = "'x'";
	printf("Think of a number between 1-100. \n");
	printf("I'm going to guess the number in 7 or less tries. \n");
	printf(
	    "Tell me my guess is too high by typing H, tell me it's too low by "
	    "typing L, or tell me it's correct with C. \n");
	while(*answer != 'C') { //NOTE
		printf("Is your number %d? Try #%d \n", guess, tries);
		scanf("%s", answer);
		tries++;
		if(*answer == 'H'){ //Open block
			tooHigh = guess;
			guess = rand() % (tooLow - tooHigh);
		} //Closing block
		if(*answer == 'L'){
			tooLow == guess;
			guess = rand() % (tooLow - tooHigh);
		}
	} //end while
} // end main 
Topic archived. No new replies allowed.