Computing.Net > Forums > Programming > noobi c++ WHILE prob

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

noobi c++ WHILE prob

Reply to Message Icon

Name: Mechanix2Go
Date: February 26, 2006 at 13:04:24 Pacific
OS: w2k
CPU/Ram: PIII 933/256MB
Product: brand x
Comment:

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 code

It 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:


^C

C:\temp\->



If at first you don't succeed, you're about average.

M2



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: February 27, 2006 at 04:44:45 Pacific
Reply:

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,
Chi

They mostly come at night...mostly.


0

Response Number 2
Name: Mechanix2Go
Date: February 27, 2006 at 05:00:54 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Prevent DOS window from c... Capturing All Keystrokes?...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: noobi c++ WHILE prob

C++ constructor prob www.computing.net/answers/programming/c-constructor-prob/11351.html

C Programming prob. www.computing.net/answers/programming/c-programming-prob/1275.html

Dos c++ timer www.computing.net/answers/programming/dos-c-timer/6884.html