4

I am a newbie to C. I typed the following code in Visual Studio Express Dekstop 2014 edition but the output is forcefully closing in command prompt. I tried to add getchar(); not once not twice but thrice also but nothing changes. I also tried to change the settings in Project -> Properties -> Linker -> System -> Sub System -> Console (/SUBSYSTEM:CONSOLE) but now the output is showing in "without debugger mode" but in "debugger on mode" still output is closing in command prompt. I also tried to use system("pause"); before return 0; statement. The only way by which command prompt don't closes is by applying breakpoint at return 0; statement by pressing F9 key. What should I do in this situation?

#include <string.h> // for strlen() prototype
#define DENSITY 62.4 // human density in lbs per cu ft
int main()
{
    float weight, volume;
    int size, letters;
    char name[40]; // name is an array of 40 chars
    printf("Hi! What's your first name?\n");
    scanf_s("%s", name);
    printf("%s, what's your weight in pounds?\n", name);
    scanf_s("%f", &weight);
    size = sizeof name;
    letters = strlen(name);
    volume = weight / DENSITY;
    printf("Well, %s, your volume is %2.2f cubic feet.\n",
        name, volume);
    printf("Also, your first name has %d letters,\n",
        letters);
    printf("and we have %d bytes to store it.\n", size);

    getchar();
    getchar();

    return 0;
}
Coldplay
  • 133
  • 2
  • 2
  • 8

2 Answers2

4

Usually, with "debugger mode on" (F5which has a much longer startup and shutdown time and is slower), you're actually...debugging and you probably have breakpoints set in your program and can simply add one on the return 0; statement.

"Without debugger mode" (CTRL + F5) Visual Studio will keep the console application window open until you press a button.

This makes sense.

In your case the problem is that there are characters left in the input buffer.

Of course the are a lot of workarounds:

  • run the program from the command line (probably the best)
  • _getch() (from #include <conio.h>) this is non-standard
  • system("pause") this also isn't portable and it's a bit like burning your furniture for heat when you have a perfectly good thermostat on the wall
  • something like while (getchar() != '.');
manlio
  • 18,345
  • 14
  • 76
  • 126
  • So if I want to run the programme Visual Studio only, which method is the most appropriate? I think fourth option is the best. But I don't know how to use it can you please make a small basic C sample code and place this "while" statement in it. By the way thanks for the answer :) – Coldplay Sep 26 '14 at 15:31
  • I'd set the "subsystem" property to "console (/SUBSYSTEM:CONSOLE)" and start the program without debugging (CTRL+F5). – manlio Sep 26 '14 at 15:39
1

For what it's worth, on a Mac OSX it worked fine. To get the program to exit I had to hit return one time. So, the first getchar() already had something to pick up... a stray \n maybe.

The getchar() doesn't have to wait for input because it's already there, not picked up yet from your previous scanf statement.

Being that your operating on Microsoft, there's probably a \r\n sitting in stdin. So you're picking those up with each getchar(). Try calling getchar() a few more times and you'll see what I mean.

The reason the code worked for me is that unix-y environments only use \n, so the first getchar() picked that up and the the next getchar() waited for input.