1

I'm not a super beginner at programming, but I am relatively new to compiling and running code through the windows command prompt window.

#include <stdio.h>
int main(){
double LATER = 0, EARLIER = 0, RESULT = 0; // Declare and initialize LATER and EARLIER
                                           //to store operands and RESULT to hold a 
                                           //calculated result. Declare as type double.
char COMMAND = ' ';         // Declare COMMAND to store the last entered character. 
return 0;}
/*
while !(COMMAND == 'q')
{
    printf("Please enter operand(s), and/or an operator. \n (For division and  subtraction, ensure that the numerator or minuend is entered first): \n");
    scanf("%lf %lf %c \n",&EARLIER,&LATER,&OPERATOR); // Read in large float values for EARLIER and LATER and a character value for OPERAND
    printf("= %.3lf", RESULT);
}
*/

Whenever I compile this (GCC) and try to run it, my command prompt freezes -- that is, it accepts no input and just lingers around as a black screen (with prior console output still displayed). Anyone have any idea what I should do to fix this?

Update: Tried to run the same thing from CodeBlocks and had the same problem. Two console windows popped up, one ran the code and closed, the other stuck around and won't close. Still cannot run the program again without a restart.

DesertEagle_PWN
  • 81
  • 1
  • 2
  • 8
  • Your code looks simple enough and runs fine for me. – moeCake Feb 10 '14 at 02:31
  • This should not freeze your command prompt. It's simple and straight forward. – nonsensickle Feb 10 '14 at 02:31
  • share the complete problematic code. – mahendiran.b Feb 10 '14 at 02:45
  • That is literally all the code. I don't have any clue what's going on. – DesertEagle_PWN Feb 10 '14 at 02:52
  • Your code literally Does Nothing. You may feel "it freezes" because somehow you *expect* it to Do Something. However, it does not. – Jongware Apr 06 '14 at 12:24
  • 2
    What happens if you run this from the command line within a command window? Perhaps what you are seeing is behavior of the IDE's program-under-test launcher. Also, make sure you are really testing the posted version of your code, and not some older, staler one with the while() loop still in it. Finally you could see if your IDE will let you easily run it under a debugger (or perhaps you **have** run it under a debugger, but the debug control window is hiding somewhere or even not created) – Chris Stratton May 01 '14 at 22:26
  • Oh man oh man oh man--based on the last sentence of Arcane Engineer's answer, I think I finally get to be the guy to ask... **Have you tried turning it off and on again?** – Kyle Strand Mar 08 '15 at 10:34
  • https://www.youtube.com/watch?v=nn2FB1P_Mn8 – Kyle Strand Mar 08 '15 at 10:35
  • Given that this may be a bug, OP, please add info about your system: GCC version, OS, etc. – Kyle Strand Mar 08 '15 at 10:37
  • Also please edit the title to be less self-deprecating and more informative :D – Kyle Strand Mar 08 '15 at 10:37
  • @ChrisStratton FWIW, no, this is purely a GCC under win32 issue, since I'm getting the same using gcc on command line. – Engineer Mar 08 '15 at 12:18
  • @KyleStrand The OP hasn't been online in 5 months and low rep posters cannot change question titles as you surely know. – Engineer Mar 08 '15 at 12:19
  • @ArcaneEngineer Wow. I completely failed to notice the dates on any of these posts; having arrived here via one of the review queues, I assumed they were newish. Also, no, I did not realize that low-rep posters cannot edit the titles of their own questions. – Kyle Strand Mar 08 '15 at 13:43

2 Answers2

0

Good question. I firmly believe this is a bug in GCC win32 (in my case MinGW), although specifics are hard to determine, so I'm not sure if there's an existing bug report for it or I'd quote it here. It seems to affect very few people since I've seen no mention of it elsewhere.

Evidence I have a complex running application that I've been developing for some time; but every now and then, even with a trivial incremental change (one line, error-free), GCC will lock up in the command prompt (with Admin permissions) which sometimes can be closed, whereas other times neither the close button nor Task Manager nor Windows shutdown can release the process; attempting to run GCC in another command prompt fails similarly. I must then force reboot. Following reboot, some sort of cache or gcc application state must have been cleared, since compilation then goes off without a hitch. gcc -v returns:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=m
ingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto
--enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++
,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-l
ibstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gm
p-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --
with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-
libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/
mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)

P.S. According to this question, GCC is probably locked waiting on a kernel resource. This may be due to the way it attempts to access Windows kernel resources vs. Linux, insofar as having been ported from the latter.

P.P.S. If this is not a GCC bug then the OP and I have some OS issues in common.

Community
  • 1
  • 1
Engineer
  • 8,529
  • 7
  • 65
  • 105
  • Interesting. Does the specific example code posted by OP fail for you? Also, even if they're unpredictable, do failures seem deterministic? – Kyle Strand Mar 08 '15 at 10:27
  • @KyleStrand I haven't tried that code; I just answered this in passing. If there is any way to know what gcc is getting hitched on (e.g. via log files or the like), I'd be interested to look into these. – Engineer Mar 08 '15 at 10:41
-1

The code in the question simply :

  • Declares double variables LATER, EARLIER and RESULT.
  • Intialises them to 0.
  • Declares a char variable COMMAND.
  • Initialise it to a single space character.
  • exits successfully (with return value 0)

Hence you see is an empty console.

Check out the properly indented listing of the same code. Note that the main() function ends at the line following return 0; Also everything after that line is a multi-line comment for which executabel instructions are NOT generated in the output binary.

#include <stdio.h>

int main()
{

    double LATER = 0, EARLIER = 0, RESULT = 0;
    // Declare and initialize LATER and EARLIER
    //to store operands and RESULT to hold a 
    //calculated result. Declare as type double.

    // Declare COMMAND to store the last entered character. 
    char COMMAND = ' ';

    return 0;
}

/*
   while !(COMMAND == 'q') {
    printf("Please enter operand(s), and/or an operator. \n"
           "(For division and  subtraction,"
           " ensure that the numerator or minuend is entered first):");

    // Read in large float values for EARLIER and LATER and a character value for OPERAND
    scanf("%lf %lf %c \n",&EARLIER,&LATER,&OPERATOR);

   printf("= %.3lf", RESULT);
}
*/

You need to do something along the line of the following code, which:

  1. Accepts 3 inputs.

  2. Performs some operations to obtain result.

  3. Quits if 3rd input is the character "q" else asks for 3 inputs again.

Code for Step2 is missing. Its your TODO ;-)

#include <stdio.h>

int main()
{

    // Declare and initialize LATER and EARLIER
    //to store operands and RESULT to hold a 
    //calculated result. Declare as type double.
    double earlier = 0, later = 0, result = 0;

    // Declare COMMAND to store the last entered character. 
    char cmd = ' ';

    while (cmd != 'q') {
        printf("Please enter the following 3\n"
               "< operand1 operand2 operator >\n"
               "(For division and  subtraction,"
               " ensure that the numerator or minuend is entered first):\n");

        // Read in large float values for EARLIER and LATER and a character value for OPERAND
        scanf("%lf %lf %c", &earlier, &later, &cmd);

        // TODO: Add code to calculate "result" here

        // Print "result"
        printf("result= %.3lf\n", result);
    }

    return 0;
}
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
  • 2
    I appreciate the effort, but the problem is not that the code does nothing -- I am very much aware of this. However, when the return is executed should it not terminate my program and allow me to use the command line again? It is not doing that. Instead the command window cannot be interacted with or even terminated with task manager. It just stays there. – DesertEagle_PWN May 02 '14 at 15:56
  • Unless you hit "q" key for the cmd, the program will run forever. – TheCodeArtist May 02 '14 at 16:03
  • @TheCodeArtist OP isn't saying that *your* code runs forever; the problem is that *the do-nothing code is running forever*. Obviously, this should not happen. – Kyle Strand Mar 08 '15 at 10:28
  • @DesertEagle_PWN, you should edit your original question to clarify that this is what you mean by "freezing." (Yes, that's what one should *expect* freezing to mean, but sometimes people misuse it, and as a new user here you're unlikely to get the benefit of the doubt--see for instance Jongware's (fairly reasonable) comment.) – Kyle Strand Mar 08 '15 at 10:31