/*
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.