my program runs but closes before it finshes

i used cin.get(); <- but that does not keep my console window open

Page 1 of 1

14 Replies - 2374 Views - Last Post: 31 May 2009 - 03:24 PM Rate Topic: -----

#1 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 20-May 09

my program runs but closes before it finshes

Posted 31 May 2009 - 01:33 PM

I think im doing something stupid here but i don't know where. im new to this and been doing tutorials from c++ how to program 5th edition book.

As you can see my program is meant to add two numbers togther. upon entering the second number and pressing enter the program closes and does not show the answer.

any ideas on what i have done wrong?

#include <stdafx.h>

#include <iostream>

using namespace std;
int main()
{
	int number1;
	int number2;
	int sum;

	cout <<"Enter first interger:";
	cin >>number1;
	
	cout << "Enter second interger:";
	cin >> number2;

	sum = number1 + number2;
	
	cout << "sum is" << sum << endl;
	
	cin.get();

	return 0;

}
 


Is This A Good Question/Topic? 0
  • +

Replies To: my program runs but closes before it finshes

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4317
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:35 PM

Please check out our thread titled "Holding the Execution Window Open". It is a whole thread dedicated to this common question and talks about many ways of allowing the window to stay open so you can view the results.

Holding the Execution Window Open

Let us know if you have any further questions. :)
Was This Post Helpful? 1
  • +
  • -

#3 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 20-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:38 PM

Thanks, swift reply too :) I'll get reading right away.

I read the link you sent me and the guy says to use "cin.get();" to keep the window open. but i already tried it and it don't work. it worked when i did the hello world tutorial, but with my program its not working.

This post has been edited by lrk89: 31 May 2009 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 Cha0sBG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:43 PM

last line:
 System("PAUSE"); 

Was This Post Helpful? 0
  • +
  • -

#5 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 20-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:47 PM

I get an error when i try to compile with " System("PAUSE");" is there a specific header file i need to include before this function works?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4317
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:48 PM

View Postlrk89, on 31 May, 2009 - 12:38 PM, said:

Thanks, swift reply too :) I'll get reading right away.

I read the link you sent me and the guy says to use "cin.get();" to keep the window open. but i already tried it and it don't work. it worked when i did the hello world tutorial, but with my program its not working.



I think you didn't take the time to read the thread. The user is saying that using cin.get by itself may not be enough, especially when you are prompting the user for input since a new line character may still be in the stream. (which you are doing)

They recommend CLEARING the stream before using cin.get(). And since Cha0sBG also didn't read the thread, they would know that it is recommended against System("pause") since that is platform specific. ;)
Was This Post Helpful? 0
  • +
  • -

#7 JayFCox  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 41
  • Joined: 31-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:48 PM

View Postlrk89, on 31 May, 2009 - 12:38 PM, said:

Thanks, swift reply too :) I'll get reading right away.

I read the link you sent me and the guy says to use "cin.get();" to keep the window open. but i already tried it and it don't work. it worked when i did the hello world tutorial, but with my program its not working.


No, it says
//Clean the stream and ask for input
  std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  std::cin.get();



Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4317
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:50 PM

Someone is paying attention. :^:
Was This Post Helpful? 0
  • +
  • -

#9 JayFCox  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 41
  • Joined: 31-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:52 PM

View PostCha0sBG, on 31 May, 2009 - 12:43 PM, said:

last line:
 System("PAUSE"); 


Any solution that requires execution of a shell command to complete is beyond rediculous.
Was This Post Helpful? 0
  • +
  • -

#10 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 20-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 01:54 PM

@JayFCox
I get an error when i tried that.

"1>visual studio 2008\: error C2065: 'numeric_limits' : undeclared identifier

1>visual studio 2008\ error C2275: 'std::streamsize' : illegal use of this type as an expression

1> e:\program files\microsoft visual studio 9.0\vc\include\iosfwd(25) : see declaration of 'std::streamsize'

1>visual studio 2008\: error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided

1> e:\program files\microsoft visual studio 9.0\vc\include\xutility(3364) : see declaration of 'std::max'

1>visual studio 2008\: error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided

1> e:\program files\microsoft visual studio 9.0\vc\include\xutility(3356) : see declaration of 'std::max'

1>C++ tutorial - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
"

This post has been edited by lrk89: 31 May 2009 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#11 larry_lalonde  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 24-March 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 02:09 PM

just do
// lots of code
cin.ignore();
cin.get();

return 0;
}


Was This Post Helpful? 1
  • +
  • -

#12 JayFCox  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 41
  • Joined: 31-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 02:14 PM

View Postlarry_lalonde, on 31 May, 2009 - 01:09 PM, said:

just do
// lots of code
cin.ignore();
cin.get();

return 0;
}



yeah, what he said :D.
The other code works if you
#include <limits>


Was This Post Helpful? 0
  • +
  • -

#13 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 20-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 02:14 PM

Thanks larry that works perfectly. :D
Was This Post Helpful? 0
  • +
  • -

#14 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 02:49 PM

#include <conio.h>

_getch()




works also :P

Kevin

This post has been edited by deery5000: 31 May 2009 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#15 Cha0sBG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Re: my program runs but closes before it finshes

Posted 31 May 2009 - 03:24 PM

View Postlrk89, on 31 May, 2009 - 11:47 PM, said:

I get an error when i try to compile with " System("PAUSE");" is there a specific header file i need to include before this function works?



 #include <iostream>
#include <windows.h>



try it now :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1