# bound checking problem

• 06-13-2009
n3cr0_l0rd
bound checking problem
Code:

```#include <iostream> //#include <string> using namespace std; class temp {     private:         float t,r;         char choice;     public:         void readtemp ()         {             cout << "1. Celsius to Fahreinheit" << endl << "2. Fahreinheit to Celsius" << endl;             cout << "Enter your choice : ";             do             {                 cin >> choice;             }while (choice != '1' && choice != '2');             cout << endl << "Enter the temperature : ";             cin >> t;         }         void changetemp ()         {             if (choice == 1)                 r = ((9/5.0) * t) + 32;             else                 r = ((t-32) * 5) / 9;         }         void showtemp ()         {             if (choice == 1)                 cout << t << char (248) << " Celsius = " << r << char (248) << " Fahreinheit";             else                 cout << t << char (248) << " Fahreinheit = " << r << char (248) << " Celsius";         } }; int main () {     temp t1;     t1.readtemp ();     t1.changetemp ();     t1.showtemp ();     return 0; }```
When i enter any number except 1 and 2, nothing happens and i have no choice except force exiting using ctrl + c.
Compiler : Codeblocks 8.02
• 06-13-2009
scwizzo
Worked perfect for me on codeblocks 8.02 also. I think you're confusing an infinite loop with the fact that the cursor is waiting for another input. Type 3, then type 1 or 2, it will work correctly. Try putting this to see for yourself.

Code:

```void readtemp ()         {             cout << "1. Celsius to Fahreinheit" << endl << "2. Fahreinheit to Celsius" << endl;             cout << "Enter your choice : ";             do             {                 cout<< "Please enter a different number: ";                 cin >> choice;             }while (choice != '1' && choice != '2');             cout << endl << "Enter the temperature : ";             cin >> t;         }```
• 06-13-2009
n3cr0_l0rd
and one more question, if i use double instead of float, still i can display only 6 digits in total... any help?
• 06-13-2009
n3cr0_l0rd
lol, yeah
I didn't put cout inside the loop so i was confused.. thx
• 06-13-2009
n3cr0_l0rd
Code:

```        void readtemp ()         {             do             {                 system ("cls");                 cout << "1. Celsius to Fahreinheit" << endl << "2. Fahreinheit to Celsius" << endl;                 cout << "Enter your choice : ";                 cin >> choice;             }while (choice != '1' && choice != '2');             cout << endl << "Enter the temperature : ";             cin >> t;         }```
did some editing, now it looks much better...

and what about the double thing... anybody know why i get only 6 digits.?
• 06-13-2009
scwizzo
#include <iomanip.h>
http://www.exforsys.com/tutorials/c-...ipulators.html

Code:

```void showtemp ()         {             if (choice == 1)                 cout << t << char (248) << " Celsius = " << fixed << setprecision(10) << r << char (248) << " Fahreinheit";             else                 cout << t << char (248) << " Fahreinheit = " << fixed << setprecision(10) << r << char (248) << " Celsius";         }```
edit: keep in mind a double is only so accurate, as is a float, so setting the precision to 99 obviously isn't going to work [correctly] :)
• 06-13-2009
n3cr0_l0rd
Thx, that helped..
• 06-13-2009
n3cr0_l0rd
one more question, i would like to produce a beep sound if the input is not 0 or 1. I searched for it in the net and found '\a' but that didnt help me. So, how can i produce a beep sound?

Edit: Okay i found one more ... Beep(frequency, duration) included in windows.h.... i tried it with no success... :(
• 06-13-2009
scwizzo
Beep (MSDN)

You need to link the kernel32 library (at the bottom of that page).
• 06-13-2009
tabstop
Quote:

Originally Posted by n3cr0_l0rd
one more question, i would like to produce a beep sound if the input is not 0 or 1. I searched for it in the net and found '\a' but that didnt help me. So, how can i produce a beep sound?

Edit: Okay i found one more ... Beep(frequency, duration) included in windows.h.... i tried it with no success... :(

No success meaning it didn't compile, or no success meaning you didn't hear anything? (If the latter, make sure your speakers are turned on.)
• 06-16-2009
n3cr0_l0rd
i didnt hear anything..
• 06-16-2009
n3cr0_l0rd
i had a look at the MS MSDN page,.. but how to i link to the kernel32.lib and kernel32.dll
• 06-16-2009
dwks
You probably already are if it compiled. (For future reference, it's usually in project settings or compiler settings; the details depend on your IDE. kernel32.lib is the library you'd add to the "Linked libraries" section, and the .dll would have to be in the right place at runtime for your program to execute properly. It's in the right place by default, so don't worry about that file.)

Printing '\a' should work (at least it did the last time I used Windows), but there's no telling what sound it will make. Sometimes it's a beep, sometimes it's the default windows notification message. :)

Also: there's really no need to use non-standard extended ASCII characters like this.
Code:

`cout << t << char (248) << " Fahreinheit = " << r << char (248) << " Celsius";`
But if you insist on it, you might be interested in knowing that you can embed those characters into strings if you convert the numbers into hexadecimal.
Code:

```\$ python Python 2.4.3 (#1, Mar 13 2008, 13:33:54) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> hex(248) '0xf8' >>> \$```
So
Code:

`cout << t << "\xf8 Fahreinheit = " << r << "\xf8 Celsius";`
Anyway, you might be interested in seeing how \xf8 looks on my computer. (See attached image.) Not very pretty, eh?