Thread: scanning a string twice???

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    scanning a string twice???

    I am trying to develop this retarded TIC TAC TOE game, out of boredom

    I came across this problem
    I have
    char choice
    defined and I use scanf("%s",&choice);
    but after that I can never use scanf("%s",&choice) without it returning null.

    Examine the following code
    Code:
    #include <stdio.h>
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
    int update(int player);
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i);
    int main()
    {
    
    
    	int scoreX, scoreY;
    	scoreX = 0;
    	scoreY = 0;
    	printf(" a | b | c\n");
    	printf("---+---+---\n");
    	printf(" d | e | f\n");
    	printf("---+---+---\n");
    	printf(" g | h | i\n\n");
    	printf("+-----------+\n");
    	update(1);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    	
    	update(2);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    	return 0;
    }
    
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i)
    {
    	printf("+-----------+\n\n");
    	printf(" %d | %d | %d\n",a,b,c);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n",d,e,f);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n\n",g,h,i);
    
    	printf("+-----------+\n\n\n");
    	return 0;
    }
    int update(int player)
    {
    	char choice;
    	printf("Player%d [%d] choose: ",player,player);
    	scanf("%c",&choice);
    	if(choice == 'a')
    		return a = player;
    	else if(choice == 'b')
    		return b = player;
    	else if(choice == 'c')
    		return c = player;
    	else if(choice == 'd')
    		return d = player;
    	else if(choice == 'e')
    		return e = player;
    	else if(choice == 'f')
    		return f = player;
    	else if(choice == 'g')
    		return g = player;
    	else if(choice == 'h')
    		return h = player;
    	else if(choice == 'i')
    		return i = player;
    	else 
    		return 0;
    
    	
    	return 0;
    }
    NO SOLUTION CODE PLEASE, just an explanation of what I need to do
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Clear the input buffer after your call to scanf:

    [edit]removed solution code as per request[/edit]

    -Prelude
    My best code is written with the delete key.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    and how might I clear the input buffer
    malloc?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I prefer

    while ( getchar() != '\n' );

    Because it is the most general way and effective for more than one character remaining in the stream.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I tried it and it works, sorta
    I don't think I placed it or used it right, how am I supposed to use that method, can you give me some example code?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    WHOA!!
    THIS IS ODD!!!
    I don't even know what I changed...
    Code:
    #include <stdio.h>
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
    int update(int player);
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i);
    int main()
    {
    
    
    	int scoreX, scoreY;
    	scoreX = 0;
    	scoreY = 0;
    	printf(" a | b | c\n");
    	printf("---+---+---\n");
    	printf(" d | e | f\n");
    	printf("---+---+---\n");
    	printf(" g | h | i\n\n");
    	printf("+-----------+\n");
    	update(1);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    	
    	update(2);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    
    	update(1);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    
    	update(2);
    
    	print_chart(a,b,c,d,e,f,g,h,i);
    	return 0;
    }
    
    int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i)
    {
    	printf("+-----------+\n\n");
    	printf(" %d | %d | %d\n",a,b,c);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n",d,e,f);
    	printf("---+---+---\n");
    	printf(" %d | %d | %d\n\n",g,h,i);
    
    	printf("+-----------+\n\n\n");
    	return 0;
    }
    int update(int player)
    {
    	char choice;
    	printf("Player%d [%d] choose: ",player,player);
    
    		scanf("%s",&choice);
    		if(choice == 'a')
    			return a = player;
    		else if(choice == 'b')
    			return b = player;
    		else if(choice == 'c')
    			return c = player;
    		else if(choice == 'd')
    			return d = player;
    		else if(choice == 'e')
    			return e = player;
    		else if(choice == 'f')
    			return f = player;
    		else if(choice == 'g')
    			return g = player;
    		else if(choice == 'h')
    			return h = player;
    		else if(choice == 'i')
    			return i = player;
    		else 
    			return 0;
    	
    	return 0;
    }
    works fine...
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > scanf("%s",&choice);
    You had this in your most recent post.

    > scanf("%c",&choice);
    In your first post, which you had problems with, you had this.
    The world is waiting. I must leave you now.

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I noticed that afterwards
    Thanks
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM