How to get window to stay open after C++ code runs? [duplicate]
Asked Answered
A

4

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;
}
Agalloch answered 28/1, 2012 at 6:24 Comment(10)
Welcome to Stack Overflow! Here, to format code properly you indent each line by four spaces, or click on the {} button.Marxist
11 + 12 != 30/2 ;) Set a break point at return statement.Limp
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.Agalloch
I suggest replacing dev c++, whit code blocks. codeblocks.org I had great experience.Seagraves
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.Agalloch
@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.Puppis
I am using Dev C++ because I can not get Visual Studio to work.Agalloch
@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" link for such gems as "So do yourself and everyone else a favor: don't use Dev-C++."Puppis
The Dev-C++ project has been revived, though by an entirely different party, and not many people seem to know about it: orwellengine.blogspot.comGwendolyngweneth
@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.Marxist
K
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.

Kashmiri answered 28/1, 2012 at 6:29 Comment(2)
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.Agalloch
@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.Kashmiri
K
3

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

Kylie answered 28/1, 2012 at 6:26 Comment(4)
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.Agalloch
@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.Marxist
getchar() doesn't work, compiler gives me an error. Do I place it after my cout, after { or after return0;? all are errors.Agalloch
also you must add #include <stdio.h> at the topKylie
E
2

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

Eldon answered 28/1, 2012 at 9:32 Comment(0)
D
2
#include <iostream>
using namespace std ;



int main(void)
{


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

   return 0;
}
Dilatometer answered 28/1, 2012 at 16:9 Comment(2)
I thought you didn't need the std:: if you used namespace std?Agalloch
That's correct: it's optional therefore in this case.Hegyera

© 2022 - 2024 — McMap. All rights reserved.