9 Replies - 2044 Views - Last Post: 30 October 2006 - 01:18 AM Rate Topic: -----

#1 TxMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-October 06

Prevent Command Window Closure

Posted 28 October 2006 - 08:36 PM

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.

//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

Is This A Good Question/Topic? 0
  • +

Replies To: Prevent Command Window Closure

#2 UMTopSpinC7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 20-October 06

Re: Prevent Command Window Closure

Posted 28 October 2006 - 09:23 PM

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

			 << 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 October 2006 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Prevent Command Window Closure

Posted 28 October 2006 - 09:35 PM

1) Change your headers to standard headers
2) To stop the output window check this code snippet Stopping the output window
Was This Post Helpful? 0
  • +
  • -

#4 TxMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-October 06

Re: Prevent Command Window Closure

Posted 28 October 2006 - 09:39 PM

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
Was This Post Helpful? 0
  • +
  • -

#5 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Prevent Command Window Closure

Posted 29 October 2006 - 01:21 AM

View PostTxMoose, on 29 Oct, 2006 - 10:09 AM, said:

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 October 2006 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Prevent Command Window Closure

Posted 29 October 2006 - 06:01 AM

View PostXing, on 29 Oct, 2006 - 01:21 AM, said:

View PostTxMoose, on 29 Oct, 2006 - 10:09 AM, said:

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:

#include <iostream>
#include <cstdlib>

using namespace std;

Was This Post Helpful? 0
  • +
  • -

#7 TxMoose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-October 06

Re: Prevent Command Window Closure

Posted 29 October 2006 - 08:11 PM

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
Was This Post Helpful? 0
  • +
  • -

#8 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Prevent Command Window Closure

Posted 29 October 2006 - 08:44 PM

You can start with Thinking In C++, Vol-1 by Bruce Eckel.
Was This Post Helpful? 0
  • +
  • -

#9 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Prevent Command Window Closure

Posted 30 October 2006 - 12:13 AM

View PostXing, on 29 Oct, 2006 - 08:44 PM, said:

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

It comes highly recommended~~
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Prevent Command Window Closure

Posted 30 October 2006 - 01:18 AM

there are plenty of good online tutorials

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

and on the STL
http://pages.cpsc.uc...x768/index.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1