I'm using the Window Console to create a excerice out of a book for beginers. First it's coded to ask for a number, then after you press the enter key it tells you the results, in about 2 seconds it just closes the Window Console. How can I prevent the Window Console from closeing? I'll give you the code below.

#include <iostream>
using namespace std;

double Cube (double Value);

main () 
{
    double Number, CubeNumber;

    cout << "Enter a number: ";
    cin >> Number;

    CubeNumber = Cube (Number);

    cout << CubeNumber << endl;

    return 0;
}

double Cube (double Value)
{
    double CubeReturn;

    CubeReturn = Value * Value * Value;

    return CubeReturn;
}

Easy enough :D,

Include conio.h and put a getch() before return 0; in main().getch() waits for the user to press a key and it returns that value.

So

char ch;
 
  ch = getch();

 cout<<"User Pressed :"<<ch<<"  Int value:"<<(int)ch;

So if the user pressed enter the int value is 13, if it is 'A' the int value is 64 i.e the ascii values of the key pressed.

Helps?

you could use a while loop or if your using m$ use cmd to open the exe

but any ways the while loop for ya

#include <iostream>
using namespace std;

double Cube (double Value);

main ()
{
char quit; //declare a char variable char
while (quit != 'q') //the while loop
{
double Number, CubeNumber;

cout << "Enter a number: ";
cin >> Number;

CubeNumber = Cube (Number);

cout << CubeNumber << endl;
cout << "\nPress q to quit\n";
cin>>quit; //when q is entered the program will exit
}
return 0;
}

double Cube (double Value)
{
double CubeReturn;

CubeReturn = Value * Value * Value;

return CubeReturn;
}

Think this is what your after :) but i would recomend using the cmd method intill you get used to loops and the way they work.

Well, you could just add this to the last line :
system("Pause") ;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.