Filling rational number struct

I am having issues with my getRationalNumber function. No matter what variables I use or numbers I input I get weird output values.


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <iomanip>
using namespace std;

//declare a struct called rNum that has two fields called top and bot.

struct rNum
{
  int top;
  int bot;
};

rNum getRationalNumber();

rNum addRationalNumbers(rNum n1, rNum n2);

void  print( rNum n1, rNum n2, rNum ans );

int main()
{
  rNum num1;
  rNum num2;
  rNum answer;

  cout << "Rational Number 1" << endl;
  //call getRationalNumber function to get the first rational number
  getRationalNumber();
  
  cout << "Rational Number 2" << endl;
  //call getRationalNumber function to get the second rational number
  getRationalNumber();
  
  //call addRationalNumbers function to get the sum of the two rational numbers
  addRationalNumbers(num1, num2);

  //call print function to show the answer
  print(num1, num2, answer);

  return 0;
};

/***************************************************************************\
 ********
n is a rational number

This function simplifies n, which is the rational number in the caller
****************************************************************************\
********/
/*
int simplify(rNum)
{
cout << "top num is" << num.top << endl;
/*
  if(num.bot == 0)
    {
      return num.top;
 else
    {
      if(num.top % num.bot == 0)
        {
          num.top = num.top / num.bot;
        }
      if(num.bot % num.top == 0)
        {
          num.bot = num.bot / num.bot;
        }
    }
  return num;/
}
*/

/***************************************************************************\
 *******
This function fills a rational number with the top and bottom numbers entere\
d from the keyboard
and returns it.
****************************************************************************\
********/

rNum getRationalNumber()
{
  rNum  num; //rational number

  cout << "\tEnter the top number: ";
  cin >> num.top;
  cout << "\tEnter the bottom number: ";
  cin >> num.bot;

  return num;
}


/***************************************************************************
n1 is the first rational number
n2 is the second rational number

This function returns the sum of n1 and n2 in the simplest format.
**************************************************************************/

rNum addRationalNumbers(rNum n1, rNum n2)
{
  rNum  ans;

  ans.top = (n1.top * n2.bot) + (n2.top * n1.bot);
  ans.bot = n1.bot * n2.bot;

  //call simplify here
  //simplify(ans)

  return ans;
}


/***************************************************************************
n1 is the first rational number
n2 is the second rational number
ans is the sum of n1 and n2

This function will show the answer in the following format:

    6      13       5
 ----- + ----- = -----
    5      10       2
********************************************************************************/

void  print( rNum n1, rNum n2, rNum ans )
{
  cout << n1.top << "\t\t " << n2.top << "\t\t " << ans.top << endl;
  cout << "--" << "\t+\t" << "--" << "\t=\t" << "---" << endl;
  cout << n1.bot << "\t\t " << n2.bot << "\t\t " << ans.bot << endl;

}



Here is a sample of my output:

Rational Number 1
        Enter the top number: 2
        Enter the bottom number: 3
Rational Number 2
        Enter the top number: 5
        Enter the bottom number: 7
1099091392               4197152                 0
--      +       --      =       ---
32766            0               0


Last edited on
getRationalNumber();

You aren't doing anything with the returned value, so when does num1 or num2 ever get set? Never.

addRationalNumbers(num1, num2);
You aren't doing anything with the returned value, so when does answer ever get set? Never.
Topic archived. No new replies allowed.