PDA

View Full Version : C help




MacDonaldsd
Mar 24, 2007, 05:44 AM
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.



toddburch
Mar 24, 2007, 07:15 AM
What do all the special characters in the scanf() do?

MacDonaldsd
Mar 24, 2007, 07:33 AM
What do all the special characters in the scanf() do?

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.

MacDonaldsd
Mar 24, 2007, 07:41 AM
OK I fixed it

I replaced the scanf with

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

Works perfect now

pilotError
Mar 24, 2007, 07:45 AM
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
Mar 24, 2007, 07:59 AM
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:


#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);
setbuf(stdin,NULL) ;
flag=1;

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


Todd

MacDonaldsd
Mar 24, 2007, 08:07 AM
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:


#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);
setbuf(stdin,NULL) ;
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
Mar 24, 2007, 08:10 AM
Also, since C uses ZERO for false, and anything else for true, you can shorten your "before" test to say


if (flag) { ... }

as long as you initialize flag to zero.

Todd

toddburch
Mar 24, 2007, 08:15 AM
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:


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
Mar 24, 2007, 08:37 AM
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?


do{
...
}while(strcmp(buffer,"EXIT99"));


Todd

MacDonaldsd
Mar 24, 2007, 08:40 AM
Thanks :D

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

Seems your brain is in coding mode today Todd.

program
May 26, 2007, 10:32 AM
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
May 26, 2007, 02:15 PM
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?


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.