0

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

I have the following console program:

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;


cout<<"Enter a";
cin>>a;

cout<<"Enter b";
cin>>b;

int result = a*b;

cout<<"You entered"<<a<<"and you entered"<<b<<"Their product is"<<result<<endl;
    return 0;
}

Once i run the program,it accepts the input but exits before i can have a look at the results.What do i need to do for the program not to exit before i can take a look at the results?.

Community
  • 1
  • 1
Gandalf
  • 1
  • 29
  • 94
  • 165

6 Answers6

2

BTW, you've already calculated the value of result before you've gotten your input for a and b, so the value of result will either be 0 if your compiler assembles code that zero-initializes any variables declared on the stack, or will just be some random value. In fact, you really don't need to even declare result ... you can calculate it's value in the cout statement. So you can adjust your last line so it looks like this:

cout << "You entered" << a <<"and you entered"<< b 
     << "Their product is" << (a*b) << endl;

To stop the program from exiting, you can grab another char from stdin. So you can do the following:

cout << "Press any key to exit..." << endl;
char extra;
cin >> extra;
Jason
  • 31,834
  • 7
  • 59
  • 78
1

Use getche() ,getch() or any character based input function.

int main()
{
    int a;
    int b;
    int result = a*b;

cout<<"Enter a";
cin>>a;

cout<<"Enter b";
cin>>b;

cout<<"You entered"<<a<<"and you entered"<<b<<"Their product is"<<result<<endl;
getch(); //use this.It would wait for  a character to input.
return 0;
}

And generally we use Enter to exit the program whose ASCII value is fetched by it .But since it is of no use to us not storing it in a variable.

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
1

What about adding system ("pause"); before return 0; statement?

patapizza
  • 2,398
  • 15
  • 14
1

You could ask for more feedback

cout<<"You entered"<<a<<"and you entered"<<b<<"Their product is"<<result<<endl;

char stop;
cin >> stop;
Vinnyq12
  • 1,519
  • 9
  • 9
0

Windows:

//1
    system ("pause");
//2
    #include<conio.h>
    _getch();

.NET (Windows):

System::Console::ReadLine();

Overall:

    #include <cstdlib.h>
    return EXIT_SUCCESS;
Secko
  • 7,664
  • 5
  • 31
  • 37
  • I've no idea what `` is (or indeed `_getch`), but I'm guessing that also only works on Windows. In Standard C++, you want `getchar()` from `` or `cin.get()` from ``. – Mike Seymour Jun 03 '11 at 12:22
  • @Secko: Not in my version it doesn't. The standard functions will work on any conforming compiler. – Mike Seymour Jun 03 '11 at 12:32
  • Hmm, it appears that it is so. However, it works on my machine with gcc. – Secko Jun 03 '11 at 12:35
  • Also, in C++ you should get `EXIT_SUCCESS` from ``, not the deprecated C header; and you don't need a return statement at the end of `main` at all. – Mike Seymour Jun 03 '11 at 12:37
  • @Mike: Right, I got used to writing C so there you go, and the return was just a demonstration for him to know where it goes. – Secko Jun 03 '11 at 12:40
0

I like to use getch() from conio.h when I'm on Windows, but that's not quite portable :/

King_DuckZ
  • 238
  • 1
  • 6