# Thread: What's wrong with this code(decimals to fractions)?

1. ## What's wrong with this code(decimals to fractions)?

The code actually works, but I would like to know what in this program would be considered "bad" programming. This code converts decimals to fracts. It also is not object-oriented.

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main()
{
char decimal[21], numerator_string[21];
int gcf, reduced_numerator, reduced_denominator;
long length, denominator, numerator;

cout << "Enter decimal: ";
cin.get(decimal, 21);
cin.ignore(80, '\n');

length = strlen(decimal);

// omit the "." to get the numerator
for(int i = 0; i <= length; i++)
{
numerator_string[i] = decimal[i+1];
}

length = strlen(numerator_string);
denominator = pow10l(length);

numerator = atol(numerator_string);

for(int x = 1; x <= numerator; x++)
{
if((numerator % x == 0) && (denominator % x == 0))
{
gcf = x;
}
}

if (gcf == 1)
{
cout << "Can't be turned to a fraction" << endl;
getch();
return 0;
}

reduced_numerator = numerator / gcf;
reduced_denominator = denominator / gcf;

cout << "\n" << reduced_numerator << "\\"
<< reduced_denominator << endl;

getch();
return 0;
}

2. The only thing I could frown apon is

char decimal[21], numerator_string[21];
int gcf, reduced_numerator, reduced_denominator;
long length, denominator, numerator;

Global variables, and declaring more than 1 variable a line.

3. a couple people gave me slack for using the .h's in your headers (instead of namespace stuff), i don't know the real difference other than one's iso standard.