Very beginner error. Console closing to soon.

After I press enter after it says enter second number and I do it automatically closes even though I have system("pause"); and im running windows vista 32-bit.


#include <conio.h>
#include <Windows.h>
#include <string>
#include <iostream>

using namespace std;

int main(){
int firstnum, secondnum, answer;
cout << "Hey" << " Dudes" << " Whatsup?" << endl;
cout << "Enter first number: ";
cin >> firstnum;
system("cls");
cout << "Thanks dude." << endl;
cout << "Now enter second number: ";
cin >> secondnum;
system("cls");
answer = firstnum + secondnum;
cout << "The addition of those numbers is: " << answer;
cout << endl << endl << endl;
system("pause");
}
closed account (zb0S216C)
#include <Windows.h>

You don't need this.

#include <conio.h>

You don't need this either.

using namespace std;

This is namespace pollution. Use this instead: using std::string; using std::cout; using std::cin; using std::endl;

system("pause");

Don't use this. Use getch( ); instead. http://www.cplusplus.com/forum/general/4237/

As for you problem, there shouldn't even be a problem.

P.S: Make sure you return an integer from the main entry-point.
Last edited on
Keep the console open long enough to see your program's output
http://www.cplusplus.com/forum/articles/7312/

Framework wrote:
using namespace std;
This is namespace pollution. Use this instead: using std::string; using std::cout; using std::cin; using std::endl;


Er, no it is not. There is nothing wrong with doing this in your program's code. (You should be aware of the implications, however, but you'll get to that later. However, never do this in an include file.)

Namespace pollution occurs when you booger the names in one namespace with those from another namespace. That isn't happening here.
using namespace std;

This is namespace pollution. Use this instead: using std::string; using std::cout; using std::cin; using std::endl;


Sorry ?? What the hell are ya saying ??? Man if you read any C++ books I dont think you have seen any message like
DONT USE using namespace std;
!! Even programmers recommand it for people to use it as you save A LOT OF TIME THAN writting every time std::blah blah !
What you have just said in useless. We dont need anymore the std::cin ... std::cout .... !!!
On the other hand, you told him use getch() ???? Oh My GOD !! If you want to grow as a programmer is the worst thing you can do. In all C++ contents and competitions in my country, using getch() or clrscr(); or system("pause"); or any other commands like this are BANNED! If you use them you lose all your points, so please stop ! What you said in beginning was ok ! He didnt need to use windows.h or conio.h. But this is all. Everything else is useless for a programmer.

snowboarder7, if you want console open until you press a key, (i dont know why u have to use a command. i dont use any command) just use this structure:
1
2
3
4
5
6
7
8
9
10
#include <iostream>  // or <fstream> if you want to print to file
using namespace std;
int main()
{
      ...
       //your code here
      ...
      
       return 0; // This is only thing i use (p.s.: i am using MinGW Developer Studio and wxDev C++,   and rarely i work in C# or Visual C++)
}


now ill give u an eg. with fstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
using namespace std;
int main()
{
   ... //variables ...
   
   ifstream in("text.in");
   ofstream out("text.out");
   
   ... // now you will use the in, out instead of cin, cout ( you can put anything instead of in, out , for eg. : f("text.in");   g("text.out");

   // when i tell you this i suppose you have already read and learned about file printing as im not going to write here a tutorial
 
   return 0;
}

Last edited on
closed account (zb0S216C)
Even programmers recommand it for people to use it as you save A LOT OF TIME THAN writting every time std::blah blah !

Instead of including everything in the standard namespace, it's better to include only the things you need.

What you have just said in useless. We dont need anymore the std::cin ... std::cout .... !!!

He uses string and endl. Both of them are defined within the standard namespace.

In all C++ contents and competitions in my country

I'm not Romanian neither is Snowboarder.

using getch() or clrscr(); or system("pause"); or any other commands like this are BANNED!

They're banned in a competition, not in the real-world. In the real-world, there isn't any restriction on what you can and can't use.

#include <iostream> // or <fstream> if you want to print to file

If he doesn't include the <iostream> library, how will he gain access to cout, cin, and endl?

Instead of looking like w****r by putting exclamation marks everywhere, be mature and explain incorrect replies politely. You'll get more respect that way. Personally, your post gave me a really bad first impression( never seen you post before ).
Last edited on
On topic, you can use "cin.get()" just before return 0. Then you need to hit enter to terminate the program.
Last edited on
Framework wrote:
#include <conio.h>

You don't need this either


Well technically he does if you're going to suggest he use getch(). (At least, I find it a bit redundant to tell him he doesn't need it only to proceed telling him to use a function where he does. xD)
Last edited on
closed account (zb0S216C)
Well technically he does if you're going to suggest he use getch()

Lol! Thanks for pointing that out :)P Yeah, he does need that header if he's planning on using getch( ). If he's planning on using cin.get( ), he doesn't need it.
@jumper007
You're in over your head. Why don't you calm down and learn something?
Topic archived. No new replies allowed.