C & .NET

I want to use my Visual Studio .NET (2003) to run simple ANSI C programs.  I can get them to run if I use the "Start without Debugging".  If I use the regular "Start" button, it runs, then immediately closes the console window.  Also, when I run the executable from the file folder, it opens & closes, the same way.  Is there some option(s) that can let me just run the program using "Start"
LVL 1
walangAsked:
Who is Participating?
 
josgoodConnect With a Mentor Commented:
If you are restricting yourself to ANSI -- and don't want to use a vendor-defined header -- you can use

#include <stdio.h>
and
getchar();

for example,

#include <stdio.h>

void main() {
   char c = getchar();
}

This requires you to enter a non-space character followed by Enter.

The other _getch() solution requires a single charatcer input.
0
 
josgoodConnect With a Mentor Commented:
It sounds like this is a console program and the program is running to completion and then closing.  

You can have the program stop, when finished, and wait for input.

#include <conio.h>

_getch();
as the last line before your closing brace.
0
 
walangAuthor Commented:
Looks like it does work, if I put the  _getch();  right before the  return 0;
What is it that this is doing?  Are there any other suggestions, besides code?
0
 
josgoodCommented:
Sorry, I didn't see your post before sending my last one.

_getch() waits for a keystroke.  If that doesn't prevent the program from exiting then either your code exits before then or you have a stream of stdin data that satisfies the _getch().

Another suggestion: Open a console window and run the program from the console.  Here are instructions -- please forgive me if you already know all this.

1)  Click the Start button.
2)  Click Run
3)  In the text area of the popup window, enter CMD and then press the Enter key
4)  Change directory to the folder containing your executable.  For example, if your program is in
     C:\MyProgram\Debug, then enter
     cd C:\MyProgram\Debug
5)  Type the name of your program, e.g. MyProgram.exe
    and then press the enter key.

Hope this helpls.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.