having trouble with value returning functions anyone :(


[/code]
Last edited on
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?
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
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.
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.