
May 21st, 2006
10:48 PM
Neverside Newbie
Status: Offline!
c++ help
im new to c++ and want to know something about console applications. i used this code:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
return 0;
}
it prints 'hello world' in the dos prompt, but automatically exits straight after. how do i get it to not exit the dos prompt?
ps. im using visual studio 2005

May 21st, 2006
11:17 PM
Lord British
Status: Offline!
You can ask for input from the user before exiting, or to make it simpler for you as you are learning, put this before "return 0;"
___________________
PurePhotoshop Forums

May 21st, 2006
11:17 PM
Development Forum Leader
Status: Offline!
This is a fairly common question, take a look at this:
http://c-faq.com/osdep/waitforkey.html
___________________
irc.efnet.net #neverside
Neverside merchandise!

May 22nd, 2006
12:35 AM
Neverside Newbie
Status: Offline!
ok my new code is:
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
}
when i hit enter it prints "and double value is" etc. but it instantly exits the program. i removed the return 0; so why does it still do this? i used what you both suggested above but it makes no difference...

May 22nd, 2006
02:03 AM
Neversidian
Status: Offline!
What compiler are you using? system("pause"); should work.
___________________
Free-Speed Nation

May 22nd, 2006
02:11 PM
Neversidian
Status: Offline!
either than or just put a cin >> i; on the very last line before the return so it will hang until it gets an input before it exits.
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
cout << "\nPlease enter any character and hit enter to exit: ";
cin >> i;
return 0;
}
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

May 22nd, 2006
04:23 PM
Neverside Newbie
Status: Offline!
how do i make it exit when i=='end' and RETURN is pressed. i tired
but that didnt work
Last edited by sapphire, May 22nd, 2006 05:22 PM (Edited 1 times)

May 23rd, 2006
09:09 AM
Development Forum Leader
Status: Offline!
Originally posted by sapphire:
how do i make it exit when i=='end' and RETURN is pressed. i tired
but that didnt work
That shouldn't even compile. Use double-quotes ("") for strings and single-quotes ('') for individual characters. I'm not sure how it works in C++, but in C, you need a function to compare strings.
___________________
irc.efnet.net #neverside
Neverside merchandise!

May 23rd, 2006
12:03 PM
Neversidian
Status: Offline!
you can overload operators in c++, But I dont think he was doing that.
You do something like
bool operator==(stuff) then you can compare stuff with a == instead of class.equals(value)
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!