Topic: "C++ Problem (program exits after enter)" (page 1 of 1)

1
Author Post
Dhaos
groupmaster
Hi guys hopefully you can help me THE NOOB, heheh thx, the problem is when i compile it in visual c++ it works great but when i take the application out of thedebug folder and put it onto the floppy and run the application it works but when i type a number an hit enter it exits, anyways here the code

// Circle.cpp
// Circumference of a circle
// Programmer: David R. Quigley
// 3/28/2006

#include <iostream.h>

main()
{
const float PI = 3.14159f; // Declaring PI as a constant floating number
float circumference, radius;

// Asks user for radius
cout << "What is the radius of the circle? ";
cin >> radius;

circumference = 2 * PI * radius; // Formula for circumference of a perfect circle.

// Output The circumference of the circle
cout << "The circle's circumference is ";
cout << circumference << '\n';
return 0;
}
private message EMail
quangntenemy
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
There's no problem with the code. It's just that the program prints the output and then terminates right away. If you want to see the output of the program try running it in command line or put a getchar() before "return 0"
Edit: You also need to include stdio.h to use getchar()
Edited by quangntenemy on 28.03.2006 23:19:08
private message EMail Website
unknown user
or a bit more c++ like

#inlcude <iostream> // no .h
using namespace std;


.
.
.



char a;
cin>>a;
return 0;
}
EMail
Blacklotis
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
how about

system("pause");

its what ive used in the past
private message EMail
BaRa
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Hi Dhaos,

Just put this as the last line:
cin.get();

cin.get() waits for the user to enter a character before it continues.

Greetz BaRa
Edited by BaRa on 01.04.2006 21:24:11
private message
mxn
groupmastergroupmastergroupmastergroupmaster
QuoteQuote:
system("pause");

that not only makes the program system dependant, its also ugly++!

Better use one of the other solutions suggested here.
private message Website
cyph1e
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
QuoteQuote from Blacklotis:
system("pause");

It's a bad way, read here why: linkhttp://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385. system("pause") also spawns another process.
private message Website
beerhunter
groupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Note that, if you've just read a value using 'cin >> foo', there'll still be a newline in the input buffer, and cin.get() will return immediately. Add a call to cin.ignore():

float radius;
cin >> radius;
...
cin.ignore(1000, '\n');
cin.get();
private message EMail

Topic: "C++ Problem (program exits after enter)" (page 1 of 1)

1