Be a Supporter!

Very simple c++ problem

  • 593 Views
  • 13 Replies
New Topic Respond to this Topic
TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Very simple c++ problem 2005-03-05 18:51:04 Reply

I just start visual c++ today, and I'm having a problem. Whenever my console app finishes all its code, it closes. Is there a way to stop that?


abcdefghijklmnopqrstuvwxyz

pieoncar
pieoncar
  • Member since: Apr. 21, 2004
  • Offline.
Forum Stats
Member
Level 53
Blank Slate
Response to Very simple c++ problem 2005-03-05 18:56:57 Reply

You could try getting a string from the user at the end, so that it wouldn't close until there has been input.

TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Very simple c++ problem 2005-03-05 19:36:03 Reply

I tried that but for some reason it works in some of my applications, and doesn't in others:(


abcdefghijklmnopqrstuvwxyz

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Very simple c++ problem 2005-03-05 19:53:45 Reply

sytem("PAUSE")

or cin.get();

att the end of your code will do it.

Peaceblossom
Peaceblossom
  • Member since: Dec. 23, 2003
  • Offline.
Forum Stats
Member
Level 25
Reader
Response to Very simple c++ problem 2005-03-05 20:03:04 Reply

cin.ignore(); will work as well but i think thats not the way to go about it.
even just a cin.; will work i think


BBS Signature
TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Very simple c++ problem 2005-03-05 20:07:15 Reply

Both are generating errors? I think I'm doing something wrong, heres my code:

#include "stdafx.h"
#include <iostream.h>
int main(int argc, char* argv[])
{
int name;
cout << "Yo Jenni, enter your last name, then press enter.\n";
cin >> name;
for (int x = 0; x < 1000; x++) {
cout << "Jenni " << name << " is " << x << " times cooler than me\n" ;
}
system("PAUSE");
}


abcdefghijklmnopqrstuvwxyz

TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Very simple c++ problem 2005-03-05 20:09:12 Reply

Sorry for the double post, but when I use eh-productions method, the window still closes!


abcdefghijklmnopqrstuvwxyz

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Very simple c++ problem 2005-03-06 22:06:04 Reply

You forgot the "return 0;" at the end of your code.

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Very simple c++ problem 2005-03-06 22:10:55 Reply

#include <iostream>

int main() {
//Put all your code here.
cin.get();
return 0;
}

And Love-daddy, don't use system(); calls, ever.

It's quite irritating to me when people use system calls when they're obviously not doing Winsock or Win32 API programming. Use cin.get(); for that stuff, it's annoying as hell when people use system(); and their program would be absolutely portable otherwise.


omg.
Playstation Network tag: muffin-noodle
the empty set

pieoncar
pieoncar
  • Member since: Apr. 21, 2004
  • Offline.
Forum Stats
Member
Level 53
Blank Slate
Response to Very simple c++ problem 2005-03-06 22:20:29 Reply

At 3/6/05 10:10 PM, RegExp wrote: And Love-daddy, don't use system(); calls, ever.

It's quite irritating to me when people use system calls when they're obviously not doing Winsock or Win32 API programming. Use cin.get(); for that stuff, it's annoying as hell when people use system(); and their program would be absolutely portable otherwise.

Just for the sake of argument, how do you feel about something like

void wait_at_end_of_prog() {
#if defined(WIN32)
system("pause");
#elif defined(linux)
cin.get();
#endif
}

Terrible example, but it gets the idea across.

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Very simple c++ problem 2005-03-06 22:43:42 Reply

At 3/6/05 10:20 PM, pieoncar wrote:
At 3/6/05 10:10 PM, RegExp wrote: And Love-daddy, don't use system(); calls, ever.

It's quite irritating to me when people use system calls when they're obviously not doing Winsock or Win32 API programming. Use cin.get(); for that stuff, it's annoying as hell when people use system(); and their program would be absolutely portable otherwise.
Just for the sake of argument, how do you feel about something like

void wait_at_end_of_prog() {
#if defined(WIN32)
system("pause");
#elif defined(linux)
cin.get();
#endif
}

Terrible example, but it gets the idea across.

I've already seen header files like that, in which a single function does such.

Unfortunately people choose to ignore that work and decide to piss off other people anyway.


omg.
Playstation Network tag: muffin-noodle
the empty set

LordAba
LordAba
  • Member since: Nov. 2, 2000
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Very simple c++ problem 2005-03-07 11:26:29 Reply

Or how about going into the compilers options and setting the option to "not close after console application finishes".

Can't remember the exact wording, but it's there.


What may man within him hide, though angel on the outward side.

BBS Signature
TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to Very simple c++ problem 2005-03-07 15:55:27 Reply

gotcha, thanks


abcdefghijklmnopqrstuvwxyz

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to Very simple c++ problem 2005-03-07 19:09:53 Reply

At 3/6/05 10:10 PM, RegExp wrote: And Love-daddy, don't use system(); calls, ever.

It's quite irritating to me when people use system calls when they're obviously not doing Winsock or Win32 API programming. Use cin.get(); for that stuff, it's annoying as hell when people use system(); and their program would be absolutely portable otherwise.

k thanks i'll keep that in mind dude. lol remember that im still "green" in terms of C++.