7

Possible Duplicate:
How to stop C++ console application from exiting immediately?

I am trying to see my results, what do I do to my code so I can see if what I did is correct?

#include <iostream>
using namespace std;

int main()
{
    cout << "C++" << endl;
    cout << "The sum of 11 + 12 = " << 30/2 << endl;
    return 0;
}
Community
  • 1
  • 1
  • Welcome to Stack Overflow! Here, to format code properly you indent each line by four spaces, or click on the `{}` button. – In silico Jan 28 '12 at 06:26
  • 11 + 12 != 30/2 ;) Set a break point at return statement. – Mahesh Jan 28 '12 at 06:27
  • 1
    I know it doesn't work, I was just trying to see if it printed whatever I put and where the calculations were being done. I am using Dev C++ on windows 7. –  Jan 28 '12 at 06:30
  • I suggest replacing dev c++, whit code blocks. http://www.codeblocks.org/ I had great experience. – Luka Rahne Jan 28 '12 at 06:35
  • 1
    I am trying to get Visual C++ to work and it is destroying my life currently. I do not want to mess with another compiler. –  Jan 28 '12 at 06:38
  • @Jordan That means you are using Visual Studio? If so, add it to the tags (it will affect answers). In any case, add the appropriate compiler/IDE to the tags. –  Jan 28 '12 at 06:42
  • 1
    I am using Dev C++ because I can not get Visual Studio to work. –  Jan 28 '12 at 06:44
  • @Jordan You may be amused to read the tag description for "dev-c++", hover over it for a few seconds ;-) Then click on the ["info"](http://stackoverflow.com/tags/dev-c%2b%2b/info) link for such gems as "So do yourself and everyone else a favor: don't use Dev-C++." –  Jan 28 '12 at 06:45
  • The Dev-C++ project has been revived, though by an entirely different party, and not many people seem to know about it: http://orwellengine.blogspot.com/ – Benjamin Lindley Jan 28 '12 at 06:50
  • @Jordan: Visual Studio is one of the best IDEs you can use to develop for Windows. If you can't get Visual Studio to work, you can try asking about that. – In silico Jan 28 '12 at 06:51

4 Answers4

4

I think what you mean is that your DOS terminal closes as soon as your program ends.

A common solution is to have a call to cin, scanf or getch at the end of your program, just before your return 0. This forces the program to wait for some user input before exiting.

A better way is to compile your program and then run it from within a DOS prompt yourself. Just start up a DOS prompt, cd to the directory your program is in and run it from there.

MAK
  • 26,140
  • 11
  • 55
  • 86
  • 1
    I can't get cin to work. cin >>"a">> endl; I have been using the highly reccomended visual express and I don't really like it. Dev was much better, it would tell me what the errors were and where, this one does not. –  Feb 01 '12 at 01:13
  • 1
    @Jordan: Why would you use `cin>>"a"`? You use `cin` to read into a variable not a literal string. Try `char a; cin>>a;`. Simply changing the direction of the arrows from a `cout` example is not the way to learn. – MAK Feb 01 '12 at 09:39
3

Use getchar() at the end of code or just run your executable file from console.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • 1
    I don't know how to run it from the console because I do not know the file extension or how to figure out the file extension. It still instantly closes the window. –  Jan 28 '12 at 06:30
  • @Jordan: On Windows, executables typically have an `.exe` file extension, although not all valid Windows programs have `.exe` extensions. However, you do not need to type out the `.exe` to run an executable in the command line. The name of the application is enough. – In silico Jan 28 '12 at 06:31
  • 1
    getchar() doesn't work, compiler gives me an error. Do I place it after my cout, after { or after return0;? all are errors. –  Feb 01 '12 at 01:11
  • also you must add `#include ` at the top – IProblemFactory Feb 01 '12 at 06:07
2

An other way on windows: system("pause");

secmask
  • 7,649
  • 5
  • 35
  • 52
2
#include <iostream>
using namespace std ;



int main(void)
{


   std::cout<<" \nPress any key to continue\n";
   std::cin.ignore();

   return 0;
}