Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Mechanix2Go
The code:
:: 7.cpp
#include <stdio.h>
#include <math.h>
#include <iostream.h>float mynum = 1;
main()
{
while (mynum != NULL)
{
cout << "enter number: ";
cin >> mynum;
cout << mynum << "\n";
};
}
:: end codeIt compiles and runs; accepts input, but when I press enter without inputting a number it just drops down a line and I have to break out. What am I missing here?
TIA
output:
C:\temp\->7
enter number: 22
22
enter number: 3.2
3.2
enter number:
^CC:\temp\->
If at first you don't succeed, you're about average.M2

enter is not null. so your loop never ends.
Typically the system responds to an END SEQUENCE, which is something that the system will not accept (such as a negative 1)So the programs typically say:
Enter a number (-1 to quit)and your while would be
float mynum = 0;
while(mynum > -1)
{
}Hope this helps,
ChiThey mostly come at night...mostly.

Hi Chi,
This works:
:: 9.cpp
#include <stdio.h>
#include <math.h>
#include <iostream.h>float mynum = 1;
main()
{
while (mynum != 0)
{
cout << "enter 0 to quit ";
cout << "enter number: ";
cin >> mynum;
cout << mynum << "\n";
};
return 0;
}
::==While we're at it, could I trap ESC to quit?
Thanks for the help.
If at first you don't succeed, you're about average.M2

![]() |
Prevent DOS window from c...
|
Capturing All Keystrokes?...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |