-
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;
}
-
[edit] hrmm...ne'r mind.... re reading
-
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;
}
}