954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Window Console Help

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
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;
}

.·)Tusky(·.
Newbie Poster
8 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

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?

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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
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.

omol
Junior Poster
156 posts since Jul 2004
Reputation Points: 10
Solved Threads: 10
 

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

diablo
Newbie Poster
3 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You