Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,674 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,205 people online right now. Registration is fast and FREE... Join Now!




premature exit

 
Reply to this topicStart new topic

premature exit, keeping program running until user hits return

Zatarra
post 17 Jul, 2007 - 10:15 PM
Post #1


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 2


My Contributions


Hey,

I've only being learing C++ for a few days. This is my first, extremely simple program, which is meant to ask the user to input a few different values, perform a few simple operations with those values, and then display the results.
The problem is as soon as the last value (no. of shares) is entered, the program exits instantly. How can I keep it open, (making it wait for me to hit return) until I've looked at the results?
P.S. I've tried inserting cin.get(); at the end but this has no effect.



CODE

#include <iostream>
using namespace std;

int main ()
{
      float pprice, sprice, slprice, loss, profit, ratio;  //declaring variables
      int amount;
      
      cout << "Enter purchase price:";                           //input by user
      cin >> pprice;
      cout << "Enter sale price:";
      cin >> sprice;
      cout << "Enter stop-loss price:";
      cin >> slprice;
      cout << "Enter no. of shares:";
      cin >> amount;
      
      profit = amount * (sprice - pprice)- 43.8;                 // calculations
      loss = amount * (pprice - slprice) - 58.8;
      ratio = profit / loss;
        
      cout << "Profit: " << profit << "\n";            //Results of calculations
      cout << "Loss: " << loss << "\n";
    
}


This post has been edited by Zatarra: 17 Jul, 2007 - 10:16 PM
User is offlineProfile CardPM

Go to the top of the page

ljfox4
post 17 Jul, 2007 - 10:41 PM
Post #2


New D.I.C Head

*
Joined: 10 Feb, 2007
Posts: 30


My Contributions


I'm surpised that cin.get() didn't work. Can I assume you are running windows? If so, try the following;

CODE

#include <stdlib>
...
...
...
system("PAUSE");


Of course the first line goes with your includes and the last line wherever you want the pause.
User is offlineProfile CardPM

Go to the top of the page

Zatarra
post 18 Jul, 2007 - 12:44 AM
Post #3


New D.I.C Head

*
Joined: 17 Jul, 2007
Posts: 2


My Contributions


Thanks, that worked.
I don't suppose someone could explain the difference between system("PAUSE"); and cin.get(); , and why the latter didn't work in my case??

This post has been edited by Zatarra: 18 Jul, 2007 - 12:48 AM
User is offlineProfile CardPM

Go to the top of the page

enpey
post 18 Jul, 2007 - 01:43 AM
Post #4


D.I.C Head

**
Joined: 2 May, 2007
Posts: 59



Thanked 1 times
My Contributions


I recall I was having a similar problem a month or two ago similar to this (in C). It was even reading in and then displaying the character at the end of the program
(ie
CODE

printf ("Hit any key to continue...");
scanf (anything);
printf ("%c", anything);

)

It was running through without my or anyone elses pressing of a button and putting a full stop if I recall correctly. I had no idea, didn't have the time to learn what was going on and changed my approach.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Jul, 2007 - 04:46 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


system("pause") is a function call using the system function to call an executable file on the local machine. It is platform dependent, and not compliant to ANSI standards. I would advise against it's use.

cin.get() takes one character from the stream. this is compliant to standards, but did not work in your case because there was already one or more characters (likely a newline character) in the stream, so that character was taken, and the application proceeds.

this can be a common problem while learning C++, and here is a great solution:

http://www.dreamincode.net/code/snippet582.htm

You may also wish to read through the following thread, where the discussion has occurred in the past.

http://www.dreamincode.net/forums/showtopic30088.htm
User is offlineProfile CardPM

Go to the top of the page

muso
post 18 Jul, 2007 - 04:56 AM
Post #6


D.I.C Head

**
Joined: 6 Jul, 2007
Posts: 51



Thanked 2 times
My Contributions


QUOTE(Amadeus @ 18 Jul, 2007 - 05:46 AM) *

system("pause") is a function call using the system function to call an executable file on the local machine. It is platform dependent, and not compliant to ANSI standards. I would advise against it's use.

cin.get() takes one character from the stream. this is compliant to standards, but did not work in your case because there was already one or more characters (likely a newline character) in the stream, so that character was taken, and the application proceeds.

this can be a common problem while learning C++, and here is a great solution


I was just looking into this, and thinking on similar lines. It seems a poor alternative to have to resort to something platform specific for such a fundamental purpose.

I saw another, simpler option, was to run the program at a command prompt (i.e. Start > Run > "cmd"), and then the pause may not be required.

Also looking at the sample:

CODE
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );


Would there be a problem with using something like:

CODE
std::cin.ignore ( 1000, '\n' );


instead? As this is easier to remember...

This post has been edited by muso: 18 Jul, 2007 - 04:57 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Jul, 2007 - 05:12 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


you are correct, running the program at a command prompt will not require a pause, as the command window will remain open.

your modification would absolutely work...providing there were no more than 1000 characters in the stream (which is a pretty safe bet).
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 18 Jul, 2007 - 11:49 PM
Post #8


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


getch() from conio.h will work well on Windows.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:23AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month