Using structs in functions?

Hello, I am trying to create a program that uses structs to perform fraction multiplication. I need the actual multiplication to be performed in a function. I am sure how to do this and have failed so far. I think I am having issues in both the function definition and in the call. Any feed back would be great!
Thanks

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
 #include <cstdlib>
#include <iostream>
using namespace std;


struct fraction//struct creation
{
    int num;
    int denom;
    bool positive;
    
}fraction;

fraction fracMult(fraction& f1,fraction& f2);// Function define
/*
 * 
 */
int main(int argc, char** argv) 
{
    fraction f1,f2, fresult;//made two new fraction structs.
    char tempchar;
    cout << "Input the first numerator"<<endl;
    cin >> f1.num;
    cout << "input the first denominator"<< endl;
    cin >> f1.denom;
    cout << "Is the fraction positive?(Y or N)"<<endl;
    cin >> tempchar;
    if (tempchar=='Y')//If the the value is positive, tempchar=true, else, it is false. 
        f1.positive=true;
    else f1.positive=false;
     cout << "Input the second numerator"<<endl;
    cin >> f2.num;
    cout << "input the second denominator"<< endl;
    cin >> f2.denom;
    cout << "Is the fraction positive?(Y or N)"<<endl;
    cin >> tempchar;
   
    if (tempchar=='Y')
        f2.positive=true;
    else f2.positive=false;
      cout<< "the result is: ";
     fracMult(f1,f2);
        if(!fresult.positive)//if the statement is false, add a - sign. 
        {
            cout << "-";
        }
    cout << fresult.num << "/" << fresult.denom << endl;
            
      return 0;
}
fraction fracMult(f1, f2)//
{
    fraction fresult;
  fresult.num=f1.num*f2.num;
    fresult.denom=f1.denom*f2.denom;
    if(f1.positive==f2.positive)
    {
        fresult.positive=true;
    }
    else fresult.positive=false;
    return fresult;
}
line 42: fracMult returns a fraction, but you ignore the value returned.

 
  fresult = fracMult(f1,f2);

the fraction returned is fresult. I use it later in the if statement and then in the cout. what do you mean by ignore? Do i need to define somethingmore in the function call?
thanks
It's been awhile since AbstractionAnon has replied, hopefully he won't mind if I help you now.

You can either have a function that returns something, then assign that value to another variable, just like AA has shown, or you can send a reference as an argument to your function, assign a value to it inside the function, so it's value will change in the scope of main.

The fresult on line 53 is not the same as the one on line 20: variables declared inside a function only last until the end of that function and having nothing to do with a variable with the same name in any other function. That is called function scope. The value of variables that are arguments to a function, parameters by the time they arrive at the function definition are only copies of the original, so assigning values to them doesn't do what you want either.

If you want to use references, then try this declaration:

void fracMult(fraction& f1, fraction& f2, fraction& fresult);// Function define declaration

The function definition is on lines 51 - 62.

and alter the function call on line 42:

fracMult(f1,f2, fresult);

and alter the function definition to match the declaration.

Of the the two different methods, AA's solution is probably the easiest. I just wanted you to understand function return values better.

Hope all goes well.
Okay, so I tried it by reference first
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
#include <cstdlib>
#include <iostream>
using namespace std;


struct fraction//struct creation
{
    int num;
    int denom;
    bool positive;
    
}fraction;

void fracMult(fraction& f1,fraction& f2, fraction& fresult);// Function declaration
/*2
 * 
 */
int main(int argc, char** argv) 
{
    fraction f1,f2,fresult;//made two new fraction structs.
    char tempchar;
    cout << "Input the first numerator"<<endl;
    cin >> f1.num;
    cout << "input the first denominator"<< endl;
    cin >> f1.denom;
    cout << "Is the fraction positive?(Y or N)"<<endl;
    cin >> tempchar;
    if (tempchar=='Y')//If the the value is positive, tempchar=true, else, it is false. 
        f1.positive=true;
    else f1.positive=false;
    cout << "Input the second numerator"<<endl;
    cin >> f2.num;
    cout << "input the second denominator"<< endl;
    cin >> f2.denom;
    cout << "Is the fraction positive?(Y or N)"<<endl;
    cin >> tempchar;
   
    if (tempchar=='Y')
        f2.positive=true;
    else f2.positive=false;
    fracMult(f1,f2,fresult);
      cout<< "the result is: ";
    if(!fresult.positive)//if the statement is false, add a - sign. 
        {
            cout << "-";
        }
    cout << fresult.num << "/" << fresult.denom << endl;
            
      return 0;
}
fraction fracMult(fraction& f1, fraction& f2, fraction& fresult)//
{
    fresult.num=f1.num*f2.num;
    fresult.denom=f1.denom*f2.denom;
    if(f1.positive==f2.positive)
    {
        fresult.positive=true;
    }
    else fresult.positive=false;
    return fresult;
}

When I try and build, I get tons of errors but nothing is highlighted in netbeans.
ERRORS
fraction.cpp:24:15: error: variable or field ‘fracMult’ declared void
void fracMult(fraction& f1,fraction& f2, fraction& fresult);// Function declaration
^
fraction.cpp:24:25: error: ‘f1’ was not declared in this scope
void fracMult(fraction& f1,fraction& f2, fraction& fresult);// Function declaration
^
fraction.cpp:24:38: error: ‘f2’ was not declared in this scope
void fracMult(fraction& f1,fraction& f2, fraction& fresult);// Function declaration
^
fraction.cpp:24:52: error: ‘fresult’ was not declared in this scope
void fracMult(fraction& f1,fraction& f2, fraction& fresult);// Function declaration
^
fraction.cpp: In function ‘int main(int, char**)’:
fraction.cpp:30:14: error: expected ‘;’ before ‘f1’
fraction f1,f2,fresult;//made two new fraction structs.
^
fraction.cpp:33:12: error: ‘f1’ was not declared in this scope
cin >> f1.num;
^
fraction.cpp:42:12: error: ‘f2’ was not declared in this scope
cin >> f2.num;
^
fraction.cpp:51:20: error: ‘fresult’ was not declared in this scope
fracMult(f1,f2,fresult);
^
fraction.cpp:51:27: error: ‘fracMult’ was not declared in this scope
fracMult(f1,f2,fresult);
^
fraction.cpp: At global scope:
fraction.cpp:61:1: error: ‘fraction’ does not name a type
fraction fracMult(fraction& f1, fraction& f2, fraction& fresult)//
^
make: *** [fraction] Error 1


CLEAN FAILED (exit value 2, total time: 465ms)
Your declaration and your implementation do not agree. They must do so.
1
2
3
4
// Declaration
void fracMult(fraction& f1,fraction& f2, fraction& fresult);
//Implementation
fraction fracMult(fraction& f1, fraction& f2, fraction& fresult)

The declaration says it returns nothing, while the implementation says it returns an object of type fraction.

NEVER MIND FIGURED IT OUT!
I have to put 'struct' in front of fraction in all the function stuff!
 
struct fraction fracMult(struct fraction& f1, struct fraction& f2, struct fraction& fresult)

Thanks alot. Yay now I can actually sleep
Only 3 more weeks of c++!
Topic archived. No new replies allowed.