Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, everyone. I am trying to write a program that displays fraction using toString function. i let the user enter a numerator and a denominator and i display the fraction. if the user does not input anything, i display 0/1 as my default fraction. i am having trouble with my toString function and cannot seem to understand the error that i am getting. Please have a look at my code, and point out/revise the errors. i cannot thank you enough :)

C++
#include <iostream>
#include <string>
#include<sstream>
using namespace std;

class Fractions {
    int num,den;
  public:
    Fractions ();
    Fractions (int,int);
    int set_numerator(int num);
    int set_denominator(int denom);
    string toString();
};

Fractions::Fractions() {
  num= 0;
  den = 1;
  int set_numerator(0);
  int set_denominator(1);


}

Fractions::Fractions(int a, int b) {
  int set_numerator(a);
  int set_denominator(b);
}
int Fractions::set_numerator(int n)
{
        num = n;
}
int Fractions::set_denominator(int d)
{
        if (d=0){
            cout << "YOU CAN NOT DIVIDE BY ZERO" << endl;
                den== 1;
                }
        else
            den = d;
}
 static string toString(int num,int den){
        stringstream s1,s2;
        s1<<num;
        s2<<den;
        return s1.str()+"/"+s2.str();
    }


int main () {
    int a,b;
    Fractions f1;
    Fractions f2(a,b);
 cout<<"Enter a numerator:"<<endl;
 cin>>a;
 cout<<"Enter a denominator:"<<endl;
 cin>>b;
 if(b==0){
 cout<<"Default Fraction: "<<f1.toString()<<endl;
 }
 else{
  f2.set_numerator(a);
  f2.set_denominator(b);
 cout<<"Your Fraction: "<<f2.toString()<<endl;
 }
  return 0;
}
Posted
Comments
Mohibur Rashid 27-Nov-15 0:09am    
What is the error?
PinkPrint 27-Nov-15 1:27am    
It says undefined reference to toString function(). And if doesn't even show the line numbers for the error that's why I don't know what to make of the error message. Thank you
Mohibur Rashid 27-Nov-15 0:15am    
You have declared string toString();

but you haven't declare the body. Same problem goes with set_denominator
[no name] 27-Nov-15 0:22am    
We don't write code here or clean up buggy code. There are many and fundamental errors. Start by reading carefully what the compiler is telling you (which you have not told us) and make an effort to address those messages first. Google will help.
By all means come back with a specific question.
PinkPrint 27-Nov-15 1:26am    
I have been searching the toString method in Google all day. There is one to_string method in cplusplusreference that doesn't work when I compile it. I have come this forum so that I can get help from real people who are willing to help. I'm sure this is not an exclusive club where few get access. Thank you

Unfortunately your code features many errors. Try
C++
 #include <iostream>
 #include <string>
 #include <sstream>
 #include <stdexcept>

using namespace std;

class Fractions {
    int num,den;
  public:
    Fractions ();
    Fractions (int,int);
    void set_numerator(int num);
    void set_denominator(int denom);
    string toString();
};

Fractions::Fractions():Fractions(0,1){}

Fractions::Fractions(int a, int b)
{
  if ( b == 0) throw logic_error("invalid argument for denominator");
  num = a, den = b;
}

void Fractions::set_numerator(int n)
{
  num = n;
}

void Fractions::set_denominator(int d)
{
  if ( d == 0) throw logic_error("invalid argument for denominator");
  den = d;
}

string Fractions::toString()
{
  stringstream s;
  s << num << "/" << den;
  return s.str();
}


int main ()
{
  int a,b;
  Fractions f1;
  cout<<"Enter a numerator:"<<endl;
  cin>>a;
  cout<<"Enter a denominator:"<<endl;
  cin>>b;
  Fractions f2(a,b);
  cout << "f1 " << f1.toString() << endl;;
  cout << "f2 " << f2.toString() << endl;
}
 
Share this answer
 
My C++ is a little rusty, but this looks like a teaching moment.

Hint 1: What does the static keyword do to a member function?
Hint 2: How do signature differences affect member function calls?

When you can answer these questions, you should be able to see the problem.
 
Share this answer
 
C++
cout<<"Default Fraction: "<<f1.toString()<<endl;

Look at your definition of toString; is it a member of the Fractions class? See also https://msdn.microsoft.com/en-us/library/s1sb61xd.aspx[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900