Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

57 users online



c++ help

c++ help

Currently viewing this thread: 1 (0 members and 1 guests)


Page 1 out of 2
sapphire

sapphire

Neverside Newbie
Status: Offline!

c++ help

im new to c++ and want to know something about console applications. i used this code:

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

Kryptonite

Kryptonite

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;"

Code:

system("PAUSE");

___________________

PurePhotoshop Forums

James

James

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!

sapphire

sapphire

Neverside Newbie
Status: Offline!

thanks guys%20:D%20

sapphire

sapphire

Neverside Newbie
Status: Offline!

ok my new code is:

Code:

#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...

Sykil

Sykil

Neversidian
Status: Offline!

What compiler are you using? system("pause"); should work.

___________________

http://imagegen.last.fm/artist/artists/Sykil.gif
Free-Speed Nation

BigToach

BigToach

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.

Code:

#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!

sapphire

sapphire

Neverside Newbie
Status: Offline!

how do i make it exit when i=='end' and RETURN is pressed. i tired

Code:

f(i=='end'){
     return 0;
}

but that didnt work

Last edited by sapphire, May 22nd, 2006 05:22 PM (Edited 1 times)

James

James

Development Forum Leader
Status: Offline!
Originally posted by sapphire:

how do i make it exit when i=='end' and RETURN is pressed. i tired

Code:

f(i=='end'){
     return 0;
}

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!

BigToach

BigToach

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!

Page 1 out of 2
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0133 seconds.