# Thread: bound checking problem

1. ## bound checking problem

Code:
```#include <iostream>
//#include <string>
using namespace std;
class temp
{
private:
float t,r;
char choice;
public:
{
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.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

2. 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;
}```

3. and one more question, if i use double instead of float, still i can display only 6 digits in total... any help?

4. lol, yeah
I didn't put cout inside the loop so i was confused.. thx

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

6. #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]

7. Thx, that helped..

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

9. Beep (MSDN)

You need to link the kernel32 library (at the bottom of that page).

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

11. i didnt hear anything..

12. i had a look at the MS MSDN page,.. but how to i link to the kernel32.lib and kernel32.dll

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

Popular pages Recent additions