C help

Discussion in 'Mac Programming' started by MacDonaldsd, Mar 24, 2007.

  1. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    do{
    if(flag==1){
    printf("Before function called %s\n",buffer);

    }
    printf("Type in a command or EXIT99 to quit \n");

    scanf("%[^\n]",buffer);
    flag=1;

    }while(strcmp(buffer,"EXIT99")!=0);

    I can't work out what is wrong with my code, if i type EXIT99 it quits like it is supposed to, but if I type something else it loops forever and wont let me enter the information again.
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    What do all the special characters in the scanf() do?
     
  3. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    That means until the newline character is typed.

    So it reads in a whole sentence as if I used %c instead of %[^\n] it would only store the first word.
     
  4. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    OK I fixed it

    I replaced the scanf with

    while((letter = getchar()) !='\n'){
    buffer[counter++] = letter;
    }
    buffer[counter]='\0';

    Works perfect now
     
  5. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    It looks like its not pulling the new line out of the stdin stream and your getting the looping condition.

    if you change it to

    scanf("%s",buffer);

    it terminates as expected, but I suspect your looking for the entire line of input instead of just the parsed input (individual strings).

    Do you need to use scanf or could you just do a gets(buffer) which would do what you want?
     
  6. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    Bingo!

    I don't know C, but I just pulled out my book, and clearing the input stream fixes the problem without having to resort to use of other functions:

    Code:
    #include <stdio.h>
    char buffer[20] ; 
    int flag = 0 ; 
    int main (int argc, const char * argv[]) {
    	do{
    		if(flag==1){
    			printf("Before function called %s\n",buffer);
    		}
    		printf("Type in a command or EXIT99 to quit \n");
    
    		scanf("%[^\n]",buffer);
    		[b]setbuf(stdin,NULL) ;[/b]  
    		flag=1;
    		
    	}while(strcmp(buffer,"EXIT99")!=0);    return 0;
    }
    
    Todd
     
  7. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #7
    I was searching for that bit of code for ages yesterday

    Thank you very much :D
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    Also, since C uses ZERO for false, and anything else for true, you can shorten your "before" test to say

    Code:
    if (flag) { ... }
    as long as you initialize flag to zero.

    Todd
     
  9. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #9
    And... and... and... (I feel like I'm doing a code review at work...)

    You can trim the logic a bit in the main loop (as if it matters for this piece of code) by adding an else to the if:

    Code:
    if(flag){
    	printf("Before function called %s\n",buffer);
    }
    else flag = 1 ; 
    
    and then you can remove it from below. This way, the assignment only happens once.

    So, no, I'm not a C expert - but I can code!

    Todd
     
  10. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    This is a cool language.

    We can further improve it in the do/while loop test by leveraging strcmp(). strcmp() returns 1 of 3 values:

    <0 for string1 < string2 (ergo... TRUE)
    0 for string1 = string2 (FALSE)
    >0 for string1 > string2 (TRUE)

    So, we don't have to test for NOT EQUAL to FALSE, we can just test for TRUE (AKA equals), and if so, strcmp() returns FALSE (0). Get it?

    Code:
    	do{
    		...
    	}while(strcmp(buffer,"EXIT99")); 
    
    Todd
     
  11. thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #11
    Thanks :D

    All coding tips and tricks are very welcome. Knowledge is power :)

    Seems your brain is in coding mode today Todd.
     
  12. macrumors newbie

    Joined:
    May 26, 2007
    #12
    Hey u have to clear the input buffer before u use scanf

    u can do this by typing

    fflush(stdin);
    scanf("%[^\n]",buffer);

    :D :D
     
  13. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    Please, please, please don't do this. You may think it is clever, but it is not. Maybe it is even clever, but programming is not about being clever, it is about writing code that is correct and easy to read and maintain. You changed code that needed no explanation to code that needed an explanation.
     

Share This Page