### Input not accepting "1E6"

Hello. I am working on a project and I cannot seem to get this to work. I am having the user input the final frequency and for the "test" values my teacher gave us, he has the final frequency to equal 1E6Hz. If I type in 1000000Hz, everything works fine, but if I type in 1E6Hz, it does not work. Maybe it has something to do with my string? Can't figure it out. Any help would be great.

I have fftype as a string.

do
{
cout << "Please enter the final frequency (Hz, kHz, MHz): ";
cin >> ff >> fftype;
if (ff >= 1E9 || ff < 2*fi)
{
cout << "ERROR: Finial frequency must be less than 1E9Hz and\n";
cout << "greater than twice the initial frequency.\n";
}
}
while (ff >= 1E9 || ff < 2*fi);

if (fftype[0] == 'k')
ff=ffi*1000;
else if (fftype[0] == 'M')
ff=ffi*1000000;
else
ff=ffi;

First, please use the code brackets to the right when posting. It's the <> button when you edit/post. Makes it much easier to read the code, gives lines, and keep any indentation you may have. :)

What types are ff and fftype?. Without seeing how they are declared we can't help you.
Okay sorry about that. First time posting on here!
 ``123456789101112131415161718192021`` ``````double fi=0.0, ff=0.0; string ftype; do { cout << "Please enter the final frequency (Hz, kHz, MHz): "; cin >> ff >> fftype; if (ff >= 1E9 || ff < 2*fi) { cout << "ERROR: Finial frequency must be less than 1E9Hz and\n"; cout << "greater than twice the initial frequency.\n"; } } while (ff >= 1E9 || ff < 2*fi); if (fftype[0] == 'k') ff=ff*1000; else if (fftype[0] == 'M') ff=ff*1000000; else ff=ff;``````
Ah ok, they are simply doubles. I was wondering if you had a special type you defined. I don't know how you would read a double in scientific notation into the variable. I know for sure that in the `if` statement to make the comparison you should use a straight integer value like 1000000. You may have to write something to convert scientific to regular decimal or use something in the STL to do so. I'm not as familiar with that though. Sorry I couldn't be more help.
Does it work if you separate the cin statements?

 ``1234`` ``````cout << "Please enter the final frequency value " << endl; cin >> ff ; cout << "Please enter the frequency Unit (Hz, kHz, MHz): " << endl; cin >> fftype;``````

Edit:

Raezzor is right - you need to be careful comparing floating point values (float or double). They are represented as binary fractions and cannot represent all real numbers exactly. For this reason comparison conditions often fail. Consider this:

 ``1234`` ``````float a = 0.1; //a == 0.09999997 float b = 10 * a; //b == 0.99999997 if (b == 1.0) //false ``````

It looks like your example the numbers are whole even though they are doubles. Although I don't know what calculations you might want to do.

You can use `static_cast<int>` to cast a double into an int.

Also your loop and if condition should use the && operator not || like this:

`(ff >= 1E9 && ff < 2*fi)`

The OR operator returns true if ANY of the clauses are true (it doesn't even continue evaluation as soon as a true condition is found), whereas with AND, ALL of them must be true. So in your example, if you entered a value that was bigger than `2 * fi` the result would still be true.

I personally dislike do loops - I notice you have the test twice, can you figure out how to do it with a while loop and one test?

Hope all goes well. :)

Last edited on
Topic archived. No new replies allowed.