Thread: MinGw shell vs. Microsoft command

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    MinGw shell vs. Microsoft command

    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:


    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 {
    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?

    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?
    Last edited by zolfaghar; 05-29-2016 at 03:54 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by zolfaghar View Post
    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?
    For that, you need to understand how integer values are converted into boolean logic in C++( they're carried from C that way ):
    Code:
    if (c)
    if (!c)
    is equivalent to:
    Code:
    if (c != 0)
    if (c == 0)
    Last edited by GReaper; 05-29-2016 at 04:54 PM.
    Devoted my life to programming...

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Usually someone else has the pleasure of doing so, but since no one did, I'll do it.

    conio.h is old, non-standard, and came from the MS-DOS era. If you are learning to code from a book that mentions conio.h, you should search for a newer, better, (probably free) one online.

  4. #4
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by Epy View Post
    Usually someone else has the pleasure of doing so, but since no one did, I'll do it.

    conio.h is old, non-standard, and came from the MS-DOS era. If you are learning to code from a book that mentions conio.h, you should search for a newer, better, (probably free) one online.
    I have switched. Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Epy View Post
    conio.h is old, non-standard, and came from the MS-DOS era.
    If you haven't yet found a library that gives you key presses without the need to press enter each time, you could use ncurses on Linux/Unix, or pdcurses on Windows. The API is different, but there's tons of functionality there, that you might find useful, and those two libraries are (mostly) source compatible, so code written for Windows will compile on Linux, and vice versa, as long as it doesn't use anything OS specific.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by Elkvis View Post
    If you haven't yet found a library that gives you key presses without the need to press enter each time, you could use ncurses on Linux/Unix, or pdcurses on Windows. The API is different, but there's tons of functionality there, that you might find useful, and those two libraries are (mostly) source compatible, so code written for Windows will compile on Linux, and vice versa, as long as it doesn't use anything OS specific.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Microsoft Shell functions speed
    By @nthony in forum Windows Programming
    Replies: 6
    Last Post: 05-31-2009, 08:21 AM
  2. Command Prompt Shell
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 20
    Last Post: 09-19-2007, 02:36 PM
  3. SSH Secure Shell to Microsoft Development Environment
    By s_jsstevens in forum C Programming
    Replies: 2
    Last Post: 05-08-2007, 02:38 PM
  4. Convert Microsoft LIB to MingW compatible lib
    By tigs in forum Windows Programming
    Replies: 0
    Last Post: 07-20-2004, 06:53 PM
  5. using c++ to use a ms-dos shell command
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2002, 06:23 PM

Tags for this Thread