C++ Fractions Program

May 28, 2010 at 12:55pm
I have a program I need to make, it must add, subtract, and multiply fractions. I get the following errors on the bottom.

The program must run on void functions.
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Fraction Calculator
*/

#include <iostream>

using namespace std;

// Declare a fraction (defines an implicit typedef)


struct fraction
{
    int num;                        // Declare Numerator Variable
    int den;                        // Declare Denominator Variable
};

void functadd(fraction a, fraction b);


void functsubtr(fraction a, fraction b);


void functmulti(fraction a, fraction b);



void functdivide(fraction a, fraction b);


int main()
{
	int ans;
    fraction number[2];          
    char c;                         // Declare c, a dummy character to "eat"
                                    //  the slashes in the input fractions
    char oprtr;                     // Declare oprtr, the operator in the
                                    //  input expression
    fraction result;                // Declare result, a fraction to store the
                                    //  result of the computation
    fraction reduced;               // Declare reduced, a fraction to store the
                                    //  reduced result of the computation
    bool n = true;                  // Declare n, a boolean determining whether
                                    //  the program continues, defaults to true
    bool showans;                   // Declare showans, a boolean determining
                                    //  whether the answer is shown
    char cont; 

    do  
    {
        showans = true;

        // Prompt user for input fractions
        cout << "\nEnter Values in this form  2/3+94/3: ";
        cin >> number[0].num >> c >> number[0].den >>    // 1st fraction
                oprtr >>                             // Operator
                number[1].num >> c >> number[1].den;    // 2nd fraction

        switch(oprtr)
        {
        case '+':
            // User entered a addition sign, add the fractions
            return ans;
            break;

        case '-':
            // User entered a subtraction sign, subtract the fractions
            return ans;
            break;

        case '*':
            // User entered a multiplication sign, multiply the fractions
            return ans;
            break;

        case '/':
            // User entered a division sign, divide the fractions
            return ans;
            break;

        default:
            // User entered something else, show an error message and don't
            //  display the answer
            cout << "You have used an invalid operator.\n";
            cout << "Please use any one of following operators: +, -, *, /\n";
            showans = false;
            break;
        }


        if(showans)
        {
           
            // Display the un-reduced result
            cout << "Answer = " << result.num << "/" << result.den;

            // User will be stuck in this loop until they enter either: y,Y,n,N
            do{
                // Ask the user about whether to continue / repeat
                cout << "\n";
                cout << "Continue? y/n \n";
                // Accept user input
                cin >> cont;
            } while((cont != 'y')&&(cont != 'Y')&&(cont != 'n')&&(cont != 'N'));

            // If the user entered n, break out of this loop.
            //  (If the user entered y, don't do anything, n = true).
            if((cont == 'n') || (cont == 'N'))
            {
                n = false;
            }

        }
    } while(n);
	
    // Return 0 for a successful exit
    return 0;

}

void functadd(fraction a,fraction b);
{

    // Declare ans, the fraction resulting from the operation
    fraction ans;                 
    // Calculate the numerator and denominator of the resulting fraction
    ans.num = (a.num * b.den) + (b.num * a.den);
    ans.den = a.den * b.den;
    // Return the resulting fraction
    result = functadd(number[0], number[1]);
	return result;
}

void functsubtr(fraction a, fraction b);
{
    // Declare ans, the fraction resulting from the operation
    fraction ans;                 
    // Calculate the numerator and denominator of the resulting fraction
    ans.num = (a.num * b.den) - (b.num * a.den);
    ans.den = a.den * b.den;
    // Return the resulting fraction
    result = functsubtr(number[0], number[1]);
	return ans;
}


void functmulti(fraction a, fraction b);
{
    // Declare ans, the fraction resulting from the operation
    fraction ans;                 
    // Calculate the numerator and denominator of the resulting fraction
    ans.num = a.num * b.num;
    ans.den = a.den * b.den;
    // Return the resulting fraction
	result = functmulti(number[0], number[1]);
    ;
}


void functdivide(fraction a, fraction b);
{
    // Declare ans, the fraction resulting from the operation
    fraction ans;                 
    // Calculate the numerator and denominator of the resulting fraction
    ans.num = a.num * b.den;
    ans.den = a.den * b.num;
    // Return the resulting fraction
	result = functdivide(number[0], number[1])
    return ans;
}




1>.\fract.cpp(122) : error C2447: '{' : missing function header (old-style formal list?)
1>.\fract.cpp(135) : error C2447: '{' : missing function header (old-style formal list?)
1>.\fract.cpp(148) : error C2447: '{' : missing function header (old-style formal list?)
1>.\fract.cpp(161) : error C2447: '{' : missing function header (old-style formal list?)
Last edited on May 28, 2010 at 12:56pm
May 28, 2010 at 1:34pm
closed account (z05DSL3A)
Lines 121, 134, 147, and 160 have an ; at then end that should not be there.
May 28, 2010 at 2:32pm
Also you can not use return to return the result with a void function.

If you MUST use void functions then you need to return the result as one of the parameters.

One way to do this is to declare a return parameter as a reference:

1
2
3
4
5

void functdivide(fraction a, fraction b, fraction& answer)
{
}


Note that the fraction answer is passed using the & symbol making it a reference. This means that setting the value of answer sets the value in the variable you pass in:

1
2
3
4
5
6
7
8
9
10
11

fraction a;
fraction b;

fraction c;

functdivide(a, b, c);

// now c contains the answer

Last edited on May 28, 2010 at 2:32pm
May 28, 2010 at 3:17pm
Are you sure you understood the task description properly? The add/muliply functions should be operators of the fraction class, everything else doesn't make much sense. Least of all functions that return void. Even though it would work the way Galik proposed, it's an unnatural solution.
May 30, 2010 at 2:26pm
I am using Visual studio 2008 .When i make some of the correction in the program and try to compile the program i get the error as .


error C2061: syntax error : identifier '_TCHAR'


in the start of the program in main()

 
int _tmain(int argc, _TCHAR* argv[])
.
Any suggestion is welcome one .
Thanks .
Last edited on May 30, 2010 at 2:27pm
Topic archived. No new replies allowed.