bound checking problem

This is a discussion on bound checking problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> //#include <string> using namespace std; class temp { private: float t,r; char choice; public: void readtemp () ...

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    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
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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. #3
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    and one more question, if i use double instead of float, still i can display only 6 digits in total... any help?
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  4. #4
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    lol, yeah
    I didn't put cout inside the loop so i was confused.. thx
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  5. #5
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    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.?
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    #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]
    Last edited by scwizzo; 06-13-2009 at 08:12 AM.

  7. #7
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    Thx, that helped..
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  8. #8
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    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...
    Last edited by n3cr0_l0rd; 06-13-2009 at 08:50 AM.
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Beep (MSDN)

    You need to link the kernel32 library (at the bottom of that page).
    Last edited by scwizzo; 06-13-2009 at 09:30 AM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by n3cr0_l0rd View Post
    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. #11
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    i didnt hear anything..
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  12. #12
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    i had a look at the MS MSDN page,.. but how to i link to the kernel32.lib and kernel32.dll
    I had crush on her, before that crush could CRUSH me, i CRUSHED that crush !!! Fun time over, Lets find a way to not to blow off the whole leg !

    __________________________________________________ __________________________________________________ _______________

    In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
    - Bjarne Stroustrup

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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?
    Attached Images Attached Images  
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. error checking problem with scanf
    By Axel in forum C Programming
    Replies: 1
    Last Post: 10-19-2005, 05:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21