Need help debugging this simple yet elusive problem

This is a discussion on Need help debugging this simple yet elusive problem within the C Programming forums, part of the General Programming Boards category; Simple scanning in an input with whitespaces except I can't figure out why the code is enter my 'if' statement ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Need help debugging this simple yet elusive problem

    Simple scanning in an input with whitespaces except I can't figure out why the code is enter my 'if' statement when i don't input anything. Can anyone see the problem?

    Code

    Code:
       while(exitStatus != 0){
    
    
    	printf("Say(0 to exit): ");
    	//scanf("%s", &message);  
    	 
    
    	int inc = 0;
    	   while((ch = getchar()) != '\n')
    		{
    	   	message[inc++] = ch;}	
    	   
    	   message[inc] = '\0';
    	   inc = 0;
    	   
    	while(message[inc] != '\0'){
    	   	putchar(message[inc++]);}
    	
    
    	//printf("\n");
    	
    	if(strcmp(message, "0") == 0){
    		exitStatus = 0;
    		printf("Exiting", exitStatus);
    		break;
    	}else{
    	char *ptr = message;
    	
    	send(sockfd,ptr,strlen(message),0);
    	printf("Sent '%s' to server.\n",message);
    	}
    
       }
    and the output is:


    Say(0 to exit):
    Sent '' to server.
    Say(0 to exit):
    I don't know where the " '' " is coming from since nothing is getting passed in. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so somehow there is a '\n' getting passed in but i don't know where :\

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    First thought I have is you should initialize your array to some default data, say all 1's. I say this since you are searching for "0" to compare against to exit.

  4. #4
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    994
    I assume that you have a scanf right before you hit this bit of code? It's a common problem with back to back scanfs. You might want to look up "clearing the stdin buffer". Remember to not use fflush(stdin), as this is undefined.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    well I am using a scanf earlier in my code, but I am not using a scanf in this portion as I have it commented out. If I uncomment the scanf it works fine but will not permit the use of white spaces. Unless there is some correlation between the scanf used earlier and the getchar() function.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() can leave a newline char in the keyboard buffer, and muck up the next part of your program that gets data from the keyboard.

    You can pull the troublesome newline char off the buffer by adding a variable=getchar() line of code, right after the scanf().

    If your program stops, awaiting your keyboard input, then you don't have a newline waiting in the buffer. If it continues on, then 1 char (probably the newline), will have been pulled off the buffer.

    If your scanf()'s are only for numbers, then it's no problem, and you can ignore this. When scanning for digits, scanf() will skip over newlines, anyway. Char's are the usual problems, because a newline char is a char itself. scanf() won't skip over a newline if it's expecting a char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  2. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  3. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. debugging problem
    By _ag0nizer in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21