Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
OK I fixed it

I replaced the scanf with

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

Works perfect now
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
It looks like its not pulling the new line out of the stdin stream and your getting the looping condition.

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
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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

I was searching for that bit of code for ages yesterday

Thank you very much :D
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Thanks :D

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

Seems your brain is in coding mode today Todd.
 

program

macrumors newbie
May 26, 2007
1
0
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.