943,472 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3633
  • C++ RSS
Jul 25th, 2004
0

Window Console Help

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
.·)Tusky(·. is offline Offline
8 posts
since Jul 2004
Jul 26th, 2004
0

Re: Window Console Help

Easy enough ,

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
C++ Syntax (Toggle Plain Text)
  1.  
  2. char ch;
  3.  
  4. ch = getch();
  5.  
  6. 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?
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jul 26th, 2004
0

Re: Window Console Help

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.
Reputation Points: 10
Solved Threads: 10
Junior Poster
omol is offline Offline
156 posts
since Jul 2004
Jul 27th, 2004
0

Re: Window Console Help

Well, you could just add this to the last line :
system("Pause") ;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diablo is offline Offline
3 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Text File Input and manipulation
Next Thread in C++ Forum Timeline: scanf - compiles but causes error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC