Program Auto-Closing, even with proper measures taken

I'm writing a program that basically encompasses everything from the tutorial. I have run into a problem with cin. I declared a float d, and said cin >> d;
Entering the number was fine, but the program closed when I actually hit the enter button. I changed it to an int to see if it would fix the problem, and it did not. I find this early close strange, because I thought the following took care of it:
1
2
3
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0; }
i think its just that your program runs nicely and exits... but its so fast that you cant even see what happened...
so, you now have solved your problem by adding a "cin" to the end rite?

if you are using Visual Studio, you can do Debug>Start without debugging to run the program.. (Ctrl + F5)

if its GCC on linux, i dont think that you need anything like that because you are running inside the terminal(shell)...
Actually, I'm on Dev-C++ (yes, I know it's discontinued). And I don't follow this line:
you now have solved your problem by adding a "cin" to the end rite?
I'm not sure if it's me being naive or stupid, but I require more help.
i havent used Dev-C++. maybe there is something simillar to what i told about visual studio, just search about it via google..

ok, think like this.
try writing a Hello World program that just have something like this...
1
2
3
4
5
int main()
{
     cout << "Hello World" << endl;
     return 0;
}


when you run this program it will close suddenly.. you will not be able to see what just happened..

if you modify this like
1
2
3
4
5
6
7
8
9
int main()
{
     cout << "Hello World" << endl;
     // added 3 lines are below
     cout << "Type a number and press enter to exit the program";
     int r;
     cin>>r;   // wait until a number is given and enter is pressed
     return 0;
}


the new 3 lines will make the program to wait untill you type a number and press enter to exit..
rite...
so now the program will not close suddenly when you just run it...

but this way is not very good because you will always have to enter a number and press enter to exit..
the lines you used,
1
2
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

will make things nice by letting you just press enter to exit the program..

i hope this is what you wanted to know.. rite?
I already know what my lines do... I need help figuring out how to make it so my program doesn't close as soon as I input my number (A for effort though).
can you please put your code and explain the problem a little bit more... i am very sorry that i dont seem to understand your question, i guess..... please show us the code..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//C++ 
#include <iostream>
#include <string>
#include <sstream>
#include <new>
#define cake delic
#define debt 18.30
using namespace std;
int main ()
{ int a = 5;
int b = 7, third = (b-a);
int d;
string cake = "yummy";
string string1 = "Whoa! Typing shortcut!";
cout << "This is displayed in the window. " << endl;
cout << "A is equel to " << a << ". " << endl;
cout << "B minus A is " << third << ". " << endl;
cout << string1 << endl;
cout << "Cake is " << cake << " as my debt of $" << debt << ". " << endl;
cout << "5+1=" << ++a << endl;
cout << "Please enter a numeric value. ";
cin >> d;
cout << endl << "You entered " << d << ". "; 
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0; }
/* see http://www.cplusplus.com/doc/tutorial/operators/ for operators */

But when I run, and it asks me what to put for d, it auto-closes before
You entered
appears.
you have to #include <limits> at the top or this code should not compile.. dd it compile without it?

I am not very good with c++.
i found that the "istream" object "cin" still contains the value '\n' from the last call to "cin".. in your program. you can test this by adding
1
2
char lastChar = cin.get();
cout<<endl<< "last=|"<<lastChar<<"|"<<endl;

between the lines 24 and 25..
so, thats why the cin.ignore() function exits suddenly...
your program works fine...

to overcome the problem, just waste this last character by adding something like
char lastChar = cin.get();
between 24 - 25 lines..
or there maybe a more legit way to destroy the values within "cin" which i dont know.

ps.
i recommend using visual studio (or Anjuta if u r in linux).... you can get VS for free, non-commercial Express version.. for students..
its very good for debugging..

cheers....
closed account (3pj6b7Xj)
Funny, when I do console type programming, Code::Blocks creates a debug version and automatically throws in the press any key to continue so I can see the program output.......switch to code::blocks, youll be a happy man if you do.
I'll try that code adjustment, thanks.
And I have Code::Blocks also installed, but I've grown used to Dev-C++ so I rejected it after just a few minutes of messing with it (I want to switch, but the coding gods from above don't permit it!).
The method I always use is

std::cin.get();

right before your return statement.
Topic archived. No new replies allowed.