having trouble with value returning functions anyone :(

Nov 29, 2016 at 4:53am

[/code]
Last edited on Nov 29, 2016 at 1:34pm
Nov 29, 2016 at 6:03am
The only function that I see returning a value here is main, and it seems to be returning a value as expected. Which function are you referring to?
Nov 29, 2016 at 6:15am
the instructions say that my program should use two functions besides main. (One void function and one value returning function.) Could I perhaps make another value returning function for
1
2
3
4
 
if (numEnt % 2 == 0)
            cout << numEnt << " is even." << endl;  
void printAns (int numEnt){ //where it checks if number is even, off, positive or negative 

something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if (even== 0)
            cout << numEnt << " is even." << endl; //if divisible by 2 then it's even
        else
            cout << numEnt << " is odd." << endl; //if it is not divisible by 2 then it's odd
        if (numEnt  >= 0)
            cout << numEnt << " positive." << endl; //if number is greater or equal to 0 then it's positive
        else
            cout << numEnt << " is negative." << endl; //if number is less than 0 it's negative
}

int even (int numEnt){
numEnt % 2 = even
return even;
}
Last edited on Nov 29, 2016 at 6:17am
Nov 29, 2016 at 6:19am
I guess you could make printAnswer return the value of the answer instead of printing it and then do something like cout << "Answer = " << printAnswer(numEnt) << endl;
Somewhat vague assignments were always my favorite, for sure.
Nov 29, 2016 at 6:50am
You can have your function do it's calculations, as your printAnswer is doing. Once calculations are complete return a value back to your main function. I think returning boolean values would be the easiest.



Obviously this code won't compile, but you get the idea. You can call another function to get positive or negative the same way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool someFunct(int someNum)
{ 
    if (sumNum % 2 == 0) 
        return true;
    else
        return false;
}

int main() {


    displayIntro();

    cin >> someNum;
    bEven = someFunct(someNum);

    if (bEven)
        cout << "even"
    else 
        cout << "odd"
    return 0;
}

Topic archived. No new replies allowed.