pplzzz look for the error in my code!

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <conio.h>




int main(void)
{
     int num;
     int randnum;
     int tries;

	 
     char choice = 'y';
	 
	 

     randnum = 1 + rand() % 1000;
     tries = 1;

     printf("I have a number between 1 and 1000.\n");
     printf("Can you guess it?\n");
     printf("Please type your guess: ");
     scanf("%d", &num);
	 printf("\n\n");

    do {	
          while(num != randnum)
          {
               if(num < randnum)
               {
                    printf("Too low. Try again.\n\n");
                    printf("Next guess:\t ");
                    
                    scanf("%d", &num);
                    printf("\n");
               }

               if(num > randnum)
               {
                    printf("Too high. Try again.\n");
                    printf("Next guess: \t");
                   
                    scanf("%d", &num);
                    printf("\n"); }

			   tries ++;}
				
		       if(num == randnum)
               {
                printf("Excellent! You guessed the number with %d tries!\n\n", tries );
					                    
				printf("Would you like to play again (y or n)? ");
				scanf("%c", &choice);}}
	while(choice == 'y');
				

	getchar();

     return 0;
	}

Please tell us what is the problem.
the do while isnt working.... ie when

1
2
3
printf("Would you like to play again (y or n)? ");
				scanf("%c", &choice);}}
	while(choice == 'y');


this isnt working

and please tell how to exit when input is n
i don't know about C language but in C++ u could use this code and try to apply the same method on C:

1
2
3
4
5
6
char yesno;

do{
       cout<<"Would you like to play again (y or n)?  ";
       cin>>yesno;
}while(yesno=='Y' || yesno=='y');
man wrote:
c The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.
So you need to use scanf(" %c", &choice);

Later you will realize that you need to seed the rng.
And initialize 'num'
Topic archived. No new replies allowed.