Easy programming question...

I have just started writing basic programs in school, and I am not very familiar with the whole process.
Anyway, we are using Microsoft Visual C++ 6.0.
The problem I seem to be having is that when I try to build the *.exe, it builds, but when it is supposed to display the answer, the DOS window disappears. I don’t know what I did wrong, but it must have something to do with the actual building of the exe file, because it works perfectly in the program (MS Visual C++) when I execute it there. when I try to open it independantly, however, it closes immediately.
I will list all the file that are in the same directory: (the name of the program will be “circumference”)
circumference.dsp
circumference.ilk
circumference.obj
circumference.pch
circumference.pdb
vc60.idb
vc60.pdb
circumference.opt
circumference.exe

Any help is appreciated, since my teacher has no idea what I did wrong. All the code is correct, according to him and his TA, though.
Thanks

Here is the actual code.(don’t laugh! I started monday)
//calculate cicumference of a circle
#include <iostream>
using namespace std;

int main()
{
//declare variables
float circumference = 0.0;
float radius = 0.0;
float pi = 0.0;

//enter input items
cout &lt;&lt; "Enter radius: ";
cin &gt;&gt; radius;

//calculate circumference
pi = 3.1415;
circumference = 2 * pi * radius;
//display output item
cout &lt;&lt; "The circumference of the circle is: " &lt;&lt; circumference &lt;&lt; endl;

return 0;

}
//end of main function

thanks again

What kind of program did you create? Was it a console program? A console program runs in the command-line window. Usually console programs are the first ones you learn.

Zev Steinhardt

Yes, it runs in the command line window. Well, technically, it doesn’t, but that is what pops up when you click the exe. Then when you enter the variable (in this case the radius) and hit ‘Enter’ it closes the command line window.
Is there some specal way to build the exe or did I code something wrong?

No. That’s the way a console application runs.

If you want the command line window to remain open after the application finishes then run it initially from the command line (don’t click the application from Windows Explorer. Open a command line window, go to the directory where your program is and manually call your program from the command line.

Zev Steinhardt

If you click on a command-line program .exe from Explorer, then Windows pops up a console window, runs the program, and closes the window immediately. It doesn’t pause to let you see the answer. Maybe this is what is happening.

To see if it is, add a dummy input just before you return from main:


cin >> dummy;
return;
}

You are basically adding a pause at the end of the program.

If this fixes it, then you just need to run your program from inside a command shell (“command” for Win95 or Win98 or “cmd” for WinNT or Win2000).

Good luck!

Ah, okay. Makes sense. Thank you.
That is the only way to make it stay open, though? There is no code I can write into the program to make the window stay open by simply clicking the app from Explorer? I know this may sound stupid, but I am just learning.

CaveMike responded while I was posting… I’ll try what he said.

Thank you all.

I think a little perspective would help. Back in the olden days (actually, after I got out of college, because I did all my programming on punch cards), a computer was a simple thing. The OS provided a file system and console I/O. You’d type a command, which is the name of a program, into the command line, and your program would print out the answer. Real-world programs would read and write to files, but it was the command line that you were working with.

Nowadays, computers are simple for the user. But that user-oriented simplicity comes at the cost of making the programmer’s job very difficult. Writing a program that deals with all of the messages that the Windows system sends to applications is a major job in itself, even for a program that does no real work. That’s why, when you just start out, you pretend that you’re in those old days, with console I/O. Think of it as the baby pool. You’re not ready to dive off the diving board yet.

Back in my day, we didn’t have all these fancy “icons” and “windows.” We had to program with ones and zeroes. And sometimes we didn’t even have ones. I wrote a whole database application using only zeroes.

Be careful you don’t enter in letters instead of numbers if you add CaveMike’s suggestion to the end. Infinite loop if you do. Puts the input stream into an error state and it is hell to get out of.

If you’re working inside of a debugger, put a breakpoint in at the return statement and execute the debugger-built executable. That’s what I always do.

Nevermind, not an infinite loop, but it will just exist again like it was before.

Here is what worked: (Thanks CaveMike)

//calculate cicumference of a circle
#include <iostream>
using namespace std;

int main()
{
//declare variables
float circumference = 0.0;
float radius = 0.0;
float pi = 0.0;

//enter input items
cout << "Enter radius: ";
cin >> radius;

//calculate circumference
pi = 3.1415;
circumference = 2 * pi * radius;
//display output item
cout << "The circumference of the circle is: " << circumference << endl;
cin >> dummy;
return 0;
}
//end of main function
Stays open after you insert the radius. Heh, I’ll get to teach my teacher something on monday!

Just a quick note that this is not a programming problem per se, but just an intrinsic behavior of how Windows and/or Visual C++ runs console programs. Using the CodeWarrior(*) C++ IDE, for instance, I can write a text-based console program, and the default project template will link in a “console window” library that opens a window and keeps it open until I explicitly choose “Quit” from the menu.

(* = At least, this is for CodeWarrior on the Macintosh, though I believe the Windows version will behave the same way)