| | |
calling a decimal function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
Ok, so i changed the "/" to a '/' but i can't get my calculate decimal function for work. I want to call a private function calculateDecimalValue() that computes a floating decimal of the fraction inside the enterFractionValue() function after the num and den are filled. I'm not understanding how to get it inside the enterFractionValue() function. I got the slash to work but the decimal in displayDecimal is just junk. Thanks again, Here's the code i have:
class Fraction
{
private:
double num, den;
double decimal;
static char slash;
void calculateDecimalValue(float decimal);
public:
void enterFractionValue();
void displayFraction();
static void displaySlash();
};
//implementation part-to be named-fraction.cpp
char Fraction::slash='/';
void Fraction::displaySlash()
{
cout<<slash<<endl;
}
void Fraction::enterFractionValue()
{
cout <<"Please enter a numerator "<<endl;
cin >>num;
cin.ignore(80,'\n');
cout <<"The numerator is "<<num<<endl;
cout <<"Please enter a denominator "<<endl;
cin >>den;
cin.ignore(80,'\n');
while (den == 0)
{
cout<<"Enter a number greater than zero."<<endl;
cin >>den;
cin.ignore(80,'\n');
}
cout <<"The denominator is "<<den<<endl;
}
void Fraction::calculateDecimalValue(float decimal)
{
decimal = num/den;
cout<<decimal<<endl;
}
void Fraction::displayFraction()
{
int input;
cout<<"Enter the number 1 to see fraction in fraction format or "<<endl;
cout<<"the number 2 to see it in decimal form"<<endl;
cin>>input;
cin.ignore(80,'\n');
while (input != 1 && input !=2)
{
cout<<"Your entry must be a 1 or a 2, please try again."<<endl;
cin>>input;
cin.ignore(80,'\n');
}
if (input == 1)
cout<<"The fraction in traditional form is "<<num <<slash<<den<<endl;
if (input == 2)
cout<<"The fraction in decimal form is " <<decimal<<endl;
}
//main function to be named testfraction.cpp
#include <iostream.h>
#include "fraction.h"
#include "fraction.cpp"
int main()
{
Fraction oneFraction;
oneFraction.enterFractionValue();
oneFraction.displayFraction();
cout << "Press Enter to continue";
getchar();
return 0;
}
class Fraction
{
private:
double num, den;
double decimal;
static char slash;
void calculateDecimalValue(float decimal);
public:
void enterFractionValue();
void displayFraction();
static void displaySlash();
};
//implementation part-to be named-fraction.cpp
char Fraction::slash='/';
void Fraction::displaySlash()
{
cout<<slash<<endl;
}
void Fraction::enterFractionValue()
{
cout <<"Please enter a numerator "<<endl;
cin >>num;
cin.ignore(80,'\n');
cout <<"The numerator is "<<num<<endl;
cout <<"Please enter a denominator "<<endl;
cin >>den;
cin.ignore(80,'\n');
while (den == 0)
{
cout<<"Enter a number greater than zero."<<endl;
cin >>den;
cin.ignore(80,'\n');
}
cout <<"The denominator is "<<den<<endl;
}
void Fraction::calculateDecimalValue(float decimal)
{
decimal = num/den;
cout<<decimal<<endl;
}
void Fraction::displayFraction()
{
int input;
cout<<"Enter the number 1 to see fraction in fraction format or "<<endl;
cout<<"the number 2 to see it in decimal form"<<endl;
cin>>input;
cin.ignore(80,'\n');
while (input != 1 && input !=2)
{
cout<<"Your entry must be a 1 or a 2, please try again."<<endl;
cin>>input;
cin.ignore(80,'\n');
}
if (input == 1)
cout<<"The fraction in traditional form is "<<num <<slash<<den<<endl;
if (input == 2)
cout<<"The fraction in decimal form is " <<decimal<<endl;
}
//main function to be named testfraction.cpp
#include <iostream.h>
#include "fraction.h"
#include "fraction.cpp"
int main()
{
Fraction oneFraction;
oneFraction.enterFractionValue();
oneFraction.displayFraction();
cout << "Press Enter to continue";
getchar();
return 0;
}
Last edited by Guppy25; Feb 6th, 2005 at 10:38 am. Reason: better title
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
You don't need a private function here. Also I don't think it's a good idea to make the numerator and denominator doubles. For one thing, they may not print out properly, i.e., you may have a fraction 3.0/4.0 where you really want 3/4.
Attached is a simple fraction program. (It really should be improved by reducing to lowest terms.)
Attached is a simple fraction program. (It really should be improved by reducing to lowest terms.)
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Guppy25
Ok, so i changed the "/" to a '/' but i can't get my calculate decimal function for work. I want to call a private function calculateDecimalValue() that computes a floating decimal of the fraction inside the enterFractionValue() function after the num and den are filled. I'm not understanding how to get it inside the enterFractionValue() function. I got the slash to work but the decimal in displayDecimal is just junk. Thanks again, Here's the code i have:
class Fraction
{
private:
double num, den;
double decimal;
static char slash;
void calculateDecimalValue(float decimal);
public:
void enterFractionValue();
void displayFraction();
static void displaySlash();
};
//implementation part-to be named-fraction.cpp
char Fraction::slash='/';
void Fraction::displaySlash()
{
cout<<slash<<endl;
}
void Fraction::enterFractionValue()
{
cout <<"Please enter a numerator "<<endl;
cin >>num;
cin.ignore(80,'\n');
cout <<"The numerator is "<<num<<endl;
cout <<"Please enter a denominator "<<endl;
cin >>den;
cin.ignore(80,'\n');
while (den == 0)
{
cout<<"Enter a number greater than zero."<<endl;
cin >>den;
cin.ignore(80,'\n');
}
cout <<"The denominator is "<<den<<endl;
}
void Fraction::calculateDecimalValue(float decimal)
{
decimal = num/den;
cout<<decimal<<endl;
}
void Fraction::displayFraction()
{
int input;
cout<<"Enter the number 1 to see fraction in fraction format or "<<endl;
cout<<"the number 2 to see it in decimal form"<<endl;
cin>>input;
cin.ignore(80,'\n');
while (input != 1 && input !=2)
{
cout<<"Your entry must be a 1 or a 2, please try again."<<endl;
cin>>input;
cin.ignore(80,'\n');
}
if (input == 1)
cout<<"The fraction in traditional form is "<<num <<slash<<den<<endl;
if (input == 2)
cout<<"The fraction in decimal form is " <<decimal<<endl;
}
//main function to be named testfraction.cpp
#include <iostream.h>
#include "fraction.h"
#include "fraction.cpp"
int main()
{
Fraction oneFraction;
oneFraction.enterFractionValue();
oneFraction.displayFraction();
cout << "Press Enter to continue";
getchar();
return 0;
}
![]() |
Similar Threads
- Calling PHP function Onchange on Javascript (PHP)
- Calling PHP function on Javascript events e.g onchange (PHP)
- Calling a random function (Python)
Other Threads in the C++ Forum
- Previous Thread: visual studio c++ and linux?
- Next Thread: GET! GOT! GOTCHA! I still don't Get it...
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelp homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings template text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





