While trying the following exercise, I found out that it runs differently depending on if I use MinGw shell or the dos command. Here is the code:
From MinGw shell, backspace does not work very well. It actually goes back two spaces it seems. Line 30 does not seem to work when I execute from MinGw shell, and I have to use something like line 29 instead. Does this have anything to do with MinGw being based on posix?Code:1 #include <stdio.h> 2 #include <conio.h> 3 int main() 4 { 5 char sent[100]; 6 xputs ("Enter a sentence ..."); 7 xgets (sent); 8 printf ("\n\n"); 9 xputs (sent); 10 printf ("\n\n\n\n\nPress any key to exit..."); 11 getch(); 12 } 13 xputs (char *s) 14 { 15 while (*s) // I have not seen this before; I need to find out more about it 16 { 17 putch (*s); 18 s++; 19 } 20 } 21 xgets ( char *s) 22 { 23 int i; 24 char ch; 25 26 for (i=0; i<=98; i++) 27 { 28 ch = getche (); 29 //if (ch == 13) 30 if (ch == '\r') 31 {
Update: I found out why pressing backspace would cause the curser go back two spaces. I commented out the printf ("\b"); sentence and now backspace works the way it is supposed to.
My other question is about the while loop. What does line 15 do? Is that an infinite loop? Why not just do "while (1)"? I mean the author is using the break statement to leave the loop. Why use *s in the parenthesis?