-
loop problem in program
I'm trying to make a value converter to use at work, and I can't seem to get the second part to work correctly. I'm trying to get the user, myself, to input a frequency then ouput the corresponding channel number. I'm not really sure what the problem is right now. I'm pretty stumped at this point. The first statement works in the second for loop, but I can't get anything after that to work. I've thrown in some couts to try to troubleshoot it without much luck.
-
Man, you've just got a lot to learn - like arrays would really help with this problem.
Code:
#include <iostream.h>
#include <math.h>
const int MaxChannels = 96;
// A table of frequencies
// Frequencies[0] is channel 1
double Frequencies[MaxChannels] = {
191.50, 191.55, 191.60, 191.65, 191.70, 191.75, 191.80, 191.85,
191.90, 191.95, 192.00, 192.05, 192.10, 192.15, 192.20, 192.25,
192.30, 192.35, 192.40, 192.45, 192.50, 192.55, 192.60, 192.65,
192.70, 192.75, 192.80, 192.85, 192.90, 192.95, 193.00, 193.05,
193.10, 193.15, 193.20, 193.25, 193.30, 193.35, 193.40, 193.45,
193.50, 193.55, 193.60, 193.65, 193.70, 193.75, 193.80, 193.85,
193.90, 193.95, 194.00, 194.05, 194.10, 194.15, 194.20, 194.25,
194.30, 194.35, 194.40, 194.45, 194.50, 194.55, 194.60, 194.65,
194.70, 194.75, 194.80, 194.85, 194.90, 194.95, 195.00, 195.05,
195.10, 195.15, 195.20, 195.25, 195.30, 195.35, 195.40, 195.45,
195.50, 195.55, 195.60, 195.65, 195.70, 195.75, 195.80, 195.85,
195.90, 195.95, 196.00, 196.05, 196.10, 196.15, 196.20, 196.25,
};
int main(int argc, char* argv[] ) {
char d;
int channel;
double frequency;
cout << endl;
cout << "If you know the channel number enter c, "
"or if you know the frequency enter f.";
cout << endl;
cin >> d;
switch ( d ) {
case 'c':
cout << endl;
cout << "What is the channel number?";
cout << endl;
cin >> channel;
if ( (channel<1) || (channel>MaxChannels) ) {
cout << endl;
cout << "I\'m sorry that is not a valid channel at this time.";
cout << endl;
} else {
cout << endl;
cout << "Channel: " << channel << endl;
cout << "That channel\'s related information is:";
cout << endl;
cout << "\tFrequency: " << Frequencies[channel-1] << endl;
}
break;
case 'f':
cout << endl;
cout << "Enter a frequency between 191.50 and 196.25THz.";
cout << endl;
cin >> frequency;
for ( channel = 0 ; channel < MaxChannels ; channel++ ) {
if ( fabs(frequency - Frequencies[channel]) < 0.01 ) {
cout << "freq= " << frequency << endl;
cout << "\tChannel: #" << channel+1 << endl;
cout << endl;
break; // stop looking - we've found it
}
}
if ( channel == MaxChannels ) {
// didn't find it, print message
cout << endl;
cout << "That is not a valid frequency at this time.";
cout << endl;
}
break;
}
return 0;
}
Oh, and this....
fabs(frequency - Frequencies[channel]) < 0.01
You can't compare floating point numbers for equality - if the two numbers are off by even 0.00000....001, they will compare not equal. So you have to use some kind of 'close enough' test.
This probably explains why some of your comparisons passed, and others failed.
-
ok
Yes, I have pretty much the whole journey ahead of me yet.
Thanks for the help.
/em runs off to read about arrays.