Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top