/*
  Name: 
  Copyright: 
  Author: 
  Date: 14/09/10 21:17
  Description: 
*/

#include<iostream>
#include<istream>
#include<ostream>
#include<string>

int c_f_fraction();
int c_f_decimal();
int c_f_percent();
int convert_to();
int convert();
int help();

std::string convert_code("");

float c_f_fraction_1(0);
float c_f_fraction_2(0);
float c_f_result(0);
float c_f_decimal_1(0);
float c_f_percent_1(0);
int help_code(0);



int main()
{
    std::cout << "\nWhich would you like to convert, fraction, decimal or percentage.<f/d/p/help> ";
    std::cin >> convert_code;
    
    if (convert_code == "f")
    {
        c_f_fraction();
    }
    else if (convert_code == "d")
    {
        c_f_decimal();
    }
    else if (convert_code == "p")
    {
        c_f_percent();
    }
    else
    {
        help();
    }
    
}

int c_f_fraction()
{
    std::cout << "\nPlease input the numerator of the fraction you wish to convert.<#/help> ";
    std::cin >> c_f_fraction_1;
    std::cout << "\nPlease input the denominator of the fraction you wish to convert.<#/help> ";
    std::cin >> c_f_fraction_2;
    
    convert_to();
}

int c_f_decimal()
{
    std::cout << "\nPlease input the value of decimal you would like to convert.<#/help> ";
    std::cin >> c_f_decimal_1;
    
    convert_to();
}

int c_f_percent()
{
    std::cout << "\nPlease input the value of percentage you would like to convert.<#/help> ";
    std::cin >> c_f_percent_1;
    
    convert_to();
}

int convert_to()
{
    if (convert_code == "f")
    {
        std::cout << "\nWhat do you want to convert " << c_f_fraction_1 << '/' << c_f_fraction_2 << " into?<d/p/help> ";
        std::cin >> convert_code;
        
        if (convert_code == "d")
        {
            convert_code = "fd";
            convert();
        }
        else if (convert_code == "p")
        {
            convert_code = "fp";
            convert();
        }
        else
        {
            help();
        }
    }
    else if (convert_code == "d")
    {
        std::cout << "\nWhat do you want to convert " << c_f_decimal_1 << " into?<f/p/help> ";
        std::cin >> convert_code;
        
        if (convert_code == "f")
        {
            convert_code = "df";
            convert();
        }
        else if (convert_code == "p")
        {
            convert_code = "dp";
            convert();
        }
        else
        {
            help();
        }
    }
    else if (convert_code == "p")
    {
        std::cout << "\nWhat do you want to convert " << c_f_percent_1 << " into?<f/d/help> ";
        std::cin >> convert_code;
        
        if (convert_code == "f")
        {
            convert_code = "pf";
            convert();
        }
        else if (convert_code == "d")
        {
            convert_code = "pd";
            convert();
        }
        else
        {
            help();
        }
    }
    else
    {
        help();
    }
}

int convert()
{
    if (convert_code == "fd")
    {
        c_f_result = c_f_fraction_1 / c_f_fraction_2;
        std::cout << "\nIf you convert " << c_f_fraction_1 << '/' << c_f_fraction_2 << " into a decimal. It becomes " << c_f_result << ".\n";
    }
    else if (convert_code == "fp")
    {
        c_f_result = c_f_fraction_1 / c_f_fraction_2;
        c_f_result = c_f_result * 100;
        std::cout << "\nIf you convert " << c_f_fraction_1 << '/' << c_f_fraction_2 << " into a percentage. It becomes " << c_f_result << "%.\n";
    }
    else if (convert_code == "df")
    {
    }
    else if (convert_code == "dp")
    {
        c_f_result = c_f_decimal_1 * 100;
        std::cout << "\nIf you convert " << c_f_decimal_1 << " into a percentage. It becomes " << c_f_result << "%.\n";
    }
    else if (convert_code == "pf")
    {
    }
    else if (convert_code == "pd")
    {
        c_f_result = c_f_percent_1 / 100;
        std::cout << "\nIf you convert " << c_f_percent_1 << " into a decimal. It becomes " << c_f_result << ".\n";
    }
    else
    {
        help();
    }
}

int help()
{
}

Ok so i'm 45 pages into my first read of a C++ book (Exploring C++, Ray Lischner) and I decided to use what I had learnt to create a simple fraction/decimal/percentages converter. I rewrote the code once already to remove garbage functions and variables and I think i'm getting close to making a decently compact program.

I'm posting it because I need help. I cant make code to convert into fractions. I have no background in high level maths (no college/sixth form/uni training). I have spent a few hours on the web and sort of know what needs to be done, but I can't do it.

I need to read how many decimal places are after the decimal point, think of those decimal places as 0's and put a 1 in front of it and convert the decimal into an integer. So 0.75 would be 75/100 (I think) after that i would need to work on loops that can reduce the size of the fraction using Euclidian (a new name for me) maths to put it into its simplest form, 3/4 in this case but what's easy in my mind isn't easy in code.

I realise that in its current state the program is buggy (like decimals being allowed to be over 0.9999999), and has no error handling, this will come later when the help function is finished.

If you could help me find out the code I need I would be grateful. Also if you see any problems with the way I build programs/functions/anything please let me know. Thanks.

A few comments:

1) You should handle all of the user input directly in main and then pass values to the functions (rather than call the functions with no arguments and do the input inside as you have done).

2) Rather than have a conversion from each type to every other one (imagine how complicated this would get if there were 10 types instead of 3!), I would always convert to a "common" format (say always to decimal). Then you can have a single function for each type to convert from the common type. You will then have ~2N functions vs ~N^2 (where N is the number of types)

3) Use descriptive variable and function names! c_f_fraction_1 is not obvious to a first time reader of the code.

Good luck,

David

1) I'm not exactly sure what you mean there, if I could see an example of how it's used i'm sure I could convert my program.

2) I see exactly what you mean here, i'll work on this now.

3) I see what you mean here but at the moment my program is not commented. If you still think it needs to be more verbose after commenting i'll do that.

Thanks for taking the time to post, back to work for me!

1) Instead of

if (convert_code == "f")
    {
        c_f_fraction();
    }

you'd want to do:

main()
{
  if (convert_code == "f")
    {
    std::cout << "\nPlease input the numerator of the fraction you wish to convert.<#/help> ";
    std::cin >> numerator;
    std::cout << "\nPlease input the denominator of the fraction you wish to convert.<#/help> ";
    std::cin >> denominator;
        c_f_fraction(numerator, denominator);
    }
}

double c_f_fraction(double numerator, double denominator)
{
  // your logic here (something like return numerator/denominator;
}

See what I mean? This way the function is much more reusable (if you read numbers from a file, or generated them manually, you can still convert them (rather than relying on user input to happen in every instance).

3) The idea is "documentation by good code writing". The problem is that you have to assume programmers will not keep the documentation up to date (that is, they will change the code but forget to update the documentation). So,you have to train them to write the code in such a way that it is "self documenting". The use of intelligible variable names is a good start.

Thanks again. I think i'm going to have to basically start from the start again but all practice is good practice. I'll post the result (which I hope will be a more compact, reusable program) so someone can check if it conforms to standards.

/*
  Name: FDP Converter
  Copyright: 
  Author: 
  Date: 15/09/10 12:19
  Description: 
*/

#include<iostream>
#include<istream>
#include<ostream>
#include<string>
// functions
double convert_from_fraction(double numerator, double denominator);
double convert_from_decimal(double decimal);
double convert_from_percent(double percent);
int help(int help_code);
// variables
std::string convert_code_1(""); // to hold user input
std::string convert_code_2(""); // to hold user input
std::string convert_code_result(""); // to hold the data of both convert_code_1 and convert_code_2
double numerator(0);    // holds the value of the (users) fractions numerator
double denominator(0);  // holds the value of the (users) fractions denominator
double decimal(0);      // holds the value of the (users) decimal
double percent(0);      // holds the value of the (users) percentage
double result(0);       // may hold the value of a conversion(if neccessary)
int help_code(0);       // used in the help() function to determine what output is printed(unfinished)

int main()
{
    std::cout << "\nWhich would you like to convert, fraction, decimal or percentage.<f/d/p/help> ";
    std::cin >> convert_code_1;
    
    if (convert_code_1 == "f")
        convert_code_result = "fraction";
        
    else if (convert_code_1 == "d")
        convert_code_result = "decimal";
        
    else if (convert_code_1 == "p")
        convert_code_result = "percentage";
        
    else
        help();
    
    std::cout << "\nWhat would you like to convert your " << convert_code_result << " into?<f/d/p/help> ";
    std::cin >> convert_code_2;
    
    if (convert_code_1 == "f")
    {
        std::cout << "\nPlease input the value of your numerator. ";
        std::cin >> numerator;
        std::cout << "Please input the value of your denominator. ";
        std::cin >> denominator;
        
        if (convert_code_2 == "f")
        {
            std::cout << "\nIf you convert " << numerator << '/' << denominator << " into a fraction it becomes " << numerator << '/' << denominator << ". Wait, what?\n";
            main();
        }
        else if (convert_code_2 == "d")
        {
            convert_from_fraction(numerator, denominator);
        }
        if (convert_code_2 == "p")
        {
            convert_from_fraction(numerator, denominator);
        }
        else
            help();        
    }
    else if (convert_code_1 == "d") 
    {
        std::cout << "\nPlease input the value of your decimal. ";
        std::cin >> decimal;
        
        if (convert_code_2 == "f")
        {
            std::cout << "\nThankyou for choosing FDP Converter! We're sorry but the operation that you have chosen is not available at this time, please come back later. Thankyou!\n";
            main();
        }
        else if (convert_code_2 == "d")
        {
            std::cout << "\nIf you convert " << decimal << " into a decimal it becomes " << decimal << ". Wait,what?\n";
            main();
        }
        else if (convert_code_2 == "p")
        {
        }
        else
            help();
    }
    else if (convert_code_1 == "p")
    {
        std::cout << "\nPlease input the value of your percentage. ";
        std::cin >> percent;
        
        if (convert_code_2 == "f")
        {
            std::cout << "\nThankyou for choosing FDP Converter! We're sorry but the operation that you have chosen is not available at this time, please come back later. Thankyou!\n";
            main();
        }
        else if (convert_code_2 == "d")
        {
        }
        else if (convert_code_2 == "p")
        {
            std::cout << "\nIf you convert " << percent << "% into a percentage it becomes " << percent << "%. Wait,what?\n";
            main();
        }
        else
            help();
    } 
    else
        help();
}

double convert_from_fraction(double numerator, double denominator)
{
    // I really don't know what to put here. daviddoria said return 
    // numerator/denominator. Does the maths go here? then we go back to the main() function...
    // or do the maths and output here and program ends here?
}

int help(){} //placeholder

Ok, I rewrote the program in a better way (I think) not sure about the nested if/else statements. Seems like a place I would use switch/case statements in java (I was bad at java too tbh).

if (<variable> <equality> <value>)
      {
          if (<variable> <equality> <value>)
          {

just feels really redundant to me...

Apart from that does this seem like an improvement from the first example?

#include<iostream>
#include<istream>
#include<ostream>

int main();

float decimal(0);
int numerator(0);
int denominator(1);
int numerator_iterator(0);
int denominator_iterator(0);
int GCD(0);

int final_output()
{
    numerator = numerator / GCD;
    denominator = denominator / GCD;
    std::cout << "\nThe GCD at these proportians is: " << GCD << '\n'; 
    std::cout << "\nThe fraction in its smallest form is: " << numerator << '/' << denominator;
    std::cin >> GCD; // used to pause the program at the end
}

int iterating_function()
{
    if (denominator_iterator > numerator_iterator)
    {
        denominator_iterator = denominator_iterator - numerator_iterator;
        iterating_function();
    }
    else if (denominator_iterator < numerator_iterator)
    {
        numerator_iterator = numerator_iterator - denominator_iterator;
        iterating_function();
    }
    else if (denominator_iterator == numerator_iterator)
    {
        GCD = denominator_iterator;
        final_output();
    }    
}

int main()
{
    std::cout << "What decimal do you want to convert into a fraction? ";
    std::cin >> decimal;
    
    if(decimal >= 1)
    {
        std::cout << "\nPlease input a value under 1.\n\n";
        main();
    }    
    else if (decimal < 0.000008)
    {
        std::cout << "\nYou cannot convert a decimal this small.\n\n";
        main();        
    }
    else if (decimal > 0.999993)
    {
        std::cout << "\nYou cannot convert a decimal this large.\n\n";
        main();
    }
            
    std::cout << "\nYou want to convert " << decimal << " into a fraction.\n";
    
    decimal = decimal * 1000000;
    numerator = decimal;
    denominator = denominator * 1000000;
    numerator_iterator = numerator;
    denominator_iterator = denominator;
    
    std::cout << "\nAs a fraction it can be shown as: " << numerator << '/' << denominator << '\n';
            
    iterating_function();    
}

Ok so this is a program I wrote to deal with the problem of not being able to convert into fractions. It converts floats, which can hold 6 digits past the decimal point, hence the x1000000, just to make sure the number is definitely an integer.

Certain numbers break the program, its not the algorithm that's the problem, it's the iterations. after 130156 iterations the program breaks. Hence it can convert a decimal of 0.000008 (125000 iterations), but cant convert a decimal of 0.000007 (142857.1428571429 iterations).

Why the program stops after 130156 iterations I have no idea. If anyone can answer i'd be grateful. If not i'll mark this thread as answered in 24 hours, as I can now convert into fractions (badly I know). And thanks to Daviddoria I know my style is wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.