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

This is a discussion on What's wrong with this code(decimals to fractions)? within the C++ Programming forums, part of the General Programming Boards category; The code actually works, but I would like to know what in this program would be considered "bad" programming. This ...

  1. #1
    Unregistered
    Guest

    Lightbulb 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. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    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. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  2. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. can't get this function to reduce fractions
    By bruce in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 12:07 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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