Homework help

My program is supposed to make a mixed fraction. for example, the fraction 7/2 should be outputted as 3 1/2. The fraction is still not coming our correctly with my current code. Is there an error in logic in my reduce function or possibly somewhere else?


Fraction is: 32767/2241670080

here is the code:



[/code]
Last edited on
1. You have two using namespace std;
2. Your choice of parameter names and member names is messing you up.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$ g++ -std=c++11 -Wall -Wextra -Wshadow foo.cpp
foo.cpp:26:9: error: expected nested-name-specifier before ‘namespaceusing namespace std;
         ^
foo.cpp: In constructor ‘Fraction::Fraction(int, int)’:
foo.cpp:41:52: warning: declaration of ‘denominator’ shadows a member of ‘Fraction’ [-Wshadow]
   Fraction::Fraction(int numerator, int denominator) {
                                                    ^
foo.cpp:10:7: note: shadowed declaration is here
   int denominator;
       ^
foo.cpp:41:52: warning: declaration of ‘numerator’ shadows a member of ‘Fraction’ [-Wshadow]
   Fraction::Fraction(int numerator, int denominator) {
                                                    ^
foo.cpp:11:7: note: shadowed declaration is here
   int numerator;
       ^
foo.cpp: In member function ‘void Fraction::setNum(int)’:
foo.cpp:45:38: warning: declaration of ‘numerator’ shadows a member of ‘Fraction’ [-Wshadow]
   void Fraction::setNum(int numerator) {
                                      ^
foo.cpp:11:7: note: shadowed declaration is here
   int numerator;
       ^
foo.cpp: In member function ‘void Fraction::setDen(int)’:
foo.cpp:48:40: warning: declaration of ‘denominator’ shadows a member of ‘Fraction’ [-Wshadow]
   void Fraction::setDen(int denominator) {
                                        ^
foo.cpp:10:7: note: shadowed declaration is here
   int denominator;
       ^
foo.cpp: In member function ‘void Fraction::setFraction(int, int)’:
foo.cpp:60:60: warning: declaration of ‘denominator’ shadows a member of ‘Fraction’ [-Wshadow]
   void Fraction::setFraction(int numerator, int denominator) {
                                                            ^
foo.cpp:10:7: note: shadowed declaration is here
   int denominator;
       ^
foo.cpp:60:60: warning: declaration of ‘numerator’ shadows a member of ‘Fraction’ [-Wshadow]
   void Fraction::setFraction(int numerator, int denominator) {
                                                            ^
foo.cpp:11:7: note: shadowed declaration is here
   int numerator;
       ^
Okay removed the extra using namespace std; So in the constructors I should put the name of what I'm initializing? or what do you recommend?
All member variables have a unique prefix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  private:
  int m_denominator;  // 
  int m_numerator;
...
  void Fraction::setFraction(int numerator, int denominator) {
  if (numerator < 0 || denominator < 1) {
  m_numerator = 0;
  m_denominator = 1;
  }
  else {
  m_numerator = numerator;
  m_denominator = denominator;
  }
  }


Or resolve the scope manually:
1
2
3
4
5
6
7
8
9
10
  void Fraction::setFraction(int numerator, int denominator) {
  if (numerator < 0 || denominator < 1) {
  this->numerator = 0;
  this->denominator = 1;
  }
  else {
  this->numerator = numerator;
  this->denominator = denominator;
  }
  }


You might also want to check your default parameter values as well.
For a complete fraction class, see http://coliru.stacked-crooked.com/a/690cc09932c82fa3
Yo don't need gcd() as this is now part of C++. Consider with a bit of tidying up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <numeric>
using namespace std;

class Fraction { //setting fraction class
private:
	int denominator = 1;
	int numerator = 1;
	void reduce();

public:
	//Fraction
	Fraction() = default;
	Fraction(int numer, int denom);
	int getNum() const;
	int getDen() const;

	void setNum(int numer);
	void setDen(int denomin);
	void setFraction(int numer, int denom);
	int getFraction() const;

	string toString() const; //return as a string
};

//Fraction constructor method
Fraction::Fraction(int numer, int denom) {
	setFraction(numer, denom);
}

void Fraction::setNum(int numer) {
	setFraction(numer, getDen());
}

void Fraction::setDen(int denom) {
	setFraction(getNum(), denom);
}

int Fraction::getNum() const {
	return numerator;
}

int Fraction::getDen() const {
	return denominator;
}

void Fraction::setFraction(int numer, int denom) {
	if (numer < 0 || denom < 1) {
		numerator = 0;
		denominator = 1;
	} else {
		numerator = numer;
		denominator = denom;
	}

	reduce();
}

void Fraction::reduce()
{
	const int GCD = gcd(numerator, denominator);

	numerator /= GCD;
	denominator /= GCD;
}

int Fraction::getFraction() const {
	return numerator / denominator;
}

string Fraction::toString() const {
	return to_string(numerator) + "/" + std::to_string(denominator);
}


int main() {

	Fraction myfrac;

	cout << "\nNum: " << myfrac.getNum() << "  Dem: " << myfrac.getDen() << '\n';
	cout << "Fraction is: " << myfrac.getFraction() << '\n';
	cout << "The Fraction is: " << myfrac.toString() << '\n';

	//setting the numerator and denominator
	myfrac.setNum(3);
	myfrac.setDen(2);
	cout << "\nNum: " << myfrac.getNum() << "  Dem: " << myfrac.getDen() << '\n';
	cout << "Fraction is: " << myfrac.getFraction() << '\n';
	cout << "The Fraction is: " << myfrac.toString() << '\n';

	//setting the numerator and denominator
	myfrac.setNum(3);
	myfrac.setDen(-2);
	cout << "\nNum: " << myfrac.getNum() << "  Dem: " << myfrac.getDen() << '\n';
	cout << "Fraction is: " << myfrac.getFraction() << '\n';
	cout << "The Fraction is: " << myfrac.toString() << '\n';

	//setting the numerator and denominator
	myfrac.setNum(-9);
	myfrac.setDen(4);
	cout << "\nNum: " << myfrac.getNum() << "  Dem: " << myfrac.getDen() << '\n';
	cout << "Fraction is: " << myfrac.getFraction() << '\n';
	cout << "The Fraction is: " << myfrac.toString() << '\n';

	//setting the numerator and denominator
	myfrac.setNum(8);
	myfrac.setDen(0);
	cout << "\nNum: " << myfrac.getNum() << "  Dem: " << myfrac.getDen() << '\n';
	cout << "Fraction is: " << myfrac.getFraction() << '\n';
	cout << "The Fraction is: " << myfrac.toString() << '\n';
}

Need to put gcd() as it isn't part of repl.it and it is part of the assignment. otherwise it doesn't compile. Also thankyou for the link but it doesn't work for me?
Last edited on
That code compiles OK with repl.it. See https://repl.it/repls/SeriousStingyFolders#main.cpp

That link is OK - but the code doesn't work in coliru as command line is required.

Here's a repl link to the working code https://repl.it/repls/HappyTerrificProcessor#main.cpp

The display question raised at the start is solved thus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int numerator;
    int denominator;
    
    numerator = 7;
    denominator = 2;
    
    if(numerator > denominator)
        std::cout << numerator / denominator << '-';
    
    std::cout << numerator % denominator << '/' << denominator << '\n';
    
    return 0;
}


Topic archived. No new replies allowed.