C++ :: Fraction Class - Warning Converting To Int From Double

Jul 28, 2013

I am trying to write a Fraction class and getting the following warning when compiling my code :

Fraction.cpp: In constructor 'Fraction::Fraction(double)':
Fraction.cpp:8: warning :converting to 'int' from 'double'

My Fraction.cpp class looks like :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d) {
cout << This is double param constructor <<endl;
}
Fraction::Fraction(double d):num(d),den(0)

[Code] ....

How can I get rid of the warning ?

View 8 Replies


ADVERTISEMENT

C/C++ :: Fraction Calculator - Converting From Fraction To String

May 27, 2014

I'm having a problem when i convert from fraction to string. When I run my program it runs fine I'm supposed to get an output of the fraction ex (2/5) and the decimal 0.4 the problem is that it does not output the fraction all I get it / and 0.4

the code for converting from fraction to string is the following
std::string string;
char numerator[100]/* = {0}*/;
char denominator[100]/* = {0}*/;
_itoa_s(numerator_, numerator, 10);
_itoa_s(denominator_, denominator, 10);

[Code] .....

View 5 Replies View Related

C/C++ :: Converting A Fraction To Decimal?

Apr 28, 2015

i am having issues converting a fraction to a decimal, i think the code would go in the istream because it is working with input data but i just cant figure it out.

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

[Code].....

View 2 Replies View Related

C++ :: Write A Class Definition For A Fraction Class

Nov 25, 2013

Write a class definition for a Fraction class. Its member fields are num and den, both of type int. The constructor builds the default fraction 1/1. It has the following operations:

void plusEquals(Fraction second);
//Adds the second fraction to this fraction like the operator += void minusEquals (Fraction second);
//Subtracts the second fraction from this fraction void timesEquals (Fraction second);
//Divides this fraction by the second fraction void dividesEquals (Fraction second);
// Divides this fraction by the second fraction void reduce();
// Reduces this fraction to lowest terms double todecimal();
//returns the decimal value of this fraction void scan(istream&);
//scans a fraction written with a slash as in ¾ void print(ostream&);
//prints a fraction using a slash Fraction();
//constructs a default fraction with denominator 1, numerator 0 Fraction(int n, int d); //constructs a fraction given value for num and den

2. Write a menu-driven driver program designed to allow thorough testing of your Fraction class.

View 2 Replies View Related

C++ :: Static Constant Datatype For Fraction Class

Aug 4, 2013

My Fraction.h class looks like :

class Fraction {
int num;
unsigned int den;
public:
Fraction(int = 1,int =1);
//Constants of Datatype

[Code] ....

The implementation Fraction.cpp is as follows :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d){
cout << This is double param constructor <<endl;
}

And the application main.cpp is

int main(){
Fraction f1(3,9);
f1 = Fraction::sc_fUnity; // how to implement this ?
}

How can I write the Fraction.cpp for the constant static member ?

View 6 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

C++ :: Implementation Of Array Index Operator For Fraction Class

Jan 7, 2014

I need an implementation for the array index operator overloading of my Fraction class . The Fraction.h looks like :

#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(int = 1, int = 1);

[Code] .....

I am not able write the Array index operator overloading functions.

View 3 Replies View Related

C++ :: Converting A Double To A String?

Dec 7, 2014

I'm trying to find a way to accuratley convert a double in the form of a bank account number stored in a file into a string representing the number returned by a file.

View 1 Replies View Related

C++ :: Converting A Double To A String

May 2, 2013

i think i need to convert a double to a string, we are working in visual studio doing a program. when i run the calculator i'm not getting the answer i need instead its giving me 0.0 when it should be reading 0.5, here is the code i'm using

{int width;
int height;
int area;
double gop;
String ^strWidth;
String ^strHeight;
String ^strArea;
String ^strGop;
strWidth=width1->Text;

[Code]....

View 1 Replies View Related

C++ :: Converting String Into A Long Double

Mar 11, 2013

I have a problem with converting a C++ string into a long double. In order to do this, I used the function strtold, but the result I get is only the integral part, that is: if for example the input string is 12.476, I only get 12 as the converted long double value. This happens with atof too.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
string test = "12.345";
long double test_longd = strtold(test.c_str(),NULL);

[Code] .....

View 3 Replies View Related

C++ :: Converting String Into Double Array

May 18, 2014

I am having a lot of trouble extracting values from a string into a double array.

I have a string that looks something like this: "asdf 1.320 1 234.43 5.00000 3"

All I want to do is extract the doubles and store them in an array of doubles.

View 8 Replies View Related

C/C++ :: Converting String To Long And Then To Double

Jun 15, 2014

So I have problem with my code, I'm trying to convert string to long and then string to double, but I get this when I compile it:

terminating with uncaught exception of type std::invalid_argument: stod: no conversion

I was thinking if there is a way to do this without using (stol) or (stod)

Market::Market(string filename)
{
string line, word, date, stockprice;
long realdate;

[Code].....

View 14 Replies View Related

C Sharp :: Converting DB Variable To Double In C#

Aug 22, 2012

I'm having some trouble with a variable from a database. That variable in that database is of type double.

To read from the database, I'm using a variable dr of type OleDbDataReader.

So far, I've been using dr["variablename"].ToString() to work with the variables from this database - without the ToString part it wasn't working.

However, with this specific variable (I'll call it "var"), I have to do numerical comparisons of >= and <=, so obviously, just using dr["var"].ToString() doesn't work.

Therefore, I tried creating a variable of type double called varConv and setting it as

varConv = Convert.ToDouble(drPen["var"].ToString()).

That doesn't work, gives me an error message saying "Input string was not in a correct format". If I remove the ToString part, it also doesn't work, with a different error message saying "Object cannot be cast from DBNull to other types".

Trying to do a direct comparison, as in dr["var"] >= x also doesn't work, says "Operator '>=' cannot be applied to operands of type 'object' and 'int' ".

View 3 Replies View Related

C++ :: Converting Multiple Lines Of Strings To Double

Aug 26, 2014

I am trying to convert multiple lines of strings to double with std::stod .... I used the code found on the website:

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
int main() {
std::string two_doubles =" 22145.365 -3564789";
std::string::size_type K;
double first = std::stod (two_doubles, &K);
double second = std::stod (two_doubles.substr(K));
}

The string starts with white spaces. I get this error message when compiling:

warning unused variable 'first'
warning unused variable 'second'

How do you convert the two numbers in the string two_doubles to doubles?

View 2 Replies View Related

C/C++ :: Converting Int To Double Pointer Outside Main Function

Dec 22, 2014

I have int pointer called p and i want to calculate average.Since average involves using double or float so i am trying to convert this in the function averagetemp. It still gives me an error saying "cannot be converted"...

#include <iostream>
using namespace std;
int* createArray(int n);
int lowesttemp(int *p,int f);
int highesttemp(int *p,int f);
double averagetemp(int *k,double f);
void print(int *p,int lowest_temp,int highesttemp,int average_temp);

[Code] ....

View 8 Replies View Related

C++ :: Display Fraction In Proper From Based On 2 Arguments Passed To Class Member Function

Mar 15, 2015

We're assigned a project working with classes and fractions. My goal is to display a fraction in proper from based on 2 arguments passed to a class member function proper();

My strategy was to utilize the greatest common factor between the 2 arguements, then divide both the numerator and denominator by that number and then it would display.

The program actually runs, but only seems to divide the numerator and not the denominator. This in return makes my other class member functions have incorrect comparisons and sums.

Code:
#include<iostream>
#include<conio.h>
class Fraction {
friend void compare(Fraction a, Fraction b);
friend void sum(Fraction a, Fraction b);

[Code] ....

View 14 Replies View Related

C++ :: Template Class Which Is Designed To Take Int Or Double

Sep 25, 2013

Let's say I defined a template function or a template class which is designed to take int or double. I use it for the first time with int arguments so the template is instantiated using int. During instantiation a code is actually generated. Then I use this template second time with int argument, so is it considered that it is instantiated again? Is new code generated again or compiler is using code from the the first time I used my template with int?

View 5 Replies View Related

C++ :: Converting Object To Different Class

Jun 22, 2014

Sandy is of class Person, who converts to Muslim and takes on a new name Fatima, and has all the attributes of Sandy, with new Muslim attributes (e.g. religion == Islam, etc..). At this point, Sandy can be deleted, and Fatima, now of class Muslim will play Sandy's role henceforth. The problem is that due to her new address, all the people who knew Sandy does not know Fatima. Manually changing Sandy's address to Fatima's address for all those people who knew Sandy is clearly not an acceptable method. I looked through all the design patterns and cannot find one to solve this problem. how to improve the design? Here's my simplified code showing the problem:

#include <iostream>
#include <string>
#include <typeinfo>
class Person {
std::string name;
Person* bestFriend;

[Code]...

Output:

sandy = 0x32658, 6Person
Mary's best friend is Sandy.
fatima = 0x23fec0, 6Muslim
Mary's best friend is Sandy.

Of course, we want to have: Mary's best friend is Fatima,. with mary->getBestFriend()->getReligion() == Islam, etc... How to redesign the whole thing so that this is automated (assume there are thousands of people who know her)?

View 14 Replies View Related

C++ :: Converting From Class Array To Int

Mar 24, 2014

Version 1 (works fine)

Creating array in main and then calling a sorting function from sorting class.

In main:

const size_t SIZE = 100;
int *array = new int [SIZE];
//fill array with ints, not shown here
quickSort(array, SIZE); //calling the sorting function in the sorting class

In the sorting class, quickSort is declared as such:

void quickSort(int arr[], int num);

Everything works great.

Version 2 (issues)

Instead of creating the array in main, I have set up a class for the array, MyArrayClass, where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Even though my MyArrayClass object contains an array of ints, the code calling for quickSort() won't compile as the data type isn't ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from 'const MyArrayClass' to 'int[]'.

How do I cast my class object array of ints as an int[] in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int[] might be easier...

View 6 Replies View Related

Visual C++ :: Converting Program From Struct To Class

Sep 20, 2014

I am not exactly sure how to do this and i keep running into problems. This is my code here that works.

Code:
#include<iostream>
using namespace std;
struct record {
double quiz1;
double quiz2;
double midyear, midyear_one;

[Code] .....

View 5 Replies View Related

C++ :: Creating A Class That Would Implement Concept Of Double Linked List Like A Queue

Jun 15, 2013

Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).

The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?

Worked perfectly before including all the template stuff..

// Source.cpp <=> Main.cpp
#include <iostream>
#include "DList.h"
using namespace std;
int main(void) {
DList<int> *list;
list = new DList<int>();

[Code] .....

The errors returned by the compiler:

Error1error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
Error2error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6

View 6 Replies View Related

C++ :: Class To Store Nodes Of Tree - Converting Between Types

Jun 4, 2013

I'm writing a class to store a tree I have a small issue.

I have a class which stores a node of the tree, something like this:

template<class T>
class node {
private:
(stuff regarding parent node and child nodes);
T __data;
public:
// more stuff (constructors and utility functions) here
};

And I want to I want to be able to do the conversion T(node<T>) implicitly. For example, I want to be able to do this:

node<int> myNode;
myNode=5;
myNode+=10;
myNode*=9;
int x=int(myNode/4);
cout << x+myNode;

And this:

class myClass {
int x;
int y;
void print () {cout << x << ' ' << y << endl;}

[Code] ....

How could I achieve this?

View 2 Replies View Related

C++ :: Get Fraction For Any Decimal Number In Variable?

Jun 9, 2013

How do i get the fraction for any decimal number in a variable?

Code: Code: #include<iostream>
using namespace std;
int main()
{
double x = 1/6;
cout << x;
cin.get();
}

View 1 Replies View Related

C++ :: Writing A Function That Compares Two Fraction?

Dec 25, 2014

I'm writing a function that compares two fraction. if the fractions are equal it returns 0. If the fraction in the first parameter is less than the fraction in the second parameter it returns a negative number. Otherwise it returns a positive number. in doing so convert the fraction to a floating point number.

completing the function to run

typedef struct fracion
{
int numo;
int denom;
}fraction;

[Code].....

View 2 Replies View Related

C++ :: How To Give Fraction Object In S String Via Template

May 9, 2014

I am trying to give fraction object in s string via template.

struct Fr // simple Fraction {
int num;
int denom;
Fr(int i,int j) {

[Code] ....

View 10 Replies View Related

C++ :: Create A Program That Calculates Fraction Of Time?

Dec 16, 2013

I am trying to create a program that calculates fraction of time remaining when a student leaves during the middle of the semester. I have to use function files here. I am having trouble with the division in function long double tuitionfrac(int, int, int). No matter what numbers are entered, fractime gives an output of 0. I don't understand that.

long double tuitionfrac(int opening, int leaving, int semend)
{
int lefttime = semend - leaving;
cout << "Left time " << lefttime << endl; //for error checking
int tottime = semend - opening + 1;
cout << "Total time " << tottime << endl;
long double fractime = lefttime/tottime;
cout << "fractional time " << fractime << endl; //always returns as 0.
return fractime;
}

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved