Thread: Invalid Output in certain Integers

  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Question Invalid Output in certain Integers

    Hi!

    For this program the computer will be guessing the number that the user have chosen, from 1 to 10.
    It will need to keep track of the number of guesses that the computer makes before it guesses correctly.

    This is what I have done by now:

    Code:
    int main() {
    	int num, count;
    	char x, y;
    do{
    	count = 0;
    	printf("Please pick a number between 1 and 10...\n");
    	printf("Ready to Play? ");
    	scanf("%s", &x);
    	
    	while(x == 'y' || x == 'Y')
    	{   num = 1;
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);
    		num++;
    		count++;
    		while(x == 'n' || x == 'N')
    		{
    			printf("Is your number %d? ", num);
    			scanf("%s", &x);
    			count++;
    			num++;
    		}
    		printf("You chose %d and it took me %d attempts.\n", num, count);
    		printf("\n");
    		printf("Shall we play again? ");
    		scanf("%s", &y);
    		printf("\n");
    		}
    }while(y == 'y' || y == 'Y');
    The output should look like this:

    Code:
    Please pick a number between 1 and 10...
    Ready to play? Y
    Is your number 3? N
    Is your number 8? N
    Is your number 5? Y
    You chose 5 and it took me 3 attempts.
    
    Shall we play again? Y
    
    Please pick a number between 1 and 10...
    Ready to play? Y
    Is your number 3? Y
    You chose 3 and it took me 1 attempt.
    
    Shall we play again? N
    But in this case I got an invalid output in the integer that displays the number since it doesn't increase the integer, here is the example:

    Code:
    Please pick a number between 1 and 10...
    Ready to Play? Y
    Is your number 1? N
    Is your number 1? N
    Is your number 1? Y
    You chose 1 and it took me 3 attempts.
    
    Shall we play again? Y
    
    Please pick a number between 1 and 10...
    Ready to Play? Y
    Is your number 1? Y
    You chose 1 and it took me 1 attempts.
    
    Shall we play again? N
    As you can see, the number doesn't increase...

    Also it will be very appreciated if someone can gave me some advises of how to make random or not sequential numbers.
    i.e. Instead of going 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, etc. The output be 3, 8, 5, 6, 2, 4, 1, 7, 10, 9.

    Thanks for your help and happy Programming!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't reset num to 1 at the start of the loop, if you don't want it to always be one. If you want random numbers, use the pseudo-random number generator rand().

  3. #3
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    Don't reset num to 1 at the start of the loop, if you don't want it to always be one. If you want random numbers, use the pseudo-random number generator rand().
    Where should I put the integer? Since if I remove it from where it is I get the next output:

    Code:
    Please pick a number between 1 and 10...
    Ready to Play? Y
    Is your number 0? N
    Is your number 1? N
    Is your number 1? N
    Is your number 1? Y
    You chose 1 and it took me 4 attempts.
    
    Shall we play again? N

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Before your loop, not inside your loop.

  5. #5
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    Before your loop, not inside your loop.
    OK, I tried 3 different ways outside the loop without success, can you specify exactly where between which lines should I declare the integer.

    Thank you.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The line that currently says:
    Code:
    
    
    right before the loop.

  7. #7
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    The line that currently says:
    Code:
    
    
    right before the loop.
    Okay, well if you try to run the code in an editor, probably you would find that is not that easy.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by georgio777 View Post
    Okay, well if you try to run the code in an editor, probably you would find that is not that easy.
    Look at your first post. There is only one blank line in your code. How hard can it be to find the only blank line?

  9. #9
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    Look at your first post. There is only one blank line in your code. How hard can it be to find the only blank line?
    Okay, I tried that, still getting ones

    What is wrong? I don't get it

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Compare and contrast: If you make the change as mentioned, you should have this:
    Code:
    int main() {
    	int num, count;
    	char x, y;
    do{
    	count = 0;
    	printf("Please pick a number between 1 and 10...\n");
    	printf("Ready to Play? ");
    	scanf("%s", &x);
    	num = 1;
    	while(x == 'y' || x == 'Y')
    	{  
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);
    		num++;
    		count++;
    		while(x == 'n' || x == 'N')
    		{
    			printf("Is your number %d? ", num);
    			scanf("%s", &x);
    			count++;
    			num++;
    		}
    		printf("You chose %d and it took me %d attempts.\n", num, count);
    		printf("\n");
    		printf("Shall we play again? ");
    		scanf("%s", &y);
    		printf("\n");
    		}
    }while(y == 'y' || y == 'Y');
    Now there are other issues here, specifically the fact that using %s with the address of a character will overwrite one byte of another variable with the value 0. Presumably you want to use
    Code:
    scanf(" %c", &y);
    instead (and the same with x).

  11. #11
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    Compare and contrast: If you make the change as mentioned, you should have this:
    Code:
    int main() {
    	int num, count;
    	char x, y;
    do{
    	count = 0;
    	printf("Please pick a number between 1 and 10...\n");
    	printf("Ready to Play? ");
    	scanf("%s", &x);
    	num = 1;
    	while(x == 'y' || x == 'Y')
    	{  
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);
    		num++;
    		count++;
    		while(x == 'n' || x == 'N')
    		{
    			printf("Is your number %d? ", num);
    			scanf("%s", &x);
    			count++;
    			num++;
    		}
    		printf("You chose %d and it took me %d attempts.\n", num, count);
    		printf("\n");
    		printf("Shall we play again? ");
    		scanf("%s", &y);
    		printf("\n");
    		}
    }while(y == 'y' || y == 'Y');
    Now there are other issues here, specifically the fact that using %s with the address of a character will overwrite one byte of another variable with the value 0. Presumably you want to use
    Code:
    scanf(" %c", &y);
    instead (and the same with x).
    I tried that and I got something unexpected, the program terminates by itself after the input is declared. Are you sure about this?

  12. #12
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Code:
    int main() {
    	int num, count;
    	char x, y;
    do{
    	count = 0;
    	printf("Please pick a number between 1 and 10...\n");
    	printf("Ready to Play? ");
    	scanf("%s", &x);
    	
    	while(x == 'y' || x == 'Y')
    	{   num = 1;
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);
    		num++;
    		count++;
    		while(x == 'n' || x == 'N')
    		{
    			printf("Is your number %d? ", num);
    			scanf("%s", &x);
    			count++;
    			num++;
    		}
    		printf("You chose %d and it took me %d attempts.\n", num, count);
    		printf("\n");
    		printf("Shall we play again? ");
    		scanf("%s", &y);
    		printf("\n");
    		}
    }while(y == 'y' || y == 'Y');
    so,

    Code:
    	scanf("%s", &x);
    	
    	while(x == 'y' || x == 'Y')
    	{   num = 1;
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);
    put that num =1 here and try


    Code:
    	scanf("%s", &x);
    	num = 1;
    
    	while(x == 'y' || x == 'Y')
    	{   
    		printf("Is your number %d? ", num);
    		scanf("%s", &x);

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by georgio777 View Post
    I tried that and I got something unexpected, the program terminates by itself after the input is declared. Are you sure about this?
    Yes, because the program does not terminate by itself after the input is declared. Compare what you have with what is posted, carefully.

  14. #14
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    Yes, because the program does not terminate by itself after the input is declared. Compare what you have with what is posted, carefully.
    With %c I got more faulty behaviors, it seems to skip the scanf function.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    dont use the %c......use %s.....that seems to work..

    Code:
    int main()
    {
    	int num=1, count=0;
    	char x, y;
    do{
    	printf("Please pick a number between 1 and 10...\n");
    	printf("Ready to Play? ");
    	scanf("%s", &x);
    
    	while(x == 'y' || x == 'Y')
    	{
    	    printf("Is your number %d? ", num);
    		scanf("%s", &x);
    
    		while(x == 'n' || x == 'N')
    		{
    		    	printf("Is your number %d? ", num);
    			scanf("%s", &x);
                            	num++;
                              count++		
    		   }
    	}
    		printf("You chose %d and it took me %d attempts.\n", num-1, count); // the while loop exits
    		printf("\n");
    		printf("Shall we play again? ");
    		scanf("%s", &y);
    		printf("\n");
    		}
    }while(y == 'y' || y == 'Y');
    
    }
    its still an unfinished code......still you need to fix the count

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM