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