Thread: Infinite loop problem

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Infinite loop problem

    Hi all!

    This program seems to get stuck in an infinite loop anytime someone enters an octal number and then tries to display it as a a hexadecimal one... I've been scratching my head for hours-- could anyone give me a hand in figuring out why this is happening?

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    
    using namespace std;
    
    class number
    {
        private:
            int value;
        public:
            int returnnumber();
            void enternumber(int enter);
    };
    
    int number::returnnumber()
    {
        return value;
    }
    
    void number::enternumber(int enter)
    {
        value = enter;
    }
    
    class hexa
    {
    public:
        int input();
        void displayhex(int disp);
    };
    
    int hexa::input()
    {
        int num;
        cout << "Enter a hex number: ";
        cin >> hex >> num;
        return num;
    }
    
    void hexa::displayhex(int disp)
    {
        cout << "Hex: " << hex << disp << endl;
    }
    
    class octal
    {
    public:
        int input();
        void displayoct(int disp);
    };
    
    int octal::input()
    {
        int num;
        cout << "Enter an octal number: ";
        cin >> oct >> num;
        return num;
    }
    
    void octal::displayoct(int disp)
    {
        cout << "Oct: " << oct << disp << endl;
    }
    
    class binary
    {
    public:
        int input();
        void displaybin(int dec);
    };
    
    int binary::input()
    {
        int a=0;
        int b=0;
        int c;
        int power;
        int dec=0;
        char str[100];
        for (a=0;a<100;a++)
        {
            str[a] = '\0';
        }
        fflush(stdin);
        cout << "Enter binary number: ";
        cin.getline(str, 100);
        while (str[b] != '\0')
        {
            b++;
        }
        b -= 1;
        for (a=0;a<=b;a++)
        {
            power = b - a;
            if (str[a] == '1')
            {
                dec += pow(2, power);
            }
        }
        return dec;
    }
    
    void binary::displaybin(int dec)
    {
        char str[1000];
        int a=0;
        for (a;a<1000;a++)
        {
            str[a] = '\0';
        }
        a=0;
        while (dec != 0)
        {
            if (dec % 2 == 1)
            {
                str[a] = '1';
            }
            else
            {
                str[a] = '0';
            }
            dec = dec / 2;
            a++;
        }
        cout << "Binary: ";
        for (a;a>=0;a--)
        {
            cout << str[a];
        }
        cout << endl;
    }
    
    
    
    
    int main()
    {
        number num;
        binary bino;
        hexa hexer;
        octal octa;
        int choice=0;
        int decnumber;
        while (choice != 9)
        {
            cout << choice << endl;
            cout << "Menu:" << endl 
                << "1) Enter binary number" << endl
                << "2) Enter octal number" << endl
                << "3) Enter decimal number" << endl
                << "4) Enter hexadecimal number" << endl
                << "5) Display number in binary" << endl
                << "6) Display number in octal" << endl
                << "7) Display number in decimal" << endl
                << "8) Display number in hexadecimal" << endl
                << "9) Exit Program" << endl
                << "Choice: ";
            cin >> choice;
            switch (choice)
            {
            case (1):
                {
                    num.enternumber((bino.input()));
                    continue;
                }
            case (2):
                {
                    num.enternumber(octa.input());
                    continue;
                }
            case (3):
                {
                    fflush(stdin);
                    cout << endl << "Enter a decimal number: ";
                    cin >> dec >> decnumber;
                    num.enternumber(decnumber);
                    continue;
                }
            case (4):
                {
                    num.enternumber(hexer.input());
                    continue;
                }
            case (5):
                {
                    bino.displaybin(num.returnnumber());
                    continue;
                }
            case (6):
                {
                    octa.displayoct(num.returnnumber());
                    continue;
                }
            case (7):
                {
                    cout << "Decimal: " << dec << num.returnnumber();
                    continue;
                }
            case (8):
                {
                    hexer.displayhex(num.returnnumber());
                    continue;
                }
            }
        }
    return 0;
    }

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    [edit] hrmm...ne'r mind.... re reading
    Last edited by Scribbler; 02-19-2005 at 11:35 PM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    8 is not an octal digit. You are leaving the input stream in octal mode, thus when you encounter an 8, the stream refuses to read it, and sets failbit. You never check the status of your stream so you spin forever, demanding the octal value of the single digit 8.
    Code:
    if(!(cin >> choice)) {
        cin.clear();
        std::string token;
        if(cin >> token) {
            if(token == "quit") return 0;
            cout << "I did not understand " << token << endl;
            continue;
        } else {
            std::cerr << "failure on standard input, giving up" << endl;
            return 2;
        }    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  5. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM