Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,393 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,134 people online right now. Registration is fast and FREE... Join Now!




Prevent Command Window Closure

 
Reply to this topicStart new topic

Prevent Command Window Closure

TxMoose
28 Oct, 2006 - 07:36 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 18


My Contributions
OK, I'm new to C++. Brand new. I'm trying to teach myself. I have the Fundamentals of C++: Understanding Programming and Problem Solving by Kenneth A. Lambert and Douglas W. Nance (copyright 1998) textbook. I'm using the Borland C++ Builder 5. I've copied a program straight from the book, character for character, and I've even copied and pasted a few C++ code snippets from this site. The problem I've had is that the commandprompt window, after running the program, closes immediately, thus I cannot see the results of the computations. The program from the book is a simple "checkbook balancing" program.

CODE

//Program file:  chbook.cpp
//This program updates a checkbook

#include <iostream.h>
#include <iomanip.h>

int main ()
{
        double starting_balance, ending_balance, trans_amount;
        char trans_type;

        //module for getting the data
        cout << "Enter the starting balance and press <Enter>:  ";
        cin >> starting_balance;
        cout << "Enter the transaction type (D) deposit or (W) withdrawl ";
        cout << "and press <Enter>:  ";
        cin >> trans_type;
        cout << "Enter the transaction amount and press <Enter>:  ";
        cin >> trans_amount;

        //module for performing computations
        if(trans_type == 'D')
                ending_balance = starting_balance + trans_amount;
        else
                ending_balance = starting_balance - trans_amount;

        //module for displaying results
        cout << setiosflags (ios::fixed | ios::showpoint | ios:: right)
             << setprecision(2);
        cout << endl;
        cout << "Starting balance     $" << setw(8)
             << starting_balance << endl;
        cout << "Transaction          $" << setw(8)
             << trans_amount << setw(2) << trans_type << endl;
        cout << setw(30) << "-------" << endl;
        cout << "Ending balance       $" << setw(8)
             << ending_balance << endl;

        return 0;
}


Is anything wrong with this code? Is there something I'm missing? Is there a different way to run these programs?

Moose
User is offlineProfile CardPM
+Quote Post

UMTopSpinC7
RE: Prevent Command Window Closure
28 Oct, 2006 - 08:23 PM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 47


My Contributions
an easy way to make the program wait is just to do this at the end

CODE

             << ending_balance << endl;
                char temp;
                cin >> temp;

        return 0;
}


you just read in a value that doesn't matter

This post has been edited by Dark_Nexus: 29 Oct, 2006 - 10:50 AM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Prevent Command Window Closure
28 Oct, 2006 - 08:35 PM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
1) Change your headers to standard headers
2) To stop the output window check this code snippet Stopping the output window
User is offlineProfile CardPM
+Quote Post

TxMoose
RE: Prevent Command Window Closure
28 Oct, 2006 - 08:39 PM
Post #4

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 18


My Contributions
What are standard headers? That code I have there is straight copied, character for character, ver batim from the text book I'm using.

Moose
User is offlineProfile CardPM
+Quote Post

Xing
RE: Prevent Command Window Closure
29 Oct, 2006 - 12:21 AM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(TxMoose @ 29 Oct, 2006 - 10:09 AM) *

What are standard headers? That code I have there is straight copied, character for character, ver batim from the text book I'm using.

Moose


Check this out
What's the difference between <xxx> and <xxx.h> headers?
and
Should I use using namespace std in my code?

This post has been edited by Xing: 29 Oct, 2006 - 12:22 AM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Prevent Command Window Closure
29 Oct, 2006 - 05:01 AM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Xing @ 29 Oct, 2006 - 01:21 AM) *

QUOTE(TxMoose @ 29 Oct, 2006 - 10:09 AM) *

What are standard headers? That code I have there is straight copied, character for character, ver batim from the text book I'm using.

Moose


Check this out
What's the difference between <xxx> and <xxx.h> headers?
and
Should I use using namespace std in my code?


Moose,

The book you are working from seems to be a bit dated. Modern "standard" header, Xing is quiter right... today the accepted standard headers look like this:

CODE
#include <iostream>
#include <cstdlib>

using namespace std;

User is offlineProfile CardPM
+Quote Post

TxMoose
RE: Prevent Command Window Closure
29 Oct, 2006 - 07:11 PM
Post #7

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 18


My Contributions
Thank you, Xing and gregoryH. I'll read up on those. Can anyone recommend a book that is affordable and easy to use? I'm making an attempt here to learn this on my own in my free time. At school, I'm working with Java, so I have some knowledge of computer science theory, but I want to know both languages well enough to maybe get into the industry one day.

Also, thank you to the moderator who gave this thread a better title. Sorry for the lame one earlier; I couldn't come up with anything at the time of posting.

Moose
User is offlineProfile CardPM
+Quote Post

Xing
RE: Prevent Command Window Closure
29 Oct, 2006 - 07:44 PM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
You can start with Thinking In C++, Vol-1 by Bruce Eckel.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Prevent Command Window Closure
29 Oct, 2006 - 11:13 PM
Post #9

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Xing @ 29 Oct, 2006 - 08:44 PM) *

You can start with Thinking In C++, Vol-1 by Bruce Eckel.

It comes highly recommended~~
User is offlineProfile CardPM
+Quote Post

horace
RE: Prevent Command Window Closure
30 Oct, 2006 - 12:18 AM
Post #10

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
there are plenty of good online tutorials

e.g. on C+
http://www.cplusplus.com/doc/tutorial/

and on the STL
http://pages.cpsc.ucalgary.ca/~kremer/STL/...x768/index.html
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month