Saturday, May 22, 2010

Microsoft visual c++?

whenever I type in a code and debug it, as soon as I hit enter the black screen disappears instantly, without me seeing the final solution. for example:





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;


int main()


{


/* Part A) Declare and initialize variables. */


double b, h, area;


/* get the height and base values from the keyboard*/


printf("Enter the base length of the triangle :");


scanf("%lf",%26amp;b);


printf("Enter the height of the triangle :");


scanf("%lf",%26amp;h);


/*calculate the area of the triangle */


area = .5*b*h;


printf("Area = %lf \n", area);


}





when i type in the values, the screen disappears, is there anyway I can keep the black screen up until I want to close it, instead of it closing automatically?

Microsoft visual c++?
quick answer, add a line at the end of the program asking for input. Such as:





cin%26gt; x;





This will force the screen to stay open until it receices a response from you.





-Greg
Reply:the way i do it for my console based programs is to use system("PAUSE");





so in your code it will be:


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;


int main()


{


/* Part A) Declare and initialize variables. */


double b, h, area;


/* get the height and base values from the keyboard*/


printf("Enter the base length of the triangle :");


scanf("%lf",%26amp;b);


printf("Enter the height of the triangle :");


scanf("%lf",%26amp;h);


/*calculate the area of the triangle */


area = .5*b*h;


printf("Area = %lf \n", area);





system("PAUSE"); //creates a system pause


}





What this does is output the "Press any key to continue" prompt (to which you press enter to continue). You don't need to include anything (although why not put "using namespace std;" after your #include directives?).





NOTE: This command ("system("PAUSE")") will make your console proggy only work on Windows machines!
Reply:You say you are trying to debug it, but are you using the debugger?





Instead of running the program, select Debug-%26gt;Start Debug (or you can just push F5).





Once your application starts, click in the left margin to set a breakpoint. When your code execution gets to the breakpoint, the code will stop executing, and you can examine all of your variables.





You will see that your application works, it is just completing before you can see the results. Therefore, you probably want to do what Greg et al recommend about adding another scanf().





Sorry if this sounds overly simple. I'm not meaning to be condescending, especially if you already ran through the debugger. The problem is that most students never learn to use the debugger and only use printf statements. I've just spent an awful lot of time retraining young programmers to use debuggers and other productivity tools. The sooner you learn, the easier it will be for you.
Reply:You should make the project type as Cmd line type project. Then you can run it from command line and see the output.
Reply:I dumped Windoze a long time ago but I seem to remember that system("pause"); works. System is a--cstlib I believe -- routine which calls an outside program. Pause is an MS-DOS program which prints "Press Any Key" on the screen and waits till you do to exit.


No comments:

Post a Comment